Skip to content

Commit 2fd6db0

Browse files
committed
feat(css): format lightningcss minify errors/warnings
1 parent c5b73af commit 2fd6db0

File tree

1 file changed

+40
-24
lines changed
  • packages/vite/src/node/plugins

1 file changed

+40
-24
lines changed

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

+40-24
Original file line numberDiff line numberDiff line change
@@ -1922,31 +1922,47 @@ async function minifyCSS(
19221922
// See https://github.com/vitejs/vite/pull/13893#issuecomment-1678628198
19231923

19241924
if (config.build.cssMinify === 'lightningcss') {
1925-
const { code, warnings } = (await importLightningCSS()).transform({
1926-
...config.css.lightningcss,
1927-
targets: convertTargets(config.build.cssTarget),
1928-
cssModules: undefined,
1929-
// TODO: Pass actual filename here, which can also be passed to esbuild's
1930-
// `sourcefile` option below to improve error messages
1931-
filename: defaultCssBundleName,
1932-
code: Buffer.from(css),
1933-
minify: true,
1934-
})
1935-
if (warnings.length) {
1936-
config.logger.warn(
1937-
colors.yellow(
1938-
`warnings when minifying css:\n${warnings
1939-
.map((w) => w.message)
1940-
.join('\n')}`,
1941-
),
1942-
)
1943-
}
1925+
try {
1926+
const { code, warnings } = (await importLightningCSS()).transform({
1927+
...config.css.lightningcss,
1928+
targets: convertTargets(config.build.cssTarget),
1929+
cssModules: undefined,
1930+
// TODO: Pass actual filename here, which can also be passed to esbuild's
1931+
// `sourcefile` option below to improve error messages
1932+
filename: defaultCssBundleName,
1933+
code: Buffer.from(css),
1934+
minify: true,
1935+
})
1936+
if (warnings.length) {
1937+
const messages = warnings.map(
1938+
(warning) =>
1939+
`${warning.message}\n` +
1940+
generateCodeFrame(css, {
1941+
line: warning.loc.line,
1942+
column: warning.loc.column - 1, // 1-based
1943+
}),
1944+
)
1945+
config.logger.warn(
1946+
colors.yellow(`warnings when minifying css:\n${messages.join('\n')}`),
1947+
)
1948+
}
19441949

1945-
// NodeJS res.code = Buffer
1946-
// Deno res.code = Uint8Array
1947-
// For correct decode compiled css need to use TextDecoder
1948-
// LightningCSS output does not return a linebreak at the end
1949-
return decoder.decode(code) + (inlined ? '' : '\n')
1950+
// NodeJS res.code = Buffer
1951+
// Deno res.code = Uint8Array
1952+
// For correct decode compiled css need to use TextDecoder
1953+
// LightningCSS output does not return a linebreak at the end
1954+
return decoder.decode(code) + (inlined ? '' : '\n')
1955+
} catch (e) {
1956+
e.message = `[lightningcss minify] ${e.message}`
1957+
if (e.loc) {
1958+
e.loc = {
1959+
line: e.loc.line,
1960+
column: e.loc.column - 1, // 1-based
1961+
}
1962+
e.frame = generateCodeFrame(css, e.loc)
1963+
}
1964+
throw e
1965+
}
19501966
}
19511967
try {
19521968
const { code, warnings } = await transform(css, {

0 commit comments

Comments
 (0)