Skip to content

Commit 8b273d1

Browse files
fix: handle empty files (#295)
1 parent e1cdad5 commit 8b273d1

File tree

8 files changed

+53
-26
lines changed

8 files changed

+53
-26
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ class CssMinimizerPlugin {
622622
innerSourceMap,
623623
true,
624624
);
625-
} else if (item.code) {
625+
} else if (typeof item.code !== "undefined" && item.code !== null) {
626626
output.source = new RawSource(item.code);
627627
}
628628
}

src/utils.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,6 @@ const notSettled = Symbol("not-settled");
2020
* @returns {Promise<T[]>} A promise that fulfills to an array of the results
2121
*/
2222
function throttleAll(limit, tasks) {
23-
if (!Number.isInteger(limit) || limit < 1) {
24-
throw new TypeError(
25-
`Expected \`limit\` to be a finite number > 0, got \`${limit}\` (${typeof limit})`,
26-
);
27-
}
28-
29-
if (
30-
!Array.isArray(tasks) ||
31-
!tasks.every((task) => typeof task === "function")
32-
) {
33-
throw new TypeError(
34-
"Expected `tasks` to be a list of functions returning a promise",
35-
);
36-
}
37-
3823
return new Promise((resolve, reject) => {
3924
const result = Array.from({ length: tasks.length }).fill(notSettled);
4025
const entries = tasks.entries();

test/CssMinimizerPlugin.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,34 @@ describe("CssMinimizerPlugin", () => {
239239
).toMatchSnapshot();
240240
});
241241

242+
it("should work", async () => {
243+
const compiler = getCompiler({
244+
entry: path.join(__dirname, "fixtures", "entry.js"),
245+
});
246+
247+
new CssMinimizerPlugin().apply(compiler);
248+
249+
const stats = await compile(compiler);
250+
251+
expect(readAssets(compiler, stats, /\.css$/)).toMatchSnapshot("assets");
252+
expect(getErrors(stats)).toMatchSnapshot("errors");
253+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
254+
});
255+
256+
it("should work with empty files", async () => {
257+
const compiler = getCompiler({
258+
entry: path.join(__dirname, "fixtures", "empty.js"),
259+
});
260+
261+
new CssMinimizerPlugin().apply(compiler);
262+
263+
const stats = await compile(compiler);
264+
265+
expect(readAssets(compiler, stats, /\.css$/)).toMatchSnapshot("assets");
266+
expect(getErrors(stats)).toMatchSnapshot("errors");
267+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
268+
});
269+
242270
it("should build error", () => {
243271
const compiler = getCompiler({
244272
entry: {

test/__snapshots__/CssMinimizerPlugin.test.js.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,16 @@ exports[`CssMinimizerPlugin should work with child compilation: errors 1`] = `[]
232232

233233
exports[`CssMinimizerPlugin should work with child compilation: warnings 1`] = `[]`;
234234

235+
exports[`CssMinimizerPlugin should work with empty files: assets 1`] = `
236+
{
237+
"main.css": "",
238+
}
239+
`;
240+
241+
exports[`CssMinimizerPlugin should work with empty files: errors 1`] = `[]`;
242+
243+
exports[`CssMinimizerPlugin should work with empty files: warnings 1`] = `[]`;
244+
235245
exports[`CssMinimizerPlugin should work with source map and use memory cache when the "cache" option is "true" and the asset has been changed: assets 1`] = `
236246
{
237247
"foo.css": "body{color:red}a{color:blue}
@@ -396,6 +406,16 @@ exports[`CssMinimizerPlugin should work with warnings and use memory cache when
396406
]
397407
`;
398408

409+
exports[`CssMinimizerPlugin should work: assets 1`] = `
410+
{
411+
"main.css": "body{color:red}a{color:blue}",
412+
}
413+
`;
414+
415+
exports[`CssMinimizerPlugin should work: errors 1`] = `[]`;
416+
417+
exports[`CssMinimizerPlugin should work: warnings 1`] = `[]`;
418+
399419
exports[`CssMinimizerPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: assets 1`] = `
400420
{
401421
"one.css": ".minify {};",

test/__snapshots__/minify-option.test.js.snap

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,7 @@ exports[`"minify" option should work with "csso" minifier: warning 1`] = `[]`;
510510

511511
exports[`"minify" option should work with empty code: assets 1`] = `
512512
{
513-
"foo.css": "body {
514-
color: red;
515-
}
516-
a {
517-
color: blue;
518-
}
519-
520-
/*# sourceMappingURL=foo.css.map*/",
521-
"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":""}",
513+
"foo.css": "",
522514
}
523515
`;
524516

test/fixtures/empty.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* comment */

test/fixtures/empty.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./empty.css";

test/helpers/getCompiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import webpack from "webpack";
88
* @param {import("webpack").Configuration} config Webpack configuration
99
* @returns {import("webpack").Compiler} Webpack compiler
1010
*/
11-
export default function getCompiler(config) {
11+
export default function getCompiler(config = {}) {
1212
const compiler = webpack({
1313
mode: "development",
1414
devtool: config.devtool || false,

0 commit comments

Comments
 (0)