Skip to content

Commit

Permalink
fix: handle undefined and empty configuration export (#2930)
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 authored Sep 7, 2021
1 parent febeb84 commit 9b9040e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ class WebpackCLI {
}

// For babel/typescript
if (result.default) {
result = result.default;
if (result && typeof result === "object" && "default" in result) {
result = result.default || {};
}

return result;
return result || {};
}

loadJSONFile(pathToFile, handleError = true) {
Expand Down
16 changes: 16 additions & 0 deletions test/build/config/no-code/no-code.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
const { resolve } = require("path");
const { run } = require("../../../utils/test-utils");

describe("config flag with no code", () => {
it("should not throw error with no configuration or index file", async () => {
const { exitCode, stderr, stdout } = await run(__dirname, [
"-c",
resolve(__dirname, "webpack.config.js"),
]);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
});
});
1 change: 1 addition & 0 deletions test/build/config/no-code/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Peeves");
Empty file.
1 change: 1 addition & 0 deletions test/build/config/undefined-default/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Tom Riddle");
16 changes: 16 additions & 0 deletions test/build/config/undefined-default/undefined-default.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
const { resolve } = require("path");
const { run } = require("../../../utils/test-utils");

describe("config flag with undefined default export config file", () => {
it("should not throw error with no configuration or index file", async () => {
const { exitCode, stderr, stdout } = await run(__dirname, [
"-c",
resolve(__dirname, "webpack.config.js"),
]);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
});
});
1 change: 1 addition & 0 deletions test/build/config/undefined-default/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.default = undefined;
1 change: 1 addition & 0 deletions test/build/config/undefined/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Percy Weasley");
16 changes: 16 additions & 0 deletions test/build/config/undefined/undefined.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
const { resolve } = require("path");
const { run } = require("../../../utils/test-utils");

describe("config flag with undefined export config file", () => {
it("should not throw error with no configuration or index file", async () => {
const { exitCode, stderr, stdout } = await run(__dirname, [
"-c",
resolve(__dirname, "webpack.config.js"),
]);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
});
});
1 change: 1 addition & 0 deletions test/build/config/undefined/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = undefined;

0 comments on commit 9b9040e

Please sign in to comment.