Skip to content

Commit

Permalink
fix: handle esbuild warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Nov 8, 2021
1 parent 7f9a91d commit f427f41
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,41 @@ async function esbuildMinify(input, sourceMap, minimizerOptions) {
code: result.code,
// eslint-disable-next-line no-undefined
map: result.map ? result.map : undefined,
warnings: result.warnings
? result.warnings.map((item) => item.toString())
: [],
warnings:
result.warnings.length > 0
? result.warnings.map((item) => {
return {
source: item.location && item.location.file,
line: item.location && item.location.line,
column: item.location && item.location.column,
plugin: item.pluginName,
message: `${item.text}${
item.detail ? `\nDetails:\n${item.detail}` : ""
}${
item.notes.length > 0
? `\n\nNotes:\n${item.notes
.map(
(note) =>
`${
note.location
? `[${note.location.file}:${note.location.line}:${note.location.column}] `
: ""
}${note.text}${
note.location
? `\nSuggestion: ${note.location.suggestion}`
: ""
}${
note.location
? `\nLine text:\n${note.location.lineText}\n`
: ""
}`
)
.join("\n")}`
: ""
}`,
};
})
: [],
};
}

Expand Down
18 changes: 18 additions & 0 deletions test/__snapshots__/minify-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,24 @@ exports[`"minify" option should work with "CssMinimizerPlugin.cssoMinify" minifi

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

exports[`"minify" option should work with "CssMinimizerPlugin.esbuildMinify" minifier and emit warnings: assets 1`] = `
Object {
"foo.css": "width: calc(100px + 100){}
",
}
`;

exports[`"minify" option should work with "CssMinimizerPlugin.esbuildMinify" minifier and emit warnings: error 1`] = `Array []`;

exports[`"minify" option should work with "CssMinimizerPlugin.esbuildMinify" minifier and emit warnings: warning 1`] = `
Array [
"Warning: foo.css from Css Minimizer plugin
Expected identifier but found whitespace",
"Warning: foo.css from Css Minimizer plugin
Unexpected \\"calc(\\"",
]
`;

exports[`"minify" option should work with "CssMinimizerPlugin.esbuildMinify" minifier and generate source maps: assets 1`] = `
Object {
"foo.css": "body{color:red}a{color:#00f}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/wrong-calc.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
width: calc(100px + 100);
31 changes: 31 additions & 0 deletions test/minify-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,37 @@ describe('"minify" option', () => {
expect(getWarnings(stats)).toMatchSnapshot("warning");
});

it('should work with "CssMinimizerPlugin.esbuildMinify" minifier and emit warnings', async () => {
const compiler = getCompiler({
entry: {
foo: `${__dirname}/fixtures/wrong-calc.css`,
},
module: {
rules: [
{
test: /.s?css$/i,
use: [
MiniCssExtractPlugin.loader,
{ loader: "css-loader", options: { sourceMap: true } },
],
},
],
},
});

new CssMinimizerPlugin({
minify: CssMinimizerPlugin.esbuildMinify,
}).apply(compiler);

const stats = await compile(compiler);

expect(readAssets(compiler, stats, /\.css(\.map)?$/)).toMatchSnapshot(
"assets"
);
expect(getErrors(stats)).toMatchSnapshot("error");
expect(getWarnings(stats)).toMatchSnapshot("warning");
});

it('should work with "CssMinimizerPlugin.esbuildMinify" minifier and generate source maps', async () => {
const compiler = getCompiler({
devtool: "source-map",
Expand Down

0 comments on commit f427f41

Please sign in to comment.