Skip to content

Commit c9a11b2

Browse files
feat: allow returning errors from custom minimize function (#121)
1 parent b736099 commit c9a11b2

9 files changed

+236
-101
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module.exports = {
5050
new CssMinimizerPlugin(),
5151
],
5252
},
53+
plugins: [new MiniCssExtractPlugin()],
5354
};
5455
```
5556

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

228229
> ⚠️ **Always use `require` inside `minify` function when `parallel` option enabled**.
229230
@@ -278,6 +279,7 @@ module.exports = {
278279
code: `a{color: red}`,
279280
map: `{"version": "3", ...}`,
280281
warnings: [],
282+
errors: [],
281283
};
282284
},
283285
],
@@ -450,6 +452,7 @@ module.exports = {
450452
optimization: {
451453
minimizer: [new CssMinimizerPlugin()],
452454
},
455+
plugins: [new MiniCssExtractPlugin()],
453456
};
454457
```
455458

src/index.js

+92-55
Original file line numberDiff line numberDiff line change
@@ -53,67 +53,19 @@ class CssMinimizerPlugin {
5353
);
5454
}
5555

56-
static buildError(error, name, sourceMap, requestShortener) {
57-
let builtError;
58-
59-
if (error.line) {
60-
const original =
61-
sourceMap &&
62-
sourceMap.originalPositionFor({
63-
line: error.line,
64-
column: error.column,
65-
});
66-
67-
if (original && original.source && requestShortener) {
68-
builtError = new Error(
69-
`${name} from Css Minimizer Webpack Plugin\n${
70-
error.message
71-
} [${requestShortener.shorten(original.source)}:${original.line},${
72-
original.column
73-
}][${name}:${error.line},${error.column}]${
74-
error.stack
75-
? `\n${error.stack.split("\n").slice(1).join("\n")}`
76-
: ""
77-
}`
78-
);
79-
builtError.file = name;
80-
81-
return builtError;
82-
}
83-
84-
builtError = new Error(
85-
`${name} from Css Minimizer \n${error.message} [${name}:${error.line},${
86-
error.column
87-
}]${
88-
error.stack ? `\n${error.stack.split("\n").slice(1).join("\n")}` : ""
89-
}`
90-
);
91-
builtError.file = name;
92-
93-
return builtError;
94-
}
95-
96-
if (error.stack) {
97-
builtError = new Error(`${name} from Css Minimizer\n${error.stack}`);
98-
builtError.file = name;
99-
100-
return builtError;
101-
}
102-
103-
builtError = new Error(`${name} from Css Minimizer\n${error.message}`);
104-
builtError.file = name;
105-
106-
return builtError;
107-
}
108-
10956
static buildWarning(
11057
warning,
11158
file,
11259
sourceMap,
11360
requestShortener,
11461
warningsFilter
11562
) {
116-
let warningMessage = warning;
63+
let warningMessage =
64+
typeof warning === "string"
65+
? warning
66+
: `${warning.plugin ? `[${warning.plugin}] ` : ""}${
67+
warning.text || warning.message
68+
}`;
11769
let locationMessage = "";
11870
let source;
11971

@@ -149,7 +101,7 @@ class CssMinimizerPlugin {
149101
}
150102

151103
const builtWarning = new Error(
152-
`Css Minimizer Plugin: ${warningMessage}${
104+
`${file} from Css Minimizer Plugin\n${warningMessage}${
153105
locationMessage ? ` ${locationMessage}` : ""
154106
}`
155107
);
@@ -161,6 +113,70 @@ class CssMinimizerPlugin {
161113
return builtWarning;
162114
}
163115

116+
static buildError(error, file, sourceMap, requestShortener) {
117+
let builtError;
118+
119+
if (typeof error === "string") {
120+
builtError = new Error(`${file} from Css Minimizer Plugin\n${error}`);
121+
builtError.file = file;
122+
123+
return builtError;
124+
}
125+
126+
if (error.line) {
127+
const original =
128+
sourceMap &&
129+
sourceMap.originalPositionFor({
130+
line: error.line,
131+
column: error.column,
132+
});
133+
134+
if (original && original.source && requestShortener) {
135+
builtError = new Error(
136+
`${file} from Css Minimizer Plugin\n${
137+
error.message
138+
} [${requestShortener.shorten(original.source)}:${original.line},${
139+
original.column
140+
}][${file}:${error.line},${error.column}]${
141+
error.stack
142+
? `\n${error.stack.split("\n").slice(1).join("\n")}`
143+
: ""
144+
}`
145+
);
146+
builtError.file = file;
147+
148+
return builtError;
149+
}
150+
151+
builtError = new Error(
152+
`${file} from Css Minimizer Plugin\n${error.message} [${file}:${
153+
error.line
154+
},${error.column}]${
155+
error.stack ? `\n${error.stack.split("\n").slice(1).join("\n")}` : ""
156+
}`
157+
);
158+
builtError.file = file;
159+
160+
return builtError;
161+
}
162+
163+
if (error.stack) {
164+
builtError = new Error(
165+
`${file} from Css Minimizer Plugin\n${error.stack}`
166+
);
167+
builtError.file = file;
168+
169+
return builtError;
170+
}
171+
172+
builtError = new Error(
173+
`${file} from Css Minimizer Plugin\n${error.message}`
174+
);
175+
builtError.file = file;
176+
177+
return builtError;
178+
}
179+
164180
static getAvailableNumberOfCores(parallel) {
165181
// In some cases cpus() returns undefined
166182
// https://github.com/nodejs/node/issues/19022
@@ -350,6 +366,27 @@ class CssMinimizerPlugin {
350366
}
351367
}
352368

369+
if (result.errors && result.errors.length > 0) {
370+
const hasSourceMap =
371+
inputSourceMap &&
372+
CssMinimizerPlugin.isSourceMap(inputSourceMap);
373+
374+
for (const error of result.errors) {
375+
output.warnings.push(
376+
CssMinimizerPlugin.buildError(
377+
error,
378+
name,
379+
hasSourceMap
380+
? new SourceMapConsumer(inputSourceMap)
381+
: // eslint-disable-next-line no-undefined
382+
undefined,
383+
// eslint-disable-next-line no-undefined
384+
hasSourceMap ? compilation.requestShortener : undefined
385+
)
386+
);
387+
}
388+
}
389+
353390
if (result.warnings && result.warnings.length > 0) {
354391
const hasSourceMap =
355392
inputSourceMap &&

src/minify.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,11 @@ const minify = async (options) => {
3232
}
3333

3434
if (minifyResult.errors) {
35-
result.errors = result.errors.concat(
36-
minifyResult.errors.map((error) => error.toString())
37-
);
35+
result.errors = result.errors.concat(minifyResult.errors);
3836
}
3937

4038
if (minifyResult.warnings) {
41-
result.warnings = result.warnings.concat(
42-
minifyResult.warnings.map((warning) => warning.toString())
43-
);
39+
result.warnings = result.warnings.concat(minifyResult.warnings);
4440
}
4541

4642
result.outputs.push({ code: minifyResult.code, map: minifyResult.map });

test/__snapshots__/CssMinimizerPlugin.test.js.snap

+52-24
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,55 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`CssMinimizerPlugin buildError method 1`] = `
4-
[Error: test.css from Css Minimizer
4+
[Error: test.css from Css Minimizer Plugin
55
Message]
66
`;
77

88
exports[`CssMinimizerPlugin buildError method 2`] = `
9-
[Error: test.css from Css Minimizer
9+
[Error: test.css from Css Minimizer Plugin
1010
Message [test.css:1,1]]
1111
`;
1212

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

1818
exports[`CssMinimizerPlugin buildError method 4`] = `
19-
[Error: test.css from Css Minimizer
19+
[Error: test.css from Css Minimizer Plugin
2020
Stack]
2121
`;
2222

23-
exports[`CssMinimizerPlugin buildWarning method 1`] = `[Warning: Css Minimizer Plugin: Warning test.css:1:1]`;
23+
exports[`CssMinimizerPlugin buildWarning method 1`] = `
24+
[Warning: undefined from Css Minimizer Plugin
25+
Warning test.css:1:1]
26+
`;
2427

25-
exports[`CssMinimizerPlugin buildWarning method 2`] = `[Warning: Css Minimizer Plugin: Warning test.css:1:1]`;
28+
exports[`CssMinimizerPlugin buildWarning method 2`] = `
29+
[Warning: test.css from Css Minimizer Plugin
30+
Warning test.css:1:1]
31+
`;
2632

27-
exports[`CssMinimizerPlugin buildWarning method 3`] = `[Warning: Css Minimizer Plugin: Warning test.css:1:1]`;
33+
exports[`CssMinimizerPlugin buildWarning method 3`] = `
34+
[Warning: test.css from Css Minimizer Plugin
35+
Warning test.css:1:1]
36+
`;
2837

29-
exports[`CssMinimizerPlugin buildWarning method 4`] = `[Warning: Css Minimizer Plugin: Warning http://example.com/www/js/one.css:1:1]`;
38+
exports[`CssMinimizerPlugin buildWarning method 4`] = `
39+
[Warning: test.css from Css Minimizer Plugin
40+
Warning http://example.com/www/js/one.css:1:1]
41+
`;
3042

31-
exports[`CssMinimizerPlugin buildWarning method 5`] = `[Warning: Css Minimizer Plugin: Warning http://example.com/www/js/one.css:1:1]`;
43+
exports[`CssMinimizerPlugin buildWarning method 5`] = `
44+
[Warning: test.css from Css Minimizer Plugin
45+
Warning http://example.com/www/js/one.css:1:1]
46+
`;
3247

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

3550
exports[`CssMinimizerPlugin should build error: error 1`] = `
3651
Array [
37-
"Error: error.css from Css Minimizer
52+
"Error: error.css from Css Minimizer Plugin
3853
/error.css:1:1: Unknown word [error.css:1,1]",
3954
]
4055
`;
@@ -45,7 +60,8 @@ exports[`CssMinimizerPlugin should build warning: error 1`] = `Array []`;
4560

4661
exports[`CssMinimizerPlugin should build warning: warning 1`] = `
4762
Array [
48-
"Warning: Css Minimizer Plugin: warning-plugin:: Warning webpack://./test/foo.css:2:2",
63+
"Warning: foo.css from Css Minimizer Plugin
64+
[warning-plugin] Warning",
4965
]
5066
`;
5167

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

7288
exports[`CssMinimizerPlugin should throw error from postcss: error 1`] = `
7389
Array [
74-
"Error: foo.css from Css Minimizer Webpack Plugin
90+
"Error: foo.css from Css Minimizer Plugin
7591
error-plugin: /foo.css:2:3: Postcss error [webpack://./test/foo.css:2,2][foo.css:2,3]",
7692
]
7793
`;
@@ -297,9 +313,12 @@ a {
297313

298314
exports[`CssMinimizerPlugin should work with warnings and use memory cache when the "cache" option is "true" and the asset has been changed: errors 1`] = `
299315
Array [
300-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from foo.css",
301-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style-2.css",
302-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style.css",
316+
"Warning: foo.css from Css Minimizer Plugin
317+
[warning-plugin] Warning from foo.css",
318+
"Warning: style-2.css from Css Minimizer Plugin
319+
[warning-plugin] Warning from style-2.css",
320+
"Warning: style.css from Css Minimizer Plugin
321+
[warning-plugin] Warning from style.css",
303322
]
304323
`;
305324

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

310329
exports[`CssMinimizerPlugin should work with warnings and use memory cache when the "cache" option is "true" and the asset has been changed: warnings 2`] = `
311330
Array [
312-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from foo.css",
313-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style-2.css",
314-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style.css",
331+
"Warning: foo.css from Css Minimizer Plugin
332+
[warning-plugin] Warning from foo.css",
333+
"Warning: style-2.css from Css Minimizer Plugin
334+
[warning-plugin] Warning from style-2.css",
335+
"Warning: style.css from Css Minimizer Plugin
336+
[warning-plugin] Warning from style.css",
315337
]
316338
`;
317339

@@ -345,9 +367,12 @@ a {
345367

346368
exports[`CssMinimizerPlugin should work with warnings and use memory cache when the "cache" option is "true": errors 1`] = `
347369
Array [
348-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from foo.css",
349-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style-2.css",
350-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style.css",
370+
"Warning: foo.css from Css Minimizer Plugin
371+
[warning-plugin] Warning from foo.css",
372+
"Warning: style-2.css from Css Minimizer Plugin
373+
[warning-plugin] Warning from style-2.css",
374+
"Warning: style.css from Css Minimizer Plugin
375+
[warning-plugin] Warning from style.css",
351376
]
352377
`;
353378

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

358383
exports[`CssMinimizerPlugin should work with warnings and use memory cache when the "cache" option is "true": warnings 2`] = `
359384
Array [
360-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from foo.css",
361-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style-2.css",
362-
"Warning: Css Minimizer Plugin: warning-plugin: Warning from style.css",
385+
"Warning: foo.css from Css Minimizer Plugin
386+
[warning-plugin] Warning from foo.css",
387+
"Warning: style-2.css from Css Minimizer Plugin
388+
[warning-plugin] Warning from style-2.css",
389+
"Warning: style.css from Css Minimizer Plugin
390+
[warning-plugin] Warning from style.css",
363391
]
364392
`;
365393

0 commit comments

Comments
 (0)