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

Function minifyCSS

packages/vite/src/node/plugins/css.ts:2215–2293  ·  view source on GitHub ↗
(
  css: string,
  config: ResolvedConfig,
  inlined: boolean,
  filename: string = defaultCssBundleName,
)

Source from the content-addressed store, hash-verified

2213}
2214
2215async function minifyCSS(
2216 css: string,
2217 config: ResolvedConfig,
2218 inlined: boolean,
2219 filename: string = defaultCssBundleName,
2220) {
2221 // We want inlined CSS to not end with a linebreak, while ensuring that
2222 // regular CSS assets do end with a linebreak.
2223 // See https://github.com/vitejs/vite/pull/13893#issuecomment-1678628198
2224
2225 if (config.build.cssMinify === 'esbuild') {
2226 const { transform, formatMessages } = await importEsbuild()
2227 try {
2228 const { code, warnings } = await transform(css, {
2229 loader: 'css',
2230 target: config.build.cssTarget || undefined,
2231 sourcefile: filename,
2232 ...resolveMinifyCssEsbuildOptions(config.esbuild || {}),
2233 })
2234 if (warnings.length) {
2235 const msgs = await formatMessages(warnings, { kind: 'warning' })
2236 config.logger.warn(
2237 colors.yellow(`[esbuild css minify]\n${msgs.join('\n')}`),
2238 )
2239 }
2240 // esbuild output does return a linebreak at the end
2241 return inlined ? code.trimEnd() : code
2242 } catch (e) {
2243 if (e.errors) {
2244 e.message = '[esbuild css minify] ' + e.message
2245 const msgs = await formatMessages(e.errors, { kind: 'error' })
2246 e.frame = '\n' + msgs.join('\n')
2247 e.loc = e.errors[0].location
2248 }
2249 throw e
2250 }
2251 }
2252
2253 try {
2254 const { code, warnings } = (await importLightningCSS()).transform({
2255 ...config.css.lightningcss,
2256 targets: convertTargets(config.build.cssTarget),
2257 cssModules: undefined,
2258 filename,
2259 code: Buffer.from(css),
2260 minify: true,
2261 })
2262
2263 for (const warning of warnings) {
2264 let msg = `[lightningcss minify] ${warning.message}`
2265 msg += `\n${generateCodeFrame(css, {
2266 line: warning.loc.line,
2267 column: warning.loc.column - 1, // 1-based
2268 })}`
2269 config.logger.warn(colors.yellow(msg))
2270 }
2271
2272 // NodeJS res.code = Buffer

Callers 2

handlerFunction · 0.85
finalizeCssFunction · 0.85

Calls 8

generateCodeFrameFunction · 0.90
importEsbuildFunction · 0.85
convertTargetsFunction · 0.85
transformFunction · 0.70
warnMethod · 0.65
transformMethod · 0.45

Tested by

no test coverage detected