Skip to content

Commit 7572013

Browse files
committed
feat: use lightningcss by default for cssMinify
1 parent af62e81 commit 7572013

File tree

5 files changed

+57
-56
lines changed

5 files changed

+57
-56
lines changed

packages/vite/package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"dependencies": {
8888
"@oxc-project/runtime": "0.61.2",
8989
"esbuild": "^0.25.0",
90+
"lightningcss": "^1.29.3",
9091
"picomatch": "^4.0.2",
9192
"postcss": "^8.5.3",
9293
"rolldown": "1.0.0-beta.7-commit.7452fa0",
@@ -126,7 +127,6 @@
126127
"etag": "^1.8.1",
127128
"http-proxy": "^1.18.1",
128129
"launch-editor-middleware": "^2.10.0",
129-
"lightningcss": "^1.29.3",
130130
"magic-string": "^0.30.17",
131131
"mlly": "^1.7.4",
132132
"mrmime": "^2.0.1",
@@ -160,7 +160,6 @@
160160
"@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0",
161161
"jiti": ">=1.21.0",
162162
"less": "*",
163-
"lightningcss": "^1.21.0",
164163
"sass": "*",
165164
"sass-embedded": "*",
166165
"stylus": "*",
@@ -191,9 +190,6 @@
191190
"sugarss": {
192191
"optional": true
193192
},
194-
"lightningcss": {
195-
"optional": true
196-
},
197193
"terser": {
198194
"optional": true
199195
},

packages/vite/src/node/build.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ export interface BuildEnvironmentOptions {
161161
/**
162162
* Override CSS minification specifically instead of defaulting to `build.minify`,
163163
* so you can configure minification for JS and CSS separately.
164-
* @default 'esbuild'
164+
* @default 'lightningcss'
165165
*/
166-
cssMinify?: boolean | 'esbuild' | 'lightningcss'
166+
cssMinify?: boolean | 'lightningcss' | 'esbuild'
167167
/**
168168
* If `true`, a separate sourcemap file will be created. If 'inline', the
169169
* sourcemap will be appended to the resulting output file as data URI.
@@ -457,7 +457,8 @@ export function resolveBuildEnvironmentOptions(
457457
...merged,
458458
cssTarget: merged.cssTarget ?? merged.target,
459459
cssMinify:
460-
merged.cssMinify ?? (consumer === 'server' ? 'esbuild' : !!merged.minify),
460+
merged.cssMinify ??
461+
(consumer === 'server' ? 'lightningcss' : !!merged.minify),
461462
// Resolve to false | object
462463
modulePreload:
463464
merged.modulePreload === false

packages/vite/src/node/plugins/css.ts

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,69 +2102,70 @@ async function minifyCSS(
21022102
// regular CSS assets do end with a linebreak.
21032103
// See https://github.com/vitejs/vite/pull/13893#issuecomment-1678628198
21042104

2105-
if (config.build.cssMinify === 'lightningcss') {
2105+
if (config.build.cssMinify === 'esbuild') {
21062106
try {
2107-
const { code, warnings } = (await importLightningCSS()).transform({
2108-
...config.css.lightningcss,
2109-
targets: convertTargets(config.build.cssTarget),
2110-
cssModules: undefined,
2111-
// TODO: Pass actual filename here, which can also be passed to esbuild's
2112-
// `sourcefile` option below to improve error messages
2113-
filename: defaultCssBundleName,
2114-
code: Buffer.from(css),
2115-
minify: true,
2107+
const { code, warnings } = await transform(css, {
2108+
loader: 'css',
2109+
target: config.build.cssTarget || undefined,
2110+
...resolveMinifyCssEsbuildOptions(config.esbuild || {}),
21162111
})
21172112
if (warnings.length) {
2118-
const messages = warnings.map(
2119-
(warning) =>
2120-
`${warning.message}\n` +
2121-
generateCodeFrame(css, {
2122-
line: warning.loc.line,
2123-
column: warning.loc.column - 1, // 1-based
2124-
}),
2125-
)
2113+
const msgs = await formatMessages(warnings, { kind: 'warning' })
21262114
config.logger.warn(
2127-
colors.yellow(`warnings when minifying css:\n${messages.join('\n')}`),
2115+
colors.yellow(`warnings when minifying css:\n${msgs.join('\n')}`),
21282116
)
21292117
}
2130-
2131-
// NodeJS res.code = Buffer
2132-
// Deno res.code = Uint8Array
2133-
// For correct decode compiled css need to use TextDecoder
2134-
// LightningCSS output does not return a linebreak at the end
2135-
return decoder.decode(code) + (inlined ? '' : '\n')
2118+
// esbuild output does return a linebreak at the end
2119+
return inlined ? code.trimEnd() : code
21362120
} catch (e) {
2137-
e.message = `[lightningcss minify] ${e.message}`
2138-
if (e.loc) {
2139-
e.loc = {
2140-
line: e.loc.line,
2141-
column: e.loc.column - 1, // 1-based
2142-
}
2143-
e.frame = generateCodeFrame(css, e.loc)
2121+
if (e.errors) {
2122+
e.message = '[esbuild css minify] ' + e.message
2123+
const msgs = await formatMessages(e.errors, { kind: 'error' })
2124+
e.frame = '\n' + msgs.join('\n')
2125+
e.loc = e.errors[0].location
21442126
}
21452127
throw e
21462128
}
21472129
}
2130+
21482131
try {
2149-
const { code, warnings } = await transform(css, {
2150-
loader: 'css',
2151-
target: config.build.cssTarget || undefined,
2152-
...resolveMinifyCssEsbuildOptions(config.esbuild || {}),
2132+
const { code, warnings } = (await importLightningCSS()).transform({
2133+
...config.css.lightningcss,
2134+
targets: convertTargets(config.build.cssTarget),
2135+
cssModules: undefined,
2136+
// TODO: Pass actual filename here, which can also be passed to esbuild's
2137+
// `sourcefile` option below to improve error messages
2138+
filename: defaultCssBundleName,
2139+
code: Buffer.from(css),
2140+
minify: true,
21532141
})
21542142
if (warnings.length) {
2155-
const msgs = await formatMessages(warnings, { kind: 'warning' })
2143+
const messages = warnings.map(
2144+
(warning) =>
2145+
`${warning.message}\n` +
2146+
generateCodeFrame(css, {
2147+
line: warning.loc.line,
2148+
column: warning.loc.column - 1, // 1-based
2149+
}),
2150+
)
21562151
config.logger.warn(
2157-
colors.yellow(`warnings when minifying css:\n${msgs.join('\n')}`),
2152+
colors.yellow(`warnings when minifying css:\n${messages.join('\n')}`),
21582153
)
21592154
}
2160-
// esbuild output does return a linebreak at the end
2161-
return inlined ? code.trimEnd() : code
2155+
2156+
// NodeJS res.code = Buffer
2157+
// Deno res.code = Uint8Array
2158+
// For correct decode compiled css need to use TextDecoder
2159+
// LightningCSS output does not return a linebreak at the end
2160+
return decoder.decode(code) + (inlined ? '' : '\n')
21622161
} catch (e) {
2163-
if (e.errors) {
2164-
e.message = '[esbuild css minify] ' + e.message
2165-
const msgs = await formatMessages(e.errors, { kind: 'error' })
2166-
e.frame = '\n' + msgs.join('\n')
2167-
e.loc = e.errors[0].location
2162+
e.message = `[lightningcss minify] ${e.message}`
2163+
if (e.loc) {
2164+
e.loc = {
2165+
line: e.loc.line,
2166+
column: e.loc.column - 1, // 1-based
2167+
}
2168+
e.frame = generateCodeFrame(css, e.loc)
21682169
}
21692170
throw e
21702171
}

playground/minify/vite.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ export default defineConfig({
55
legalComments: 'none',
66
minifySyntax: false,
77
},
8+
build: {
9+
cssMinify: 'esbuild',
10+
},
811
})

pnpm-lock.yaml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)