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

Function runPostCSS

packages/vite/src/node/plugins/css.ts:1705–1804  ·  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

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