Home / Function/ compileLightningCSS() — vite Function Reference

compileLightningCSS() — vite Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  cc40a51d_2143_be43_f128_4841bfa5e9d3["compileLightningCSS()"]
  c3eb47df_971b_0616_6c9f_29b3ded72224["css.ts"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|defined in| c3eb47df_971b_0616_6c9f_29b3ded72224
  c641d137_b4cd_833f_0387_7d29ec90fde1["compileCSS()"]
  c641d137_b4cd_833f_0387_7d29ec90fde1 -->|calls| cc40a51d_2143_be43_f128_4841bfa5e9d3
  2c0b33cf_71e1_14ee_43d1_386be6a53193["removeDirectQuery()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| 2c0b33cf_71e1_14ee_43d1_386be6a53193
  a9d61901_6308_b6b2_88c5_34b648541b05["importLightningCSS()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| a9d61901_6308_b6b2_88c5_34b648541b05
  3eaf8b50_562d_6dc6_0d63_a33906ef9339["isPreProcessor()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| 3eaf8b50_562d_6dc6_0d63_a33906ef9339
  e0336cc5_add2_5b51_6027_5f5c568b2852["compileCSSPreprocessors()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| e0336cc5_add2_5b51_6027_5f5c568b2852
  8a5344e1_7e0d_70d0_9c51_6a40c301277b["transformSugarSS()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| 8a5344e1_7e0d_70d0_9c51_6a40c301277b
  dfa2b928_25a4_a78f_1e11_1e7e643cae09["resolve()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| dfa2b928_25a4_a78f_1e11_1e7e643cae09
  be351481_35b7_f392_a229_ac14e1fa7efb["checkPublicFile()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| be351481_35b7_f392_a229_ac14e1fa7efb
  b1f5b07b_f692_69cd_1795_627055928bb7["getTopLevelConfig()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| b1f5b07b_f692_69cd_1795_627055928bb7
  40ac1f90_7a40_f7a5_f25c_701a010aa803["getAtImportResolvers()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| 40ac1f90_7a40_f7a5_f25c_701a010aa803
  0dbffdb0_e182_a4a7_e983_7fa79c03a9d5["getLightningCssErrorMessageForIeSyntaxes()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| 0dbffdb0_e182_a4a7_e983_7fa79c03a9d5
  310ed049_c1b4_c917_b399_81bab290e5a2["generateCodeFrame()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| 310ed049_c1b4_c917_b399_81bab290e5a2
  bafacb32_492f_c34a_cdad_d01b78d6a2b3["skipUrlReplacer()"]
  cc40a51d_2143_be43_f128_4841bfa5e9d3 -->|calls| bafacb32_492f_c34a_cdad_d01b78d6a2b3
  style cc40a51d_2143_be43_f128_4841bfa5e9d3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/vite/src/node/plugins/css.ts lines 3184–3386

async function compileLightningCSS(
  environment: PartialEnvironment,
  id: string,
  src: string,
  deps: Set<string>,
  workerController: PreprocessorWorkerController,
  urlResolver?: CssUrlResolver,
): Promise<{
  code: string
  map?: string | undefined
  modules?: Record<string, string>
}> {
  const { config } = environment
  // replace null byte as lightningcss treats that as a string terminator
  // https://github.com/parcel-bundler/lightningcss/issues/874
  const filename = removeDirectQuery(id).replace('\0', NULL_BYTE_PLACEHOLDER)

  let res: LightningCssTransformAttributeResult | LightningCssTransformResult
  try {
    res = styleAttrRE.test(id)
      ? (await importLightningCSS()).transformStyleAttribute({
          filename,
          code: Buffer.from(src),
          targets: config.css.lightningcss?.targets,
          minify: config.isProduction && !!config.build.cssMinify,
          analyzeDependencies: true,
        })
      : await (
          await importLightningCSS()
        ).bundleAsync({
          ...config.css.lightningcss,
          filename,
          // projectRoot is needed to get stable hash when using CSS modules
          projectRoot: config.root,
          resolver: {
            async read(filePath) {
              if (filePath === filename) {
                return src
              }

              const code = fs.readFileSync(filePath, 'utf-8')
              const lang = CSS_LANGS_RE.exec(filePath)?.[1] as
                | CssLang
                | undefined
              if (isPreProcessor(lang)) {
                const result = await compileCSSPreprocessors(
                  environment,
                  id,
                  lang,
                  code,
                  workerController,
                )
                result.deps?.forEach((dep) => deps.add(dep))
                // TODO: support source map
                return result.code
              } else if (lang === 'sss') {
                const sssResult = await transformSugarSS(environment, id, code)
                // TODO: support source map
                return sssResult.code
              }
              return code
            },
            async resolve(id, from) {
              const publicFile = checkPublicFile(
                id,
                environment.getTopLevelConfig(),
              )
              if (publicFile) {
                return publicFile
              }

              // NOTE: with `transformer: 'postcss'`, CSS modules `composes` tried to resolve with
              //       all resolvers, but in `transformer: 'lightningcss'`, only the one for the
              //       current file type is used.
              const atImportResolvers = getAtImportResolvers(
                environment.getTopLevelConfig(),
              )
              const lang = CSS_LANGS_RE.exec(from)?.[1] as CssLang | undefined
              let resolver: ResolveIdFn
              switch (lang) {
                case 'css':

Domain

Subdomains

Called By

Frequently Asked Questions

What does compileLightningCSS() do?
compileLightningCSS() is a function in the vite codebase, defined in packages/vite/src/node/plugins/css.ts.
Where is compileLightningCSS() defined?
compileLightningCSS() is defined in packages/vite/src/node/plugins/css.ts at line 3184.
What does compileLightningCSS() call?
compileLightningCSS() calls 14 function(s): checkPublicFile, compileCSSPreprocessors, generateCodeFrame, getAtImportResolvers, getLightningCssErrorMessageForIeSyntaxes, getTopLevelConfig, importLightningCSS, isPreProcessor, and 6 more.
What calls compileLightningCSS()?
compileLightningCSS() is called by 1 function(s): compileCSS.

Analyze Your Own Codebase

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

Try Supermodel Free