Home / Function/ runPostCSS() — vite Function Reference

runPostCSS() — vite Function Reference

Architecture documentation for the runPostCSS() function in css.ts from the vite codebase.

Entity Profile

Dependency Diagram

graph TD
  bfb7f285_fb76_2283_e68f_d806d799034d["runPostCSS()"]
  c3eb47df_971b_0616_6c9f_29b3ded72224["css.ts"]
  bfb7f285_fb76_2283_e68f_d806d799034d -->|defined in| c3eb47df_971b_0616_6c9f_29b3ded72224
  6aa50cd4_4412_2a02_b76b_495614715811["compilePostCSS()"]
  6aa50cd4_4412_2a02_b76b_495614715811 -->|calls| bfb7f285_fb76_2283_e68f_d806d799034d
  8a5344e1_7e0d_70d0_9c51_6a40c301277b["transformSugarSS()"]
  8a5344e1_7e0d_70d0_9c51_6a40c301277b -->|calls| bfb7f285_fb76_2283_e68f_d806d799034d
  2c0b33cf_71e1_14ee_43d1_386be6a53193["removeDirectQuery()"]
  bfb7f285_fb76_2283_e68f_d806d799034d -->|calls| 2c0b33cf_71e1_14ee_43d1_386be6a53193
  9cb3db12_cd74_7c0a_4350_2ff4e6ec6fde["importPostcss()"]
  bfb7f285_fb76_2283_e68f_d806d799034d -->|calls| 9cb3db12_cd74_7c0a_4350_2ff4e6ec6fde
  a4adb1a7_cf54_091f_eb63_8217e684a8e1["normalizePath()"]
  bfb7f285_fb76_2283_e68f_d806d799034d -->|calls| a4adb1a7_cf54_091f_eb63_8217e684a8e1
  dfa2b928_25a4_a78f_1e11_1e7e643cae09["resolve()"]
  bfb7f285_fb76_2283_e68f_d806d799034d -->|calls| dfa2b928_25a4_a78f_1e11_1e7e643cae09
  310ed049_c1b4_c917_b399_81bab290e5a2["generateCodeFrame()"]
  bfb7f285_fb76_2283_e68f_d806d799034d -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  644077f9_8498_35f7_2717_1202c598eccc["formatPostcssSourceMap()"]
  bfb7f285_fb76_2283_e68f_d806d799034d -->|calls| 644077f9_8498_35f7_2717_1202c598eccc
  10b9dea8_362c_1af2_93be_afa4dd9aed9e["cleanUrl()"]
  bfb7f285_fb76_2283_e68f_d806d799034d -->|calls| 10b9dea8_362c_1af2_93be_afa4dd9aed9e
  3cf1d94a_16a2_96d6_7d1d_9757e22a2557["warn()"]
  bfb7f285_fb76_2283_e68f_d806d799034d -->|calls| 3cf1d94a_16a2_96d6_7d1d_9757e22a2557
  style bfb7f285_fb76_2283_e68f_d806d799034d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/css.ts lines 1687–1786

async function runPostCSS(
  id: string,
  code: string,
  plugins: PostCSS.AcceptedPlugin[],
  options: PostCSS.ProcessOptions,
  deps: Set<string> | undefined,
  logger: Logger,
  enableSourcemap: boolean,
) {
  let postcssResult: PostCSS.Result
  try {
    const source = removeDirectQuery(id)
    const postcss = await importPostcss()

    // postcss is an unbundled dep and should be lazy imported
    postcssResult = await postcss.default(plugins).process(code, {
      ...options,
      to: source,
      from: source,
      ...(enableSourcemap
        ? {
            map: {
              inline: false,
              annotation: false,
              // postcss may return virtual files
              // we cannot obtain content of them, so this needs to be enabled
              sourcesContent: true,
              // when "prev: preprocessorMap", the result map may include duplicate filename in `postcssResult.map.sources`
              // prev: preprocessorMap,
            },
          }
        : {}),
    })

    // record CSS dependencies from @imports
    for (const message of postcssResult.messages) {
      if (message.type === 'dependency') {
        deps?.add(normalizePath(message.file as string))
      } else if (message.type === 'dir-dependency') {
        // https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#3-dependencies
        const { dir, glob: globPattern = '**' } = message
        const files = globSync(globPattern, {
          absolute: true,
          cwd: path.resolve(path.dirname(id), dir),
          expandDirectories: false,
          ignore: ['**/node_modules/**'],
        })
        for (let i = 0; i < files.length; i++) {
          deps?.add(files[i])
        }
      } else if (message.type === 'warning') {
        const warning = message as PostCSS.Warning
        let msg = `[vite:css][postcss] ${warning.text}`
        msg += `\n${generateCodeFrame(
          code,
          {
            line: warning.line,
            column: warning.column - 1, // 1-based
          },
          warning.endLine !== undefined && warning.endColumn !== undefined
            ? {
                line: warning.endLine,
                column: warning.endColumn - 1, // 1-based
              }
            : undefined,
        )}`
        logger.warn(colors.yellow(msg))
      }
    }
  } catch (e) {
    e.message = `[postcss] ${e.message}`
    e.code = code
    e.loc = {
      file: e.file,
      line: e.line,
      column: e.column - 1, // 1-based
    }
    throw e
  }

  if (!enableSourcemap) {

Domain

Subdomains

Frequently Asked Questions

What does runPostCSS() do?
runPostCSS() is a function in the vite codebase, defined in packages/vite/src/node/plugins/css.ts.
Where is runPostCSS() defined?
runPostCSS() is defined in packages/vite/src/node/plugins/css.ts at line 1687.
What does runPostCSS() call?
runPostCSS() calls 8 function(s): cleanUrl, formatPostcssSourceMap, generateCodeFrame, importPostcss, normalizePath, removeDirectQuery, resolve, warn.
What calls runPostCSS()?
runPostCSS() is called by 2 function(s): compilePostCSS, transformSugarSS.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free