Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle undefined configuration export #2930

Merged
merged 9 commits into from Sep 7, 2021
6 changes: 3 additions & 3 deletions packages/webpack-cli/lib/webpack-cli.js
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) {
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
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
@@ -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
@@ -0,0 +1 @@
console.log("Peeves");
Empty file.
1 change: 1 addition & 0 deletions test/build/config/undefined-default/src/index.js
@@ -0,0 +1 @@
console.log("Tom Riddle");
16 changes: 16 additions & 0 deletions test/build/config/undefined-default/undefined-default.test.js
@@ -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
@@ -0,0 +1 @@
module.exports.default = undefined;
1 change: 1 addition & 0 deletions test/build/config/undefined/src/index.js
@@ -0,0 +1 @@
console.log("Percy Weasley");
16 changes: 16 additions & 0 deletions test/build/config/undefined/undefined.test.js
@@ -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
@@ -0,0 +1 @@
module.exports = undefined;
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved