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 packages/webpack-cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface IWebpackCLI {
colors: WebpackCLIColors;
logger: WebpackCLILogger;
isColorSupportChanged: boolean | undefined;
isConfigRegistered: boolean | undefined;
webpack: typeof webpack;
builtInOptionsCache: WebpackCLIBuiltInOption[] | undefined;
program: WebpackCLICommand;
Expand Down
16 changes: 14 additions & 2 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class WebpackCLI implements IWebpackCLI {
colors: WebpackCLIColors;
logger: WebpackCLILogger;
isColorSupportChanged: boolean | undefined;
isConfigRegistered: boolean | undefined;
builtInOptionsCache: WebpackCLIBuiltInOption[] | undefined;
webpack!: typeof webpack;
program: WebpackCLICommand;
Expand Down Expand Up @@ -1310,6 +1311,14 @@ class WebpackCLI implements IWebpackCLI {
cli.colors = cli.createColors(color);
});

this.program.option("--config-registered", "Disable interpret a config file.");
shuta13 marked this conversation as resolved.
Show resolved Hide resolved
this.program.on("option:config-registered", function () {
// @ts-expect-error shadowing 'this' is intended
const { configRegistered } = this.opts();

cli.isConfigRegistered = configRegistered;
});

// Make `-v, --version` options
// Make `version|v [commands...]` command
const outputVersion = async (options: string[]) => {
Expand Down Expand Up @@ -1807,12 +1816,15 @@ class WebpackCLI implements IWebpackCLI {
}

async loadConfig(options: Partial<WebpackDevServerOptions>) {
const configRegistered =
typeof this.isConfigRegistered !== undefined && this.isConfigRegistered;

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 && !configRegistered) {
const rechoir: Rechoir = require("rechoir");

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

if (options.config && options.config.length > 0) {
if (options.config && options.config.length > 0 && configRegistered) {
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
Original file line number Diff line number Diff line change
@@ -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 "config-registered" 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, ["--config-registered"]);
unlinkSync(resolve(__dirname, `${configFileName}.js`));

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