Skip to content

Commit f427f41

Browse files
fix: handle esbuild warnings
1 parent 7f9a91d commit f427f41

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

src/utils.js

+35-3
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,41 @@ async function esbuildMinify(input, sourceMap, minimizerOptions) {
158158
code: result.code,
159159
// eslint-disable-next-line no-undefined
160160
map: result.map ? result.map : undefined,
161-
warnings: result.warnings
162-
? result.warnings.map((item) => item.toString())
163-
: [],
161+
warnings:
162+
result.warnings.length > 0
163+
? result.warnings.map((item) => {
164+
return {
165+
source: item.location && item.location.file,
166+
line: item.location && item.location.line,
167+
column: item.location && item.location.column,
168+
plugin: item.pluginName,
169+
message: `${item.text}${
170+
item.detail ? `\nDetails:\n${item.detail}` : ""
171+
}${
172+
item.notes.length > 0
173+
? `\n\nNotes:\n${item.notes
174+
.map(
175+
(note) =>
176+
`${
177+
note.location
178+
? `[${note.location.file}:${note.location.line}:${note.location.column}] `
179+
: ""
180+
}${note.text}${
181+
note.location
182+
? `\nSuggestion: ${note.location.suggestion}`
183+
: ""
184+
}${
185+
note.location
186+
? `\nLine text:\n${note.location.lineText}\n`
187+
: ""
188+
}`
189+
)
190+
.join("\n")}`
191+
: ""
192+
}`,
193+
};
194+
})
195+
: [],
164196
};
165197
}
166198

test/__snapshots__/minify-option.test.js.snap

+18
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,24 @@ exports[`"minify" option should work with "CssMinimizerPlugin.cssoMinify" minifi
224224

225225
exports[`"minify" option should work with "CssMinimizerPlugin.cssoMinify" minifier: warning 1`] = `Array []`;
226226

227+
exports[`"minify" option should work with "CssMinimizerPlugin.esbuildMinify" minifier and emit warnings: assets 1`] = `
228+
Object {
229+
"foo.css": "width: calc(100px + 100){}
230+
",
231+
}
232+
`;
233+
234+
exports[`"minify" option should work with "CssMinimizerPlugin.esbuildMinify" minifier and emit warnings: error 1`] = `Array []`;
235+
236+
exports[`"minify" option should work with "CssMinimizerPlugin.esbuildMinify" minifier and emit warnings: warning 1`] = `
237+
Array [
238+
"Warning: foo.css from Css Minimizer plugin
239+
Expected identifier but found whitespace",
240+
"Warning: foo.css from Css Minimizer plugin
241+
Unexpected \\"calc(\\"",
242+
]
243+
`;
244+
227245
exports[`"minify" option should work with "CssMinimizerPlugin.esbuildMinify" minifier and generate source maps: assets 1`] = `
228246
Object {
229247
"foo.css": "body{color:red}a{color:#00f}

test/fixtures/wrong-calc.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
width: calc(100px + 100);

test/minify-option.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,37 @@ describe('"minify" option', () => {
503503
expect(getWarnings(stats)).toMatchSnapshot("warning");
504504
});
505505

506+
it('should work with "CssMinimizerPlugin.esbuildMinify" minifier and emit warnings', async () => {
507+
const compiler = getCompiler({
508+
entry: {
509+
foo: `${__dirname}/fixtures/wrong-calc.css`,
510+
},
511+
module: {
512+
rules: [
513+
{
514+
test: /.s?css$/i,
515+
use: [
516+
MiniCssExtractPlugin.loader,
517+
{ loader: "css-loader", options: { sourceMap: true } },
518+
],
519+
},
520+
],
521+
},
522+
});
523+
524+
new CssMinimizerPlugin({
525+
minify: CssMinimizerPlugin.esbuildMinify,
526+
}).apply(compiler);
527+
528+
const stats = await compile(compiler);
529+
530+
expect(readAssets(compiler, stats, /\.css(\.map)?$/)).toMatchSnapshot(
531+
"assets"
532+
);
533+
expect(getErrors(stats)).toMatchSnapshot("error");
534+
expect(getWarnings(stats)).toMatchSnapshot("warning");
535+
});
536+
506537
it('should work with "CssMinimizerPlugin.esbuildMinify" minifier and generate source maps', async () => {
507538
const compiler = getCompiler({
508539
devtool: "source-map",

0 commit comments

Comments
 (0)