Home / Function/ minifyCSS() — vite Function Reference

minifyCSS() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  297b7917_5996_b011_26d1_5bce9b0e6724["minifyCSS()"]
  c3eb47df_971b_0616_6c9f_29b3ded72224["css.ts"]
  297b7917_5996_b011_26d1_5bce9b0e6724 -->|defined in| c3eb47df_971b_0616_6c9f_29b3ded72224
  cd131d16_e223_ab79_1b7c_8ea449ae51a2["cssPostPlugin()"]
  cd131d16_e223_ab79_1b7c_8ea449ae51a2 -->|calls| 297b7917_5996_b011_26d1_5bce9b0e6724
  06417328_af61_add3_8a48_844e44f416a2["finalizeCss()"]
  06417328_af61_add3_8a48_844e44f416a2 -->|calls| 297b7917_5996_b011_26d1_5bce9b0e6724
  22baac16_2090_e193_e4e5_26aca6378448["importEsbuild()"]
  297b7917_5996_b011_26d1_5bce9b0e6724 -->|calls| 22baac16_2090_e193_e4e5_26aca6378448
  40c65cdf_e886_5196_9ef7_09b523f19a5a["resolveMinifyCssEsbuildOptions()"]
  297b7917_5996_b011_26d1_5bce9b0e6724 -->|calls| 40c65cdf_e886_5196_9ef7_09b523f19a5a
  a9d61901_6308_b6b2_88c5_34b648541b05["importLightningCSS()"]
  297b7917_5996_b011_26d1_5bce9b0e6724 -->|calls| a9d61901_6308_b6b2_88c5_34b648541b05
  6648f04f_fbaf_b4c1_7ca5_d07266e9650c["convertTargets()"]
  297b7917_5996_b011_26d1_5bce9b0e6724 -->|calls| 6648f04f_fbaf_b4c1_7ca5_d07266e9650c
  310ed049_c1b4_c917_b399_81bab290e5a2["generateCodeFrame()"]
  297b7917_5996_b011_26d1_5bce9b0e6724 -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  0dbffdb0_e182_a4a7_e983_7fa79c03a9d5["getLightningCssErrorMessageForIeSyntaxes()"]
  297b7917_5996_b011_26d1_5bce9b0e6724 -->|calls| 0dbffdb0_e182_a4a7_e983_7fa79c03a9d5
  e1266b03_e0f1_635b_5994_e16420bc0263["transform()"]
  297b7917_5996_b011_26d1_5bce9b0e6724 -->|calls| e1266b03_e0f1_635b_5994_e16420bc0263
  3cf1d94a_16a2_96d6_7d1d_9757e22a2557["warn()"]
  297b7917_5996_b011_26d1_5bce9b0e6724 -->|calls| 3cf1d94a_16a2_96d6_7d1d_9757e22a2557
  style 297b7917_5996_b011_26d1_5bce9b0e6724 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/css.ts lines 2197–2275

async function minifyCSS(
  css: string,
  config: ResolvedConfig,
  inlined: boolean,
) {
  // We want inlined CSS to not end with a linebreak, while ensuring that
  // regular CSS assets do end with a linebreak.
  // See https://github.com/vitejs/vite/pull/13893#issuecomment-1678628198

  if (config.build.cssMinify === 'esbuild') {
    const { transform, formatMessages } = await importEsbuild()
    try {
      const { code, warnings } = await transform(css, {
        loader: 'css',
        target: config.build.cssTarget || undefined,
        ...resolveMinifyCssEsbuildOptions(config.esbuild || {}),
      })
      if (warnings.length) {
        const msgs = await formatMessages(warnings, { kind: 'warning' })
        config.logger.warn(
          colors.yellow(`[esbuild css minify]\n${msgs.join('\n')}`),
        )
      }
      // esbuild output does return a linebreak at the end
      return inlined ? code.trimEnd() : code
    } catch (e) {
      if (e.errors) {
        e.message = '[esbuild css minify] ' + e.message
        const msgs = await formatMessages(e.errors, { kind: 'error' })
        e.frame = '\n' + msgs.join('\n')
        e.loc = e.errors[0].location
      }
      throw e
    }
  }

  try {
    const { code, warnings } = (await importLightningCSS()).transform({
      ...config.css.lightningcss,
      targets: convertTargets(config.build.cssTarget),
      cssModules: undefined,
      // TODO: Pass actual filename here, which can also be passed to esbuild's
      // `sourcefile` option below to improve error messages
      filename: defaultCssBundleName,
      code: Buffer.from(css),
      minify: true,
    })

    for (const warning of warnings) {
      let msg = `[lightningcss minify] ${warning.message}`
      msg += `\n${generateCodeFrame(css, {
        line: warning.loc.line,
        column: warning.loc.column - 1, // 1-based
      })}`
      config.logger.warn(colors.yellow(msg))
    }

    // NodeJS res.code = Buffer
    // Deno res.code = Uint8Array
    // For correct decode compiled css need to use TextDecoder
    // LightningCSS output does not return a linebreak at the end
    return decoder.decode(code) + (inlined ? '' : '\n')
  } catch (e) {
    e.message = `[lightningcss minify] ${e.message}`
    const friendlyMessage = getLightningCssErrorMessageForIeSyntaxes(css)
    if (friendlyMessage) {
      e.message += friendlyMessage
    }

    if (e.loc) {
      e.loc = {
        line: e.loc.line,
        column: e.loc.column - 1, // 1-based
      }
      e.frame = generateCodeFrame(css, e.loc)
    }
    throw e
  }
}

Domain

Subdomains

Frequently Asked Questions

What does minifyCSS() do?
minifyCSS() is a function in the vite codebase, defined in packages/vite/src/node/plugins/css.ts.
Where is minifyCSS() defined?
minifyCSS() is defined in packages/vite/src/node/plugins/css.ts at line 2197.
What does minifyCSS() call?
minifyCSS() calls 8 function(s): convertTargets, generateCodeFrame, getLightningCssErrorMessageForIeSyntaxes, importEsbuild, importLightningCSS, resolveMinifyCssEsbuildOptions, transform, warn.
What calls minifyCSS()?
minifyCSS() is called by 2 function(s): cssPostPlugin, finalizeCss.

Analyze Your Own Codebase

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

Try Supermodel Free