Skip to content

Commit 9b9040e

Browse files
authored
fix: handle undefined and empty configuration export (#2930)
1 parent febeb84 commit 9b9040e

File tree

10 files changed

+56
-3
lines changed

10 files changed

+56
-3
lines changed

packages/webpack-cli/lib/webpack-cli.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ class WebpackCLI {
6767
}
6868

6969
// For babel/typescript
70-
if (result.default) {
71-
result = result.default;
70+
if (result && typeof result === "object" && "default" in result) {
71+
result = result.default || {};
7272
}
7373

74-
return result;
74+
return result || {};
7575
}
7676

7777
loadJSONFile(pathToFile, handleError = true) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
const { resolve } = require("path");
3+
const { run } = require("../../../utils/test-utils");
4+
5+
describe("config flag with no code", () => {
6+
it("should not throw error with no configuration or index file", async () => {
7+
const { exitCode, stderr, stdout } = await run(__dirname, [
8+
"-c",
9+
resolve(__dirname, "webpack.config.js"),
10+
]);
11+
12+
expect(exitCode).toBe(0);
13+
expect(stderr).toBeFalsy();
14+
expect(stdout).toBeTruthy();
15+
});
16+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Peeves");

test/build/config/no-code/webpack.config.js

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Tom Riddle");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
const { resolve } = require("path");
3+
const { run } = require("../../../utils/test-utils");
4+
5+
describe("config flag with undefined default export config file", () => {
6+
it("should not throw error with no configuration or index file", async () => {
7+
const { exitCode, stderr, stdout } = await run(__dirname, [
8+
"-c",
9+
resolve(__dirname, "webpack.config.js"),
10+
]);
11+
12+
expect(exitCode).toBe(0);
13+
expect(stderr).toBeFalsy();
14+
expect(stdout).toBeTruthy();
15+
});
16+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.default = undefined;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Percy Weasley");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
const { resolve } = require("path");
3+
const { run } = require("../../../utils/test-utils");
4+
5+
describe("config flag with undefined export config file", () => {
6+
it("should not throw error with no configuration or index file", async () => {
7+
const { exitCode, stderr, stdout } = await run(__dirname, [
8+
"-c",
9+
resolve(__dirname, "webpack.config.js"),
10+
]);
11+
12+
expect(exitCode).toBe(0);
13+
expect(stderr).toBeFalsy();
14+
expect(stdout).toBeTruthy();
15+
});
16+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = undefined;

0 commit comments

Comments
 (0)