MCPcopy
hub / github.com/vitejs/vite / runPostCSS

Function runPostCSS

packages/vite/src/node/plugins/css.ts:1708–1807  ·  view source on GitHub ↗
(
  id: string,
  code: string,
  plugins: PostCSS.AcceptedPlugin[],
  options: PostCSS.ProcessOptions,
  deps: Set<string> | undefined,
  logger: Logger,
  enableSourcemap: boolean,
)

Source from the content-addressed store, hash-verified

1706}
1707
1708async function runPostCSS(
1709 id: string,
1710 code: string,
1711 plugins: PostCSS.AcceptedPlugin[],
1712 options: PostCSS.ProcessOptions,
1713 deps: Set<string> | undefined,
1714 logger: Logger,
1715 enableSourcemap: boolean,
1716) {
1717 let postcssResult: PostCSS.Result
1718 try {
1719 const source = removeDirectQuery(id)
1720 const postcss = await importPostcss()
1721
1722 // postcss is an unbundled dep and should be lazy imported
1723 postcssResult = await postcss.default(plugins).process(code, {
1724 ...options,
1725 to: source,
1726 from: source,
1727 ...(enableSourcemap
1728 ? {
1729 map: {
1730 inline: false,
1731 annotation: false,
1732 // postcss may return virtual files
1733 // we cannot obtain content of them, so this needs to be enabled
1734 sourcesContent: true,
1735 // when "prev: preprocessorMap", the result map may include duplicate filename in `postcssResult.map.sources`
1736 // prev: preprocessorMap,
1737 },
1738 }
1739 : {}),
1740 })
1741
1742 // record CSS dependencies from @imports
1743 for (const message of postcssResult.messages) {
1744 if (message.type === 'dependency') {
1745 deps?.add(normalizePath(message.file as string))
1746 } else if (message.type === 'dir-dependency') {
1747 // https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#3-dependencies
1748 const { dir, glob: globPattern = '**' } = message
1749 const files = globSync(globPattern, {
1750 absolute: true,
1751 cwd: path.resolve(path.dirname(id), dir),
1752 expandDirectories: false,
1753 ignore: ['**/node_modules/**'],
1754 })
1755 for (let i = 0; i < files.length; i++) {
1756 deps?.add(files[i])
1757 }
1758 } else if (message.type === 'warning') {
1759 const warning = message as PostCSS.Warning
1760 let msg = `[vite:css][postcss] ${warning.text}`
1761 msg += `\n${generateCodeFrame(
1762 code,
1763 {
1764 line: warning.line,
1765 column: warning.column - 1, // 1-based

Callers 2

compilePostCSSFunction · 0.85
transformSugarSSFunction · 0.85

Calls 8

removeDirectQueryFunction · 0.90
normalizePathFunction · 0.90
generateCodeFrameFunction · 0.90
cleanUrlFunction · 0.90
formatPostcssSourceMapFunction · 0.85
addMethod · 0.80
resolveMethod · 0.65
warnMethod · 0.65

Tested by

no test coverage detected