Skip to content

Commit c737383

Browse files
shuta13anshumanvsnitin315
authored andcommitted
fix(webpack-cli): add an option for preventing interpret (#3329)
* fix(webpack-cli): add an option for preventing interpret * fix: define the option for built-in flags * docs: add descriptions of the option * refactor: rename `--config-registered` to `--disable-interpret` * fix: change conditional statement * refactor: standalone test * test: use `--disable-interpret` without transpilation * docs: fix the description Co-authored-by: Anshuman Verma <anshu.av97@gmail.com> * refactor: built-in options type Co-authored-by: Nitin Kumar <snitin315@gmail.com> * test: re-update snapshots * fix: add double quote Co-authored-by: Nitin Kumar <snitin315@gmail.com> * test: update snapshots for webpack4 * chore: remove `--require` from `test:coverage` * test: update snapshots Co-authored-by: Anshuman Verma <anshu.av97@gmail.com> Co-authored-by: Nitin Kumar <snitin315@gmail.com>
1 parent 4cbb354 commit c737383

13 files changed

+165
-3
lines changed

OPTIONS.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Options:
88
-c, --config <value...> Provide path to a webpack configuration file e.g. ./webpack.config.js.
99
--config-name <value...> Name of the configuration to use.
1010
-m, --merge Merge two or more configurations using 'webpack-merge'.
11+
--disable-interpret Disable interpret for loading the config file.
1112
--env <value...> Environment passed to the configuration when it is a function.
1213
--node-env <value> Sets process.env.NODE_ENV to the specified value.
1314
-h, --hot [value] Enables Hot Module Replacement

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"pretest": "yarn build && yarn lint && yarn prepsuite",
4242
"test": "jest --reporters=default",
4343
"test:smoketests": "nyc node smoketests",
44-
"test:coverage": "nyc --no-clean --require ts-node/register jest",
44+
"test:coverage": "nyc --no-clean jest",
4545
"test:cli": "jest test --reporters=default",
4646
"test:packages": "jest packages/ --reporters=default",
4747
"test:ci": "yarn test:cli && yarn test:packages",

packages/webpack-cli/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ type WebpackDevServerOptions = DevServerConfig &
175175
merge?: boolean;
176176
config: string[];
177177
configName?: string[];
178+
disableInterpret?: boolean;
178179
argv: Argv;
179180
};
180181

packages/webpack-cli/src/webpack-cli.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ class WebpackCLI implements IWebpackCLI {
697697
"config",
698698
"config-name",
699699
"merge",
700+
"disable-interpret",
700701
"env",
701702
"mode",
702703
"watch",
@@ -746,6 +747,16 @@ class WebpackCLI implements IWebpackCLI {
746747
],
747748
description: "Merge two or more configurations using 'webpack-merge'.",
748749
},
750+
{
751+
name: "disable-interpret",
752+
configs: [
753+
{
754+
type: "enum",
755+
values: [true],
756+
},
757+
],
758+
description: "Disable interpret a config file.",
759+
},
749760
// Complex configs
750761
{
751762
name: "env",
@@ -1775,12 +1786,15 @@ class WebpackCLI implements IWebpackCLI {
17751786
}
17761787

17771788
async loadConfig(options: Partial<WebpackDevServerOptions>) {
1789+
const disableInterpret =
1790+
typeof options.disableInterpret !== "undefined" && options.disableInterpret;
1791+
17781792
const interpret = require("interpret");
17791793
const loadConfigByPath = async (configPath: string, argv: Argv = {}) => {
17801794
const ext = path.extname(configPath);
17811795
const interpreted = Object.keys(interpret.jsVariants).find((variant) => variant === ext);
17821796

1783-
if (interpreted) {
1797+
if (interpreted && !disableInterpret) {
17841798
const rechoir: Rechoir = require("rechoir");
17851799

17861800
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { run } = require("../../../utils/test-utils");
2+
const { existsSync, unlinkSync } = require("fs");
3+
const { resolve } = require("path");
4+
5+
// eslint-disable-next-line node/no-unpublished-require
6+
const execa = require("execa");
7+
const { sync: spawnSync } = execa;
8+
9+
describe("webpack cli", () => {
10+
it('should work with the "disable-interpret" option from flags', async () => {
11+
const configFileName = "webpack.config";
12+
const configFilePath = resolve(__dirname, `${configFileName}.ts`);
13+
const buildScripts = spawnSync("yarn", ["tsc", configFilePath]);
14+
expect(buildScripts.stdout).toBeTruthy();
15+
16+
const { exitCode, stderr, stdout } = await run(__dirname, ["--disable-interpret"]);
17+
unlinkSync(resolve(__dirname, `${configFileName}.js`));
18+
19+
expect(stderr).toBeFalsy();
20+
expect(stdout).toBeTruthy();
21+
expect(exitCode).toBe(0);
22+
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy();
23+
});
24+
25+
it("should log error without transpilation", async () => {
26+
const { exitCode, stderr, stdout } = await run(__dirname, ["--disable-interpret"]);
27+
28+
expect(exitCode).toBe(2);
29+
expect(stderr).toContain(`Failed to load '${resolve(__dirname, "webpack.config.ts")}' config`);
30+
expect(stdout).toBeFalsy();
31+
});
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Main typescript file");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-disable node/no-unsupported-features/es-syntax */
2+
/** eslint-disable **/
3+
import * as path from "path";
4+
5+
const config = {
6+
mode: "production",
7+
entry: "./main.ts",
8+
output: {
9+
path: path.resolve(__dirname, "dist"),
10+
filename: "foo.bundle.js",
11+
},
12+
};
13+
14+
export = config;

test/build/config-format/typescript/typescript.test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const { resolve } = require("path");
44

55
describe("webpack cli", () => {
66
it("should support typescript file", async () => {
7-
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "./webpack.config.ts"]);
7+
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "./webpack.config.ts"], {
8+
nodeOptions: ["--require=ts-node/register"],
9+
});
810

911
expect(stderr).toBeFalsy();
1012
expect(stdout).toBeTruthy();

0 commit comments

Comments
 (0)