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
Merged
1 change: 1 addition & 0 deletions OPTIONS.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ type WebpackDevServerOptions = DevServerConfig &
merge?: boolean;
config: string[];
configName?: string[];
disableInterpret?: boolean;
argv: Argv;
};

Expand Down
15 changes: 14 additions & 1 deletion packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { run } = require("../../../utils/test-utils");
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 work with the "disable-interpret" option from flags', async () => {
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();
});

it("should log error without transpilation", async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ["--disable-interpret"]);

expect(exitCode).toBe(2);
expect(stderr).toContain(`Failed to load '${resolve(__dirname, "webpack.config.ts")}' config`);
expect(stdout).toBeFalsy();
});
});
1 change: 1 addition & 0 deletions test/build/config-format/disable-interpret/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Main typescript file");
5 changes: 5 additions & 0 deletions test/build/config-format/disable-interpret/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "commonjs"
}
}
14 changes: 14 additions & 0 deletions test/build/config-format/disable-interpret/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable node/no-unsupported-features/es-syntax */
/** eslint-disable **/
import * as path from "path";

const config = {
mode: "production",
entry: "./main.ts",
output: {
path: path.resolve(__dirname, "dist"),
filename: "foo.bundle.js",
},
};

export = config;