diff --git a/package-lock.json b/package-lock.json index ba787a0..3bd55a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4110,9 +4110,9 @@ "license": "MIT" }, "node_modules/@jest/reporters/node_modules/glob": { - "version": "10.4.5", - "resolved": "https://registry.npmjs.org/glob/-/glob-10.4.5.tgz", - "integrity": "sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg==", + "version": "10.5.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-10.5.0.tgz", + "integrity": "sha512-DfXN8DfhJ7NH3Oe7cFmu3NCu1wKbkReJ8TorzSAFbSKrlNaQSKfIzqYqVY8zlbs2NLBbWpRiU52GX2PbaBVNkg==", "dev": true, "license": "ISC", "dependencies": { @@ -13960,9 +13960,9 @@ "license": "MIT" }, "node_modules/jest-config/node_modules/glob": { - "version": "10.4.5", - "resolved": "https://registry.npmjs.org/glob/-/glob-10.4.5.tgz", - "integrity": "sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg==", + "version": "10.5.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-10.5.0.tgz", + "integrity": "sha512-DfXN8DfhJ7NH3Oe7cFmu3NCu1wKbkReJ8TorzSAFbSKrlNaQSKfIzqYqVY8zlbs2NLBbWpRiU52GX2PbaBVNkg==", "dev": true, "license": "ISC", "dependencies": { @@ -14693,9 +14693,9 @@ "license": "MIT" }, "node_modules/jest-runtime/node_modules/glob": { - "version": "10.4.5", - "resolved": "https://registry.npmjs.org/glob/-/glob-10.4.5.tgz", - "integrity": "sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg==", + "version": "10.5.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-10.5.0.tgz", + "integrity": "sha512-DfXN8DfhJ7NH3Oe7cFmu3NCu1wKbkReJ8TorzSAFbSKrlNaQSKfIzqYqVY8zlbs2NLBbWpRiU52GX2PbaBVNkg==", "dev": true, "license": "ISC", "dependencies": { diff --git a/src/index.js b/src/index.js index 0583051..7f544e4 100644 --- a/src/index.js +++ b/src/index.js @@ -622,7 +622,7 @@ class CssMinimizerPlugin { innerSourceMap, true, ); - } else { + } else if (item.code) { output.source = new RawSource(item.code); } } @@ -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, @@ -691,6 +691,10 @@ class CssMinimizerPlugin { } } + if (!output.source) { + return; + } + const newInfo = { minimized: true }; const { source } = output; diff --git a/src/minify.js b/src/minify.js index d97de87..77d8153 100644 --- a/src/minify.js +++ b/src/minify.js @@ -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", + ), ); } diff --git a/test/__snapshots__/minify-option.test.js.snap b/test/__snapshots__/minify-option.test.js.snap index c63c36b..59cca25 100644 --- a/test/__snapshots__/minify-option.test.js.snap +++ b/test/__snapshots__/minify-option.test.js.snap @@ -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 @@ -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":""}", } `; diff --git a/test/minify-option.test.js b/test/minify-option.test.js index d31e436..dca4f74 100644 --- a/test/minify-option.test.js +++ b/test/minify-option.test.js @@ -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"); + }); });