Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6cbe9d3

Browse files
committed
fix(webpack-cli): add an option for preventing interpret
1 parent 58503be commit 6cbe9d3

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

packages/webpack-cli/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface IWebpackCLI {
3131
colors: WebpackCLIColors;
3232
logger: WebpackCLILogger;
3333
isColorSupportChanged: boolean | undefined;
34+
isConfigRegistered: boolean | undefined;
3435
webpack: typeof webpack;
3536
builtInOptionsCache: WebpackCLIBuiltInOption[] | undefined;
3637
program: WebpackCLICommand;

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class WebpackCLI implements IWebpackCLI {
6969
colors: WebpackCLIColors;
7070
logger: WebpackCLILogger;
7171
isColorSupportChanged: boolean | undefined;
72+
isConfigRegistered: boolean | undefined;
7273
builtInOptionsCache: WebpackCLIBuiltInOption[] | undefined;
7374
webpack!: typeof webpack;
7475
program: WebpackCLICommand;
@@ -1310,6 +1311,14 @@ class WebpackCLI implements IWebpackCLI {
13101311
cli.colors = cli.createColors(color);
13111312
});
13121313

1314+
this.program.option("--config-registered", "Disable interpret a config file.");
1315+
this.program.on("option:config-registered", function () {
1316+
// @ts-expect-error shadowing 'this' is intended
1317+
const { configRegistered } = this.opts();
1318+
1319+
cli.isConfigRegistered = configRegistered;
1320+
});
1321+
13131322
// Make `-v, --version` options
13141323
// Make `version|v [commands...]` command
13151324
const outputVersion = async (options: string[]) => {
@@ -1807,12 +1816,15 @@ class WebpackCLI implements IWebpackCLI {
18071816
}
18081817

18091818
async loadConfig(options: Partial<WebpackDevServerOptions>) {
1819+
const configRegistered =
1820+
typeof this.isConfigRegistered !== undefined && this.isConfigRegistered;
1821+
18101822
const interpret = require("interpret");
18111823
const loadConfigByPath = async (configPath: string, argv: Argv = {}) => {
18121824
const ext = path.extname(configPath);
18131825
const interpreted = Object.keys(interpret.jsVariants).find((variant) => variant === ext);
18141826

1815-
if (interpreted) {
1827+
if (interpreted && !configRegistered) {
18161828
const rechoir: Rechoir = require("rechoir");
18171829

18181830
try {
@@ -1904,7 +1916,7 @@ class WebpackCLI implements IWebpackCLI {
19041916
path: new WeakMap(),
19051917
};
19061918

1907-
if (options.config && options.config.length > 0) {
1919+
if (options.config && options.config.length > 0 && configRegistered) {
19081920
const loadedConfigs = await Promise.all(
19091921
options.config.map((configPath: string) =>
19101922
loadConfigByPath(path.resolve(configPath), options.argv),

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const { run } = require("../../../utils/test-utils");
2-
const { existsSync } = require("fs");
2+
const { existsSync, unlinkSync } = require("fs");
33
const { resolve } = require("path");
44

5+
// eslint-disable-next-line node/no-unpublished-require
6+
const execa = require("execa");
7+
const { sync: spawnSync } = execa;
8+
59
describe("webpack cli", () => {
610
it("should support typescript file", async () => {
711
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "./webpack.config.ts"]);
@@ -11,4 +15,19 @@ describe("webpack cli", () => {
1115
expect(exitCode).toBe(0);
1216
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy();
1317
});
18+
19+
it('should work with the "config-registered" option from flags', async () => {
20+
const configFileName = "webpack.config";
21+
const configFilePath = resolve(__dirname, `${configFileName}.ts`);
22+
const buildScripts = spawnSync("yarn", ["tsc", configFilePath]);
23+
expect(buildScripts.stdout).toBeTruthy();
24+
25+
const { exitCode, stderr, stdout } = await run(__dirname, ["--config-registered"]);
26+
unlinkSync(resolve(__dirname, `${configFileName}.js`));
27+
28+
expect(stderr).toBeFalsy();
29+
expect(stdout).toBeTruthy();
30+
expect(exitCode).toBe(0);
31+
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy();
32+
});
1433
});

0 commit comments

Comments
 (0)