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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 for loading the config file.
--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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"pretest": "yarn build && yarn lint && yarn prepsuite",
"test": "jest --reporters=default",
"test:smoketests": "nyc node smoketests",
"test:coverage": "nyc --no-clean --require ts-node/register jest",
"test:coverage": "nyc --no-clean jest",
"test:cli": "jest test --reporters=default",
"test:packages": "jest packages/ --reporters=default",
"test:ci": "yarn test:cli && yarn test:packages",
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
16 changes: 15 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,16 @@ class WebpackCLI implements IWebpackCLI {
],
description: "Merge two or more configurations using 'webpack-merge'.",
},
{
name: "disable-interpret",
configs: [
{
type: "enum",
values: [true],
},
],
description: "Disable interpret a config file.",
},
// Complex configs
{
name: "env",
Expand Down Expand Up @@ -1807,12 +1818,15 @@ class WebpackCLI implements IWebpackCLI {
}

async loadConfig(options: Partial<WebpackDevServerOptions>) {
const disableInterpret =
typeof options.disableInterpret !== "undefined" && options.disableInterpret;

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;
4 changes: 3 additions & 1 deletion test/build/config-format/typescript/typescript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const { resolve } = require("path");

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

expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
Expand Down
Loading