( css: string, config: ResolvedConfig, inlined: boolean, filename: string = defaultCssBundleName, )
| 2213 | } |
| 2214 | |
| 2215 | async 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 |
no test coverage detected