Skip to content

Commit

Permalink
feat: allow returning errors from custom minimize function (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 4, 2021
1 parent b736099 commit c9a11b2
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 101 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = {
new CssMinimizerPlugin(),
],
},
plugins: [new MiniCssExtractPlugin()],
};
```

Expand Down Expand Up @@ -223,7 +224,7 @@ Possible options:
- CssMinimizerPlugin.cssnanoMinify
- CssMinimizerPlugin.cssoMinify
- CssMinimizerPlugin.cleanCssMinify
- `async (data, inputMap, minimizerOptions) => {return {code: "a{color: red}", map: "...", warnings: []}}`
- `async (data, inputMap, minimizerOptions) => {return {code: "a{color: red}", map: "...", warnings: [], errors: []}}`

> ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**.
Expand Down Expand Up @@ -278,6 +279,7 @@ module.exports = {
code: `a{color: red}`,
map: `{"version": "3", ...}`,
warnings: [],
errors: [],
};
},
],
Expand Down Expand Up @@ -450,6 +452,7 @@ module.exports = {
optimization: {
minimizer: [new CssMinimizerPlugin()],
},
plugins: [new MiniCssExtractPlugin()],
};
```

Expand Down
147 changes: 92 additions & 55 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,67 +53,19 @@ class CssMinimizerPlugin {
);
}

static buildError(error, name, sourceMap, requestShortener) {
let builtError;

if (error.line) {
const original =
sourceMap &&
sourceMap.originalPositionFor({
line: error.line,
column: error.column,
});

if (original && original.source && requestShortener) {
builtError = new Error(
`${name} from Css Minimizer Webpack Plugin\n${
error.message
} [${requestShortener.shorten(original.source)}:${original.line},${
original.column
}][${name}:${error.line},${error.column}]${
error.stack
? `\n${error.stack.split("\n").slice(1).join("\n")}`
: ""
}`
);
builtError.file = name;

return builtError;
}

builtError = new Error(
`${name} from Css Minimizer \n${error.message} [${name}:${error.line},${
error.column
}]${
error.stack ? `\n${error.stack.split("\n").slice(1).join("\n")}` : ""
}`
);
builtError.file = name;

return builtError;
}

if (error.stack) {
builtError = new Error(`${name} from Css Minimizer\n${error.stack}`);
builtError.file = name;

return builtError;
}

builtError = new Error(`${name} from Css Minimizer\n${error.message}`);
builtError.file = name;

return builtError;
}

static buildWarning(
warning,
file,
sourceMap,
requestShortener,
warningsFilter
) {
let warningMessage = warning;
let warningMessage =
typeof warning === "string"
? warning
: `${warning.plugin ? `[${warning.plugin}] ` : ""}${
warning.text || warning.message
}`;
let locationMessage = "";
let source;

Expand Down Expand Up @@ -149,7 +101,7 @@ class CssMinimizerPlugin {
}

const builtWarning = new Error(
`Css Minimizer Plugin: ${warningMessage}${
`${file} from Css Minimizer Plugin\n${warningMessage}${
locationMessage ? ` ${locationMessage}` : ""
}`
);
Expand All @@ -161,6 +113,70 @@ class CssMinimizerPlugin {
return builtWarning;
}

static buildError(error, file, sourceMap, requestShortener) {
let builtError;

if (typeof error === "string") {
builtError = new Error(`${file} from Css Minimizer Plugin\n${error}`);
builtError.file = file;

return builtError;
}

if (error.line) {
const original =
sourceMap &&
sourceMap.originalPositionFor({
line: error.line,
column: error.column,
});

if (original && original.source && requestShortener) {
builtError = new Error(
`${file} from Css Minimizer Plugin\n${
error.message
} [${requestShortener.shorten(original.source)}:${original.line},${
original.column
}][${file}:${error.line},${error.column}]${
error.stack
? `\n${error.stack.split("\n").slice(1).join("\n")}`
: ""
}`
);
builtError.file = file;

return builtError;
}

builtError = new Error(
`${file} from Css Minimizer Plugin\n${error.message} [${file}:${
error.line
},${error.column}]${
error.stack ? `\n${error.stack.split("\n").slice(1).join("\n")}` : ""
}`
);
builtError.file = file;

return builtError;
}

if (error.stack) {
builtError = new Error(
`${file} from Css Minimizer Plugin\n${error.stack}`
);
builtError.file = file;

return builtError;
}

builtError = new Error(
`${file} from Css Minimizer Plugin\n${error.message}`
);
builtError.file = file;

return builtError;
}

static getAvailableNumberOfCores(parallel) {
// In some cases cpus() returns undefined
// https://github.com/nodejs/node/issues/19022
Expand Down Expand Up @@ -350,6 +366,27 @@ class CssMinimizerPlugin {
}
}

if (result.errors && result.errors.length > 0) {
const hasSourceMap =
inputSourceMap &&
CssMinimizerPlugin.isSourceMap(inputSourceMap);

for (const error of result.errors) {
output.warnings.push(
CssMinimizerPlugin.buildError(
error,
name,
hasSourceMap
? new SourceMapConsumer(inputSourceMap)
: // eslint-disable-next-line no-undefined
undefined,
// eslint-disable-next-line no-undefined
hasSourceMap ? compilation.requestShortener : undefined
)
);
}
}

if (result.warnings && result.warnings.length > 0) {
const hasSourceMap =
inputSourceMap &&
Expand Down
8 changes: 2 additions & 6 deletions src/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ const minify = async (options) => {
}

if (minifyResult.errors) {
result.errors = result.errors.concat(
minifyResult.errors.map((error) => error.toString())
);
result.errors = result.errors.concat(minifyResult.errors);
}

if (minifyResult.warnings) {
result.warnings = result.warnings.concat(
minifyResult.warnings.map((warning) => warning.toString())
);
result.warnings = result.warnings.concat(minifyResult.warnings);
}

result.outputs.push({ code: minifyResult.code, map: minifyResult.map });
Expand Down
76 changes: 52 additions & 24 deletions test/__snapshots__/CssMinimizerPlugin.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CssMinimizerPlugin buildError method 1`] = `
[Error: test.css from Css Minimizer
[Error: test.css from Css Minimizer Plugin
Message]
`;

exports[`CssMinimizerPlugin buildError method 2`] = `
[Error: test.css from Css Minimizer
[Error: test.css from Css Minimizer Plugin
Message [test.css:1,1]]
`;

exports[`CssMinimizerPlugin buildError method 3`] = `
[Error: test.css from Css Minimizer Webpack Plugin
[Error: test.css from Css Minimizer Plugin
Message [http://example.com/www/js/one.css:1,1][test.css:1,1]]
`;

exports[`CssMinimizerPlugin buildError method 4`] = `
[Error: test.css from Css Minimizer
[Error: test.css from Css Minimizer Plugin
Stack]
`;

exports[`CssMinimizerPlugin buildWarning method 1`] = `[Warning: Css Minimizer Plugin: Warning test.css:1:1]`;
exports[`CssMinimizerPlugin buildWarning method 1`] = `
[Warning: undefined from Css Minimizer Plugin
Warning test.css:1:1]
`;

exports[`CssMinimizerPlugin buildWarning method 2`] = `[Warning: Css Minimizer Plugin: Warning test.css:1:1]`;
exports[`CssMinimizerPlugin buildWarning method 2`] = `
[Warning: test.css from Css Minimizer Plugin
Warning test.css:1:1]
`;

exports[`CssMinimizerPlugin buildWarning method 3`] = `[Warning: Css Minimizer Plugin: Warning test.css:1:1]`;
exports[`CssMinimizerPlugin buildWarning method 3`] = `
[Warning: test.css from Css Minimizer Plugin
Warning test.css:1:1]
`;

exports[`CssMinimizerPlugin buildWarning method 4`] = `[Warning: Css Minimizer Plugin: Warning http://example.com/www/js/one.css:1:1]`;
exports[`CssMinimizerPlugin buildWarning method 4`] = `
[Warning: test.css from Css Minimizer Plugin
Warning http://example.com/www/js/one.css:1:1]
`;

exports[`CssMinimizerPlugin buildWarning method 5`] = `[Warning: Css Minimizer Plugin: Warning http://example.com/www/js/one.css:1:1]`;
exports[`CssMinimizerPlugin buildWarning method 5`] = `
[Warning: test.css from Css Minimizer Plugin
Warning http://example.com/www/js/one.css:1:1]
`;

exports[`CssMinimizerPlugin buildWarning method 6`] = `null`;

exports[`CssMinimizerPlugin should build error: error 1`] = `
Array [
"Error: error.css from Css Minimizer
"Error: error.css from Css Minimizer Plugin
/error.css:1:1: Unknown word [error.css:1,1]",
]
`;
Expand All @@ -45,7 +60,8 @@ exports[`CssMinimizerPlugin should build warning: error 1`] = `Array []`;

exports[`CssMinimizerPlugin should build warning: warning 1`] = `
Array [
"Warning: Css Minimizer Plugin: warning-plugin:: Warning webpack://./test/foo.css:2:2",
"Warning: foo.css from Css Minimizer Plugin
[warning-plugin] Warning",
]
`;

Expand All @@ -71,7 +87,7 @@ exports[`CssMinimizerPlugin should run plugin against assets added later by plug

exports[`CssMinimizerPlugin should throw error from postcss: error 1`] = `
Array [
"Error: foo.css from Css Minimizer Webpack Plugin
"Error: foo.css from Css Minimizer Plugin
error-plugin: /foo.css:2:3: Postcss error [webpack://./test/foo.css:2,2][foo.css:2,3]",
]
`;
Expand Down Expand Up @@ -297,9 +313,12 @@ a {

exports[`CssMinimizerPlugin should work with warnings and use memory cache when the "cache" option is "true" and the asset has been changed: errors 1`] = `
Array [
"Warning: Css Minimizer Plugin: warning-plugin: Warning from foo.css",
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style-2.css",
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style.css",
"Warning: foo.css from Css Minimizer Plugin
[warning-plugin] Warning from foo.css",
"Warning: style-2.css from Css Minimizer Plugin
[warning-plugin] Warning from style-2.css",
"Warning: style.css from Css Minimizer Plugin
[warning-plugin] Warning from style.css",
]
`;

Expand All @@ -309,9 +328,12 @@ exports[`CssMinimizerPlugin should work with warnings and use memory cache when

exports[`CssMinimizerPlugin should work with warnings and use memory cache when the "cache" option is "true" and the asset has been changed: warnings 2`] = `
Array [
"Warning: Css Minimizer Plugin: warning-plugin: Warning from foo.css",
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style-2.css",
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style.css",
"Warning: foo.css from Css Minimizer Plugin
[warning-plugin] Warning from foo.css",
"Warning: style-2.css from Css Minimizer Plugin
[warning-plugin] Warning from style-2.css",
"Warning: style.css from Css Minimizer Plugin
[warning-plugin] Warning from style.css",
]
`;

Expand Down Expand Up @@ -345,9 +367,12 @@ a {

exports[`CssMinimizerPlugin should work with warnings and use memory cache when the "cache" option is "true": errors 1`] = `
Array [
"Warning: Css Minimizer Plugin: warning-plugin: Warning from foo.css",
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style-2.css",
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style.css",
"Warning: foo.css from Css Minimizer Plugin
[warning-plugin] Warning from foo.css",
"Warning: style-2.css from Css Minimizer Plugin
[warning-plugin] Warning from style-2.css",
"Warning: style.css from Css Minimizer Plugin
[warning-plugin] Warning from style.css",
]
`;

Expand All @@ -357,9 +382,12 @@ exports[`CssMinimizerPlugin should work with warnings and use memory cache when

exports[`CssMinimizerPlugin should work with warnings and use memory cache when the "cache" option is "true": warnings 2`] = `
Array [
"Warning: Css Minimizer Plugin: warning-plugin: Warning from foo.css",
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style-2.css",
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style.css",
"Warning: foo.css from Css Minimizer Plugin
[warning-plugin] Warning from foo.css",
"Warning: style-2.css from Css Minimizer Plugin
[warning-plugin] Warning from style-2.css",
"Warning: style.css from Css Minimizer Plugin
[warning-plugin] Warning from style.css",
]
`;

Expand Down

0 comments on commit c9a11b2

Please sign in to comment.