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

Function minifyCSS

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

Source from the content-addressed store, hash-verified

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