Skip to content

Commit

Permalink
feat(css): show lightningcss warnings (#19076)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Dec 27, 2024
1 parent f7b1964 commit b07c036
Showing 1 changed file with 50 additions and 25 deletions.
75 changes: 50 additions & 25 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,7 @@ async function compileCSS(
}
} else if (message.type === 'warning') {
const warning = message as PostCSS.Warning
let msg = `[vite:css] ${warning.text}`
let msg = `[vite:css][postcss] ${warning.text}`
msg += `\n${generateCodeFrame(
code,
{
Expand Down Expand Up @@ -1922,31 +1922,47 @@ async function minifyCSS(
// See https://github.com/vitejs/vite/pull/13893#issuecomment-1678628198

if (config.build.cssMinify === 'lightningcss') {
const { code, warnings } = (await importLightningCSS()).transform({
...config.css.lightningcss,
targets: convertTargets(config.build.cssTarget),
cssModules: undefined,
// TODO: Pass actual filename here, which can also be passed to esbuild's
// `sourcefile` option below to improve error messages
filename: defaultCssBundleName,
code: Buffer.from(css),
minify: true,
})
if (warnings.length) {
config.logger.warn(
colors.yellow(
`warnings when minifying css:\n${warnings
.map((w) => w.message)
.join('\n')}`,
),
)
}
try {
const { code, warnings } = (await importLightningCSS()).transform({
...config.css.lightningcss,
targets: convertTargets(config.build.cssTarget),
cssModules: undefined,
// TODO: Pass actual filename here, which can also be passed to esbuild's
// `sourcefile` option below to improve error messages
filename: defaultCssBundleName,
code: Buffer.from(css),
minify: true,
})
if (warnings.length) {
const messages = warnings.map(
(warning) =>
`${warning.message}\n` +
generateCodeFrame(css, {
line: warning.loc.line,
column: warning.loc.column - 1, // 1-based
}),
)
config.logger.warn(
colors.yellow(`warnings when minifying css:\n${messages.join('\n')}`),
)
}

// NodeJS res.code = Buffer
// Deno res.code = Uint8Array
// For correct decode compiled css need to use TextDecoder
// LightningCSS output does not return a linebreak at the end
return decoder.decode(code) + (inlined ? '' : '\n')
// NodeJS res.code = Buffer
// Deno res.code = Uint8Array
// For correct decode compiled css need to use TextDecoder
// LightningCSS output does not return a linebreak at the end
return decoder.decode(code) + (inlined ? '' : '\n')
} catch (e) {
e.message = `[lightningcss minify] ${e.message}`
if (e.loc) {
e.loc = {
line: e.loc.line,
column: e.loc.column - 1, // 1-based
}
e.frame = generateCodeFrame(css, e.loc)
}
throw e
}
}
try {
const { code, warnings } = await transform(css, {
Expand Down Expand Up @@ -3231,6 +3247,15 @@ async function compileLightningCSS(
throw e
}

for (const warning of res.warnings) {
let msg = `[vite:css][lightningcss] ${warning.message}`
msg += `\n${generateCodeFrame(src, {
line: warning.loc.line,
column: warning.loc.column - 1, // 1-based
})}`
environment.logger.warn(colors.yellow(msg))
}

// NodeJS res.code = Buffer
// Deno res.code = Uint8Array
// For correct decode compiled css need to use TextDecoder
Expand Down

0 comments on commit b07c036

Please sign in to comment.