Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(css): show lightningcss warnings #19076

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading