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

feat(cli): defineConfig support all types #6911

Merged
merged 7 commits into from
Jun 25, 2024
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
2 changes: 1 addition & 1 deletion packages/rspack-cli/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RspackCLI } from "./rspack-cli";
import { RspackCLI } from "./cli";

export async function runCLI(argv: string[]) {
const cli = new RspackCLI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,32 @@ export class RspackCLI {
}
}

export function defineConfig(config: RspackOptions): RspackOptions {
export type RspackConfigFn = (
env: Record<string, any>,
argv: Record<string, any>
) => RspackOptions | MultiRspackOptions;

export type RspackConfigAsyncFn = (
env: Record<string, any>,
argv: Record<string, any>
) => Promise<RspackOptions | MultiRspackOptions>;

export type RspackConfigExport =
| RspackOptions
| MultiRspackOptions
| RspackConfigFn
| RspackConfigAsyncFn;

/**
* This function helps you to autocomplete configuration types.
* It accepts a Rspack config object, or a function that returns a config.
*/
export function defineConfig(config: RspackOptions): RspackOptions;
export function defineConfig(config: MultiRspackOptions): MultiRspackOptions;
export function defineConfig(config: RspackConfigFn): RspackConfigFn;
export function defineConfig(config: RspackConfigAsyncFn): RspackConfigAsyncFn;
export function defineConfig(config: RspackConfigExport): RspackConfigExport;
export function defineConfig(config: RspackConfigExport) {
return config;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-cli/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from "fs";
import { MultiStats, Stats } from "@rspack/core";

import type { RspackCLI } from "../rspack-cli";
import type { RspackCLI } from "../cli";
import { RspackCommand } from "../types";
import {
commonOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-cli/src/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
rspack
} from "@rspack/core";

import type { RspackCLI } from "../rspack-cli";
import type { RspackCLI } from "../cli";
import { RspackCommand, RspackPreviewCLIOptions } from "../types";
import { previewOptions } from "../utils/options";

Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-cli/src/commands/serve.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Compiler, DevServer } from "@rspack/core";
import type { RspackDevServer as RspackDevServerType } from "@rspack/dev-server";

import type { RspackCLI } from "../rspack-cli";
import type { RspackCLI } from "../cli";
import { RspackCommand } from "../types";
import {
commonOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { defineConfig, definePlugin, RspackCLI } from "./rspack-cli";
export { defineConfig, definePlugin, RspackCLI } from "./cli";
export * from "./types";
2 changes: 1 addition & 1 deletion packages/rspack-cli/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DevServer } from "@rspack/core";
import { Colorette } from "colorette";

import { RspackCLI } from "./rspack-cli";
import { RspackCLI } from "./cli";
export type { Configuration } from "@rspack/core";

export interface IRspackCLI {
Expand Down
68 changes: 37 additions & 31 deletions packages/rspack-cli/tests/build/basic/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,38 +80,44 @@ describe("build command", () => {
expect(mainJs).toContain("other");
expect(mainJs).not.toContain("CONFIG");
});
it.each(["-o", "--output-path"])("output-path option %p should have higher priority than config", async (command) => {
const { exitCode, stderr, stdout } = await run(__dirname, [
command,
"public",
"--config",
"./entry.config.js"
]);
const mainJs = await readFile(
resolve(__dirname, "public/main.js"),
"utf-8"
);
it.each(["-o", "--output-path"])(
"output-path option %p should have higher priority than config",
async command => {
const { exitCode, stderr, stdout } = await run(__dirname, [
command,
"dist/public",
"--config",
"./entry.config.js"
]);
const mainJs = await readFile(
resolve(__dirname, "dist/public/main.js"),
"utf-8"
);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(mainJs).toContain("CONFIG");
});
expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(mainJs).toContain("CONFIG");
}
);

it.each(["-d", "--devtool"])("devtool option %p should have higher priority than config", async (command) => {
const { exitCode, stderr, stdout } = await run(__dirname, [
command,
"--config",
"./entry.config.js"
]);
const mainJs = await readFile(
resolve(__dirname, "public/main.js"),
"utf-8"
);
it.each(["-d", "--devtool"])(
"devtool option %p should have higher priority than config",
async command => {
const { exitCode, stderr, stdout } = await run(__dirname, [
command,
"--config",
"./entry.config.js"
]);
const mainJs = await readFile(
resolve(__dirname, "dist/public/main.js"),
"utf-8"
);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(mainJs).toContain("CONFIG");
});
expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(mainJs).toContain("CONFIG");
}
);
});
7 changes: 0 additions & 7 deletions packages/rspack-cli/tests/build/basic/public/main.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/rspack-cli/tests/build/basic/public/main.js.map

This file was deleted.

37 changes: 32 additions & 5 deletions packages/rspack-cli/tests/build/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,47 @@ describe("rspack cli", () => {
});
});

describe("should load moonrepo config", () => {
const cwd = resolve(__dirname, "./moonrepo");
it("should load moonrepo config.ts file", async () => {
describe("should load config with defineConfig helper", () => {
const cwd = resolve(__dirname, "./esm");

it("should load config.ts file", async () => {
const { exitCode, stdout } = await run(cwd, ["-c", "rspack.config.ts"], {
nodeOptions: ["--experimental-loader=ts-node/esm"]
});
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
expect(
readFile(resolve(cwd, "./dist/ts.bundle.js"), { encoding: "utf-8" })
).resolves.toMatch(/Main esm file/);
});

it("should load config.mts file", async () => {
const { exitCode, stdout } = await run(cwd, ["-c", "rspack.config.mts"], {
nodeOptions: ["--experimental-loader=ts-node/esm"]
});

expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
expect(
readFile(resolve(cwd, "./dist/mts.bundle.js"), { encoding: "utf-8" })
).resolves.toMatch(/Main esm file/);
});
});

describe("should load monorepo config", () => {
const cwd = resolve(__dirname, "./monorepo");
it("should load monorepo config.ts file", async () => {
const { exitCode, stdout } = await run(cwd, ["-c", "rspack.config.ts"], {
nodeOptions: ["--experimental-loader=ts-node/esm"]
});
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
expect(
readFile(
resolve(cwd, `./dist/moonrepo.bundle.depsA.1.0.0-depsB.2.0.0.js`),
resolve(cwd, `./dist/monorepo.bundle.depsA.1.0.0-depsB.2.0.0.js`),
{ encoding: "utf-8" }
)
).resolves.toMatch(/Main moonrepo file/);
).resolves.toMatch(/Main monorepo file/);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Main esm file");
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from "node:path";
import { fileURLToPath } from "node:url";
import { defineConfig } from "@rspack/cli";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default defineConfig(env => {
return {
mode: env.RSPACK_BUILD ? "production" : "development",
entry: path.resolve(__dirname, "main.ts"),
output: {
path: path.resolve(__dirname, "dist"),
filename: "mts.bundle.js"
}
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from "node:path";
import { fileURLToPath } from "node:url";
import { defineConfig } from "@rspack/cli";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default defineConfig([
{
mode: "production",
entry: path.resolve(__dirname, "main.ts"),
output: {
path: path.resolve(__dirname, "dist"),
filename: "ts.bundle.js"
}
}
]);
15 changes: 15 additions & 0 deletions packages/rspack-cli/tests/build/config/define-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"baseUrl": ".",
"rootDir": ".",
"target": "ESNext",
"module": "NodeNext",
"noEmit": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"moduleResolution": "nodenext",
"allowImportingTsExtensions": true,
"allowSyntheticDefaultImports": true
},
"include": ["**/*.ts", "**/*.js", "rspack.config.mts"]
}
8 changes: 4 additions & 4 deletions packages/rspack-cli/tests/build/config/esm/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"baseUrl": ".",
"rootDir": ".",
"target": "ESNext",
"module": "ESNext",
"noEmit":true,
"module": "NodeNext",
"noEmit": true,
"esModuleInterop": true,
"resolveJsonModule":true,
"resolveJsonModule": true,
"moduleResolution": "nodenext",
"allowImportingTsExtensions": true,
"allowSyntheticDefaultImports": true,
"allowSyntheticDefaultImports": true
},
"include": ["**/*.ts", "**/*.js"]
}
1 change: 1 addition & 0 deletions packages/rspack-cli/tests/build/config/monorepo/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Main monorepo file");
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export default {
entry: path.resolve(__dirname, "main.ts"),
output: {
path: path.resolve(__dirname, "dist"),
filename: `moonrepo.bundle.depsA.${packageA_deps.version}-depsB.${packageB_deps.version}.js`
filename: `monorepo.bundle.depsA.${packageA_deps.version}-depsB.${packageB_deps.version}.js`
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"baseUrl": ".",
"rootDir": ".",
"target": "ESNext",
"module": "ESNext",
"noEmit":true,
"resolveJsonModule":true,
"module": "NodeNext",
"noEmit": true,
"resolveJsonModule": true,
"moduleResolution": "nodenext",
"allowImportingTsExtensions": true,
"allowSyntheticDefaultImports": true,
"allowSyntheticDefaultImports": true
},
"include": ["**/*.ts", "**/*.js"]
}
1 change: 0 additions & 1 deletion packages/rspack-cli/tests/build/config/moonrepo/main.ts

This file was deleted.

Loading