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(webpack-cli): add an option for preventing interpret #3329

Merged
merged 14 commits into from Jul 25, 2022
1 change: 1 addition & 0 deletions OPTIONS.md
Expand Up @@ -8,6 +8,7 @@ Options:
-c, --config <value...> Provide path to a webpack configuration file e.g. ./webpack.config.js.
--config-name <value...> Name of the configuration to use.
-m, --merge Merge two or more configurations using 'webpack-merge'.
--disable-interpret Disable interpret a config file.
shuta13 marked this conversation as resolved.
Show resolved Hide resolved
--env <value...> Environment passed to the configuration when it is a function.
--node-env <value> Sets process.env.NODE_ENV to the specified value.
-h, --hot [value] Enables Hot Module Replacement
Expand Down
1 change: 1 addition & 0 deletions packages/webpack-cli/src/types.ts
Expand Up @@ -175,6 +175,7 @@ type WebpackDevServerOptions = DevServerConfig &
merge?: boolean;
config: string[];
configName?: string[];
disableInterpret?: boolean;
argv: Argv;
};

Expand Down
17 changes: 15 additions & 2 deletions packages/webpack-cli/src/webpack-cli.ts
Expand Up @@ -700,6 +700,7 @@ class WebpackCLI implements IWebpackCLI {
"config",
"config-name",
"merge",
"disable-interpret",
"env",
"mode",
"watch",
Expand Down Expand Up @@ -749,6 +750,15 @@ class WebpackCLI implements IWebpackCLI {
],
description: "Merge two or more configurations using 'webpack-merge'.",
},
{
name: "disable-interpret",
configs: [
{
type: "boolean",
shuta13 marked this conversation as resolved.
Show resolved Hide resolved
},
],
description: "Disable interpret a config file.",
},
// Complex configs
{
name: "env",
Expand Down Expand Up @@ -1807,12 +1817,15 @@ class WebpackCLI implements IWebpackCLI {
}

async loadConfig(options: Partial<WebpackDevServerOptions>) {
const disableInterpret =
typeof options.disableInterpret !== undefined && options.disableInterpret;
shuta13 marked this conversation as resolved.
Show resolved Hide resolved

const interpret = require("interpret");
const loadConfigByPath = async (configPath: string, argv: Argv = {}) => {
const ext = path.extname(configPath);
const interpreted = Object.keys(interpret.jsVariants).find((variant) => variant === ext);

if (interpreted) {
if (interpreted && !disableInterpret) {
const rechoir: Rechoir = require("rechoir");

try {
Expand Down Expand Up @@ -1904,7 +1917,7 @@ class WebpackCLI implements IWebpackCLI {
path: new WeakMap(),
};

if (options.config && options.config.length > 0) {
if (options.config && options.config.length > 0 && disableInterpret) {
shuta13 marked this conversation as resolved.
Show resolved Hide resolved
const loadedConfigs = await Promise.all(
options.config.map((configPath: string) =>
loadConfigByPath(path.resolve(configPath), options.argv),
Expand Down
21 changes: 20 additions & 1 deletion test/build/config-format/typescript/typescript.test.js
@@ -1,7 +1,11 @@
const { run } = require("../../../utils/test-utils");
const { existsSync } = require("fs");
const { existsSync, unlinkSync } = require("fs");
const { resolve } = require("path");

// eslint-disable-next-line node/no-unpublished-require
const execa = require("execa");
const { sync: spawnSync } = execa;

describe("webpack cli", () => {
it("should support typescript file", async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "./webpack.config.ts"]);
Expand All @@ -11,4 +15,19 @@ describe("webpack cli", () => {
expect(exitCode).toBe(0);
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy();
});

it('should work with the "disable-interpret" option from flags', async () => {
shuta13 marked this conversation as resolved.
Show resolved Hide resolved
rishabh3112 marked this conversation as resolved.
Show resolved Hide resolved
const configFileName = "webpack.config";
const configFilePath = resolve(__dirname, `${configFileName}.ts`);
const buildScripts = spawnSync("yarn", ["tsc", configFilePath]);
expect(buildScripts.stdout).toBeTruthy();

const { exitCode, stderr, stdout } = await run(__dirname, ["--disable-interpret"]);
unlinkSync(resolve(__dirname, `${configFileName}.js`));

expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy();
});
});