Skip to content

Commit c713f79

Browse files
authored
feat(css): improve lightningcss messages (#19880)
1 parent cbdab1d commit c713f79

File tree

1 file changed

+42
-28
lines changed
  • packages/vite/src/node/plugins

1 file changed

+42
-28
lines changed

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

+42-28
Original file line numberDiff line numberDiff line change
@@ -2131,18 +2131,14 @@ async function minifyCSS(
21312131
code: Buffer.from(css),
21322132
minify: true,
21332133
})
2134-
if (warnings.length) {
2135-
const messages = warnings.map(
2136-
(warning) =>
2137-
`${warning.message}\n` +
2138-
generateCodeFrame(css, {
2139-
line: warning.loc.line,
2140-
column: warning.loc.column - 1, // 1-based
2141-
}),
2142-
)
2143-
config.logger.warn(
2144-
colors.yellow(`warnings when minifying css:\n${messages.join('\n')}`),
2145-
)
2134+
2135+
for (const warning of warnings) {
2136+
let msg = `[lightningcss minify] ${warning.message}`
2137+
msg += `\n${generateCodeFrame(css, {
2138+
line: warning.loc.line,
2139+
column: warning.loc.column - 1, // 1-based
2140+
})}`
2141+
config.logger.warn(colors.yellow(msg))
21462142
}
21472143

21482144
// NodeJS res.code = Buffer
@@ -2152,6 +2148,11 @@ async function minifyCSS(
21522148
return decoder.decode(code) + (inlined ? '' : '\n')
21532149
} catch (e) {
21542150
e.message = `[lightningcss minify] ${e.message}`
2151+
const friendlyMessage = getLightningCssErrorMessageForIeSyntaxes(css)
2152+
if (friendlyMessage) {
2153+
e.message += friendlyMessage
2154+
}
2155+
21552156
if (e.loc) {
21562157
e.loc = {
21572158
line: e.loc.line,
@@ -2171,7 +2172,7 @@ async function minifyCSS(
21712172
if (warnings.length) {
21722173
const msgs = await formatMessages(warnings, { kind: 'warning' })
21732174
config.logger.warn(
2174-
colors.yellow(`warnings when minifying css:\n${msgs.join('\n')}`),
2175+
colors.yellow(`[esbuild css minify]\n${msgs.join('\n')}`),
21752176
)
21762177
}
21772178
// esbuild output does return a linebreak at the end
@@ -3503,23 +3504,11 @@ async function compileLightningCSS(
35033504
line: e.loc.line,
35043505
column: e.loc.column - 1, // 1-based
35053506
}
3506-
// add friendly error for https://github.com/parcel-bundler/lightningcss/issues/39
35073507
try {
35083508
const code = fs.readFileSync(e.fileName, 'utf-8')
3509-
const commonIeMessage =
3510-
', which was used in the past to support old Internet Explorer versions.' +
3511-
' This is not a valid CSS syntax and will be ignored by modern browsers. ' +
3512-
'\nWhile this is not supported by LightningCSS, you can set `css.lightningcss.errorRecovery: true` to strip these codes.'
3513-
if (/[\s;{]\*[a-zA-Z-][\w-]+\s*:/.test(code)) {
3514-
// https://stackoverflow.com/a/1667560
3515-
e.message +=
3516-
'.\nThis file contains star property hack (e.g. `*zoom`)' +
3517-
commonIeMessage
3518-
} else if (/min-width:\s*0\\0/.test(code)) {
3519-
// https://stackoverflow.com/a/14585820
3520-
e.message +=
3521-
'.\nThis file contains @media zero hack (e.g. `@media (min-width: 0\\0)`)' +
3522-
commonIeMessage
3509+
const friendlyMessage = getLightningCssErrorMessageForIeSyntaxes(code)
3510+
if (friendlyMessage) {
3511+
e.message += friendlyMessage
35233512
}
35243513
} catch {}
35253514
}
@@ -3596,6 +3585,31 @@ async function compileLightningCSS(
35963585
}
35973586
}
35983587

3588+
// friendly error for https://github.com/parcel-bundler/lightningcss/issues/39
3589+
function getLightningCssErrorMessageForIeSyntaxes(
3590+
code: string,
3591+
): string | undefined {
3592+
const commonIeMessage =
3593+
', which was used in the past to support old Internet Explorer versions.' +
3594+
' This is not a valid CSS syntax and will be ignored by modern browsers. ' +
3595+
'\nWhile this is not supported by LightningCSS, you can set `css.lightningcss.errorRecovery: true` to strip these codes.'
3596+
if (/[\s;{]\*[a-zA-Z-][\w-]+\s*:/.test(code)) {
3597+
// https://stackoverflow.com/a/1667560
3598+
return (
3599+
'.\nThis file contains star property hack (e.g. `*zoom`)' +
3600+
commonIeMessage
3601+
)
3602+
}
3603+
if (/min-width:\s*0\\0/.test(code)) {
3604+
// https://stackoverflow.com/a/14585820
3605+
return (
3606+
'.\nThis file contains @media zero hack (e.g. `@media (min-width: 0\\0)`)' +
3607+
commonIeMessage
3608+
)
3609+
}
3610+
return undefined
3611+
}
3612+
35993613
// Convert https://esbuild.github.io/api/#target
36003614
// To https://github.com/parcel-bundler/lightningcss/blob/master/node/targets.d.ts
36013615

0 commit comments

Comments
 (0)