Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ class CssMinimizerPlugin {
innerSourceMap,
true,
);
} else {
} else if (item.code) {
output.source = new RawSource(item.code);
}
}
Expand All @@ -632,7 +632,7 @@ class CssMinimizerPlugin {
inputSourceMap && CssMinimizerPlugin.isSourceMap(inputSourceMap);

for (const error of result.errors) {
output.warnings.push(
output.errors.push(
CssMinimizerPlugin.buildError(
error,
name,
Expand Down Expand Up @@ -691,6 +691,10 @@ class CssMinimizerPlugin {
}
}

if (!output.source) {
return;
}

const newInfo = { minimized: true };
const { source } = output;

Expand Down
6 changes: 4 additions & 2 deletions src/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ async function minify(options) {
);

if (typeof minifyResult.code !== "string") {
throw new Error(
"minimizer function doesn't return the 'code' property or result is not a string value",
result.errors.push(
new Error(
"minimizer function doesn't return the 'code' property or result is not a string value",
),
);
}

Expand Down
55 changes: 51 additions & 4 deletions test/__snapshots__/minify-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`"minify" option should work and allow to return errors and warnings from custom function without code: assets 1`] = `
{
"foo.css": "body {
font-weight: bold;
}

body {
color: red;
}
body a {
text-align: center;
}
",
}
`;

exports[`"minify" option should work and allow to return errors and warnings from custom function without code: error 1`] = `
[
"Error: foo.css from Css Minimizer plugin
Error 1",
"Error: foo.css from Css Minimizer plugin
Error: Error 2",
"Error: foo.css from Css Minimizer plugin
Error: minimizer function doesn't return the 'code' property or result is not a string value",
]
`;

exports[`"minify" option should work and allow to return errors and warnings from custom function without code: warning 1`] = `
[
"Warning: foo.css from Css Minimizer plugin
Warning 1",
"Warning: foo.css from Css Minimizer plugin
Warning 2",
]
`;

exports[`"minify" option should work and allow to return errors and warnings from custom function: assets 1`] = `
{
"foo.css": ".test { color: red; }",
}
`;

exports[`"minify" option should work and allow to return errors and warnings from custom function: error 1`] = `[]`;

exports[`"minify" option should work and allow to return errors and warnings from custom function: warning 1`] = `
exports[`"minify" option should work and allow to return errors and warnings from custom function: error 1`] = `
[
"Error: foo.css from Css Minimizer plugin
Error 1",
"Error: foo.css from Css Minimizer plugin
Error: Error 2",
]
`;

exports[`"minify" option should work and allow to return errors and warnings from custom function: warning 1`] = `
[
"Warning: foo.css from Css Minimizer plugin
Warning 1",
"Warning: foo.css from Css Minimizer plugin
Expand Down Expand Up @@ -471,7 +510,15 @@ exports[`"minify" option should work with "csso" minifier: warning 1`] = `[]`;

exports[`"minify" option should work with empty code: assets 1`] = `
{
"foo.css": "",
"foo.css": "body {
color: red;
}
a {
color: blue;
}

/*# sourceMappingURL=foo.css.map*/",
"foo.css.map": "{"version":3,"file":"foo.css","mappings":"AAAA;EACE,UAAU;AACZ;AACA;EACE,WAAW;AACb,C","sources":["webpack:///./foo.css"],"sourcesContent":["body {\\n color: red;\\n}\\na {\\n color: blue;\\n}"],"names":[],"sourceRoot":""}",
}
`;

Expand Down
35 changes: 35 additions & 0 deletions test/minify-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1293,4 +1293,39 @@ describe('"minify" option', () => {
expect(getErrors(stats)).toMatchSnapshot("error");
expect(getWarnings(stats)).toMatchSnapshot("warning");
});

it("should work and allow to return errors and warnings from custom function without code", async () => {
const compiler = getCompiler({
entry: {
foo: path.join(__dirname, "fixtures", "sourcemap", "foo.scss"),
},
module: {
rules: [
{
test: /.s?css$/i,
use: [
MiniCssExtractPlugin.loader,
{ loader: "css-loader", options: { sourceMap: true } },
{ loader: "sass-loader", options: { sourceMap: true } },
],
},
],
},
});

new CssMinimizerPlugin({
minify: async () => ({
warnings: ["Warning 1", new Error("Warning 2")],
errors: ["Error 1", new Error("Error 2")],
}),
}).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");
});
});