@rspack/cli 1.0.13 → 1.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Dynamically import files. It will make sure it's not being compiled away by TS/Rollup.
2
+ * Dynamically import files. It will make sure it's not being compiled away by TS/Rslib.
3
3
  */
4
4
  export declare const dynamicImport: Function;
5
5
  declare const crossImport: <T = any>(path: string, cwd?: string) => Promise<T>;
@@ -1,5 +1,5 @@
1
1
  import type yargs from "yargs";
2
- export declare const commonOptions: (yargs: yargs.Argv<{}>) => yargs.Argv<yargs.Omit<{}, "entry" | "devtool" | "mode" | "watch" | "env" | "output-path" | "config" | "node-env" | "configName"> & yargs.InferredOptionTypes<{
2
+ export declare const commonOptions: (yargs: yargs.Argv) => yargs.Argv<yargs.Omit<{}, "entry" | "devtool" | "mode" | "watch" | "env" | "output-path" | "config" | "node-env" | "configName"> & yargs.InferredOptionTypes<{
3
3
  config: {
4
4
  g: boolean;
5
5
  type: "string";
@@ -48,7 +48,7 @@ export declare const commonOptions: (yargs: yargs.Argv<{}>) => yargs.Argv<yargs.
48
48
  describe: string;
49
49
  };
50
50
  }>>;
51
- export declare const previewOptions: (yargs: yargs.Argv<{}>) => yargs.Argv<yargs.Omit<{
51
+ export declare const previewOptions: (yargs: yargs.Argv) => yargs.Argv<yargs.Omit<{
52
52
  dir: string | undefined;
53
53
  }, "port" | "host" | "open" | "server" | "publicPath" | "config" | "configName"> & yargs.InferredOptionTypes<{
54
54
  publicPath: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspack/cli",
3
- "version": "1.0.13",
3
+ "version": "1.1.0-beta.0",
4
4
  "description": "CLI for rspack",
5
5
  "homepage": "https://rspack.dev",
6
6
  "bugs": "https://github.com/web-infra-dev/rspack/issues",
@@ -11,13 +11,16 @@
11
11
  },
12
12
  "license": "MIT",
13
13
  "exports": {
14
- ".": "./dist/index.js",
14
+ ".": {
15
+ "import": "./dist/index.mjs",
16
+ "require": "./dist/index.js"
17
+ },
15
18
  "./package.json": "./package.json"
16
19
  },
17
20
  "main": "./dist/index.js",
18
21
  "types": "./dist/index.d.ts",
19
22
  "bin": {
20
- "rspack": "./bin/rspack"
23
+ "rspack": "./bin/rspack.js"
21
24
  },
22
25
  "files": [
23
26
  "bin",
@@ -27,7 +30,7 @@
27
30
  "@discoveryjs/json-ext": "^0.5.7",
28
31
  "@rspack/dev-server": "1.0.5",
29
32
  "colorette": "2.0.19",
30
- "exit-hook": "^3.2.0",
33
+ "exit-hook": "^4.0.0",
31
34
  "interpret": "^3.1.1",
32
35
  "rechoir": "^0.8.0",
33
36
  "semver": "^7.6.2",
@@ -35,6 +38,7 @@
35
38
  "yargs": "17.6.2"
36
39
  },
37
40
  "devDependencies": {
41
+ "@rslib/core": "0.0.12",
38
42
  "@types/interpret": "^1.1.3",
39
43
  "@types/rechoir": "^0.6.1",
40
44
  "@types/semver": "^7.5.6",
@@ -45,8 +49,8 @@
45
49
  "execa": "^5.0.0",
46
50
  "internal-ip": "6.2.0",
47
51
  "ts-node": "^10.9.2",
48
- "typescript": "5.0.2",
49
- "@rspack/core": "1.0.13"
52
+ "typescript": "^5.6.3",
53
+ "@rspack/core": "1.1.0-beta.0"
50
54
  },
51
55
  "peerDependencies": {
52
56
  "@rspack/core": "^1.0.0-alpha || ^1.x"
@@ -56,8 +60,8 @@
56
60
  "provenance": true
57
61
  },
58
62
  "scripts": {
59
- "build": "tsc -b ./tsconfig.build.json",
60
- "dev": "tsc -b -w",
63
+ "build": "rslib build",
64
+ "dev": "rslib build -w",
61
65
  "test": "cross-env jest --colors"
62
66
  }
63
67
  }
package/bin/rspack DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
- const runCLI = require("../dist/bootstrap").runCLI;
3
- runCLI(process.argv);
@@ -1 +0,0 @@
1
- export declare function runCLI(argv: string[]): Promise<void>;
package/dist/bootstrap.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.runCLI = void 0;
4
- const cli_1 = require("./cli");
5
- async function runCLI(argv) {
6
- const cli = new cli_1.RspackCLI();
7
- await cli.run(argv);
8
- }
9
- exports.runCLI = runCLI;
package/dist/cli.js DELETED
@@ -1,257 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.definePlugin = exports.defineConfig = exports.RspackCLI = void 0;
30
- const node_path_1 = __importDefault(require("node:path"));
31
- const node_util_1 = __importDefault(require("node:util"));
32
- const core_1 = require("@rspack/core");
33
- const rspackCore = __importStar(require("@rspack/core"));
34
- const semver_1 = __importDefault(require("semver"));
35
- const yargs_1 = __importDefault(require("yargs"));
36
- const helpers_1 = require("yargs/helpers");
37
- const build_1 = require("./commands/build");
38
- const preview_1 = require("./commands/preview");
39
- const serve_1 = require("./commands/serve");
40
- const loadConfig_1 = require("./utils/loadConfig");
41
- const options_1 = require("./utils/options");
42
- class RspackCLI {
43
- constructor() {
44
- this.colors = this.createColors();
45
- this.program = (0, yargs_1.default)();
46
- }
47
- async createCompiler(options, rspackCommand, callback) {
48
- process.env.RSPACK_CONFIG_VALIDATE ??= "loose";
49
- process.env.WATCHPACK_WATCHER_LIMIT =
50
- process.env.WATCHPACK_WATCHER_LIMIT || "20";
51
- const nodeEnv = process?.env?.NODE_ENV;
52
- const rspackCommandDefaultEnv = rspackCommand === "build" ? "production" : "development";
53
- if (typeof options.nodeEnv === "string") {
54
- process.env.NODE_ENV = nodeEnv || options.nodeEnv;
55
- }
56
- else {
57
- process.env.NODE_ENV = nodeEnv || rspackCommandDefaultEnv;
58
- }
59
- let config = await this.loadConfig(options);
60
- config = await this.buildConfig(config, options, rspackCommand);
61
- const isWatch = Array.isArray(config)
62
- ? config.some(i => i.watch)
63
- : config.watch;
64
- let compiler;
65
- try {
66
- compiler = (0, core_1.rspack)(config, isWatch ? callback : undefined);
67
- }
68
- catch (e) {
69
- // Aligned with webpack-cli
70
- // See: https://github.com/webpack/webpack-cli/blob/eea6adf7d34dfbfd3b5b784ece4a4664834f5a6a/packages/webpack-cli/src/webpack-cli.ts#L2394
71
- if (e instanceof core_1.ValidationError) {
72
- this.getLogger().error(e.message);
73
- process.exit(2);
74
- }
75
- else if (e instanceof Error) {
76
- callback?.(e);
77
- return null;
78
- }
79
- throw e;
80
- }
81
- return compiler;
82
- }
83
- createColors(useColor) {
84
- const { createColors, isColorSupported } = require("colorette");
85
- const shouldUseColor = useColor || isColorSupported;
86
- return {
87
- ...createColors({ useColor: shouldUseColor }),
88
- isColorSupported: shouldUseColor
89
- };
90
- }
91
- getLogger() {
92
- return {
93
- error: val => console.error(`[rspack-cli] ${this.colors.red(node_util_1.default.format(val))}`),
94
- warn: val => console.warn(`[rspack-cli] ${this.colors.yellow(val)}`),
95
- info: val => console.info(`[rspack-cli] ${this.colors.cyan(val)}`),
96
- success: val => console.log(`[rspack-cli] ${this.colors.green(val)}`),
97
- log: val => console.log(`[rspack-cli] ${val}`),
98
- raw: val => console.log(val)
99
- };
100
- }
101
- async run(argv) {
102
- if (semver_1.default.lt(semver_1.default.clean(process.version), "14.0.0")) {
103
- this.getLogger().warn(`Minimum recommended Node.js version is 14.0.0, current version is ${process.version}`);
104
- }
105
- this.program.showHelpOnFail(false);
106
- this.program.usage("[options]");
107
- this.program.scriptName("rspack");
108
- this.program.strictCommands(true).strict(true);
109
- this.program.middleware(options_1.normalizeEnv);
110
- this.registerCommands();
111
- await this.program.parseAsync((0, helpers_1.hideBin)(argv));
112
- }
113
- async registerCommands() {
114
- const builtinCommands = [
115
- new build_1.BuildCommand(),
116
- new serve_1.ServeCommand(),
117
- new preview_1.PreviewCommand()
118
- ];
119
- for (const command of builtinCommands) {
120
- command.apply(this);
121
- }
122
- }
123
- async buildConfig(item, options, command) {
124
- const isBuild = command === "build";
125
- const isServe = command === "serve";
126
- const commandDefaultEnv = isBuild
127
- ? "production"
128
- : "development";
129
- const internalBuildConfig = async (item) => {
130
- if (options.entry) {
131
- item.entry = {
132
- main: options.entry.map(x => node_path_1.default.resolve(process.cwd(), x))[0] // Fix me when entry supports array
133
- };
134
- }
135
- // to set output.path
136
- item.output = item.output || {};
137
- if (options["output-path"]) {
138
- item.output.path = node_path_1.default.resolve(process.cwd(), options["output-path"]);
139
- }
140
- if (options.analyze) {
141
- const { BundleAnalyzerPlugin } = await import("webpack-bundle-analyzer");
142
- (item.plugins ??= []).push({
143
- name: "rspack-bundle-analyzer",
144
- apply(compiler) {
145
- new BundleAnalyzerPlugin({
146
- generateStatsFile: true
147
- }).apply(compiler);
148
- }
149
- });
150
- }
151
- if (options.profile) {
152
- item.profile = true;
153
- }
154
- if (process.env.RSPACK_PROFILE) {
155
- const { applyProfile } = await import("./utils/profile.js");
156
- await applyProfile(process.env.RSPACK_PROFILE, item);
157
- }
158
- // cli --watch overrides the watch config
159
- if (options.watch) {
160
- item.watch = options.watch;
161
- }
162
- // auto set default mode if user config don't set it
163
- if (!item.mode) {
164
- item.mode = commandDefaultEnv ?? "none";
165
- }
166
- // user parameters always has highest priority than default mode and config mode
167
- if (options.mode) {
168
- item.mode = options.mode;
169
- }
170
- // false is also a valid value for sourcemap, so don't override it
171
- if (typeof item.devtool === "undefined") {
172
- item.devtool = isBuild ? "source-map" : "cheap-module-source-map";
173
- }
174
- if (isServe) {
175
- const installed = (item.plugins ||= []).find(item => item instanceof rspackCore.ProgressPlugin);
176
- if (!installed) {
177
- (item.plugins ??= []).push(new rspackCore.ProgressPlugin());
178
- }
179
- }
180
- if (typeof item.stats === "undefined") {
181
- item.stats = { preset: "errors-warnings", timings: true };
182
- }
183
- else if (typeof item.stats === "boolean") {
184
- item.stats = item.stats ? { preset: "normal" } : { preset: "none" };
185
- }
186
- else if (typeof item.stats === "string") {
187
- item.stats = {
188
- preset: item.stats
189
- };
190
- }
191
- if (this.colors.isColorSupported &&
192
- typeof item.stats.colors === "undefined") {
193
- item.stats.colors = true;
194
- }
195
- return item;
196
- };
197
- if (Array.isArray(item)) {
198
- return Promise.all(item.map(internalBuildConfig));
199
- }
200
- return internalBuildConfig(item);
201
- }
202
- async loadConfig(options) {
203
- let loadedConfig = (await (0, loadConfig_1.loadRspackConfig)(options));
204
- if (options.configName) {
205
- const notFoundConfigNames = [];
206
- loadedConfig = options.configName.map((configName) => {
207
- let found;
208
- if (Array.isArray(loadedConfig)) {
209
- found = loadedConfig.find(options => options.name === configName);
210
- }
211
- else {
212
- found =
213
- loadedConfig.name === configName
214
- ? loadedConfig
215
- : undefined;
216
- }
217
- if (!found) {
218
- notFoundConfigNames.push(configName);
219
- }
220
- // WARNING: if config is not found, the program will exit
221
- // so assert here is okay to avoid runtime filtering
222
- return found;
223
- });
224
- if (notFoundConfigNames.length > 0) {
225
- this.getLogger().error(notFoundConfigNames
226
- .map(configName => `Configuration with the name "${configName}" was not found.`)
227
- .join(" "));
228
- process.exit(2);
229
- }
230
- }
231
- if (typeof loadedConfig === "function") {
232
- loadedConfig = loadedConfig(options.argv?.env, options.argv);
233
- // if return promise we should await its result
234
- if (typeof loadedConfig.then === "function") {
235
- loadedConfig = await loadedConfig;
236
- }
237
- }
238
- return loadedConfig;
239
- }
240
- isMultipleCompiler(compiler) {
241
- return Boolean(compiler.compilers);
242
- }
243
- isWatch(compiler) {
244
- return Boolean(this.isMultipleCompiler(compiler)
245
- ? compiler.compilers.some(compiler => compiler.options.watch)
246
- : compiler.options.watch);
247
- }
248
- }
249
- exports.RspackCLI = RspackCLI;
250
- function defineConfig(config) {
251
- return config;
252
- }
253
- exports.defineConfig = defineConfig;
254
- function definePlugin(plugin) {
255
- return plugin;
256
- }
257
- exports.definePlugin = definePlugin;
@@ -1,132 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.BuildCommand = void 0;
27
- const fs = __importStar(require("node:fs"));
28
- const options_1 = require("../utils/options");
29
- class BuildCommand {
30
- async apply(cli) {
31
- cli.program.command(["build", "$0", "bundle", "b"], "run the rspack build", yargs => (0, options_1.commonOptions)(yargs).options({
32
- analyze: {
33
- type: "boolean",
34
- default: false,
35
- describe: "analyze"
36
- },
37
- json: {
38
- describe: "emit stats json"
39
- },
40
- profile: {
41
- type: "boolean",
42
- default: false,
43
- describe: "capture timing information for each module"
44
- }
45
- }), async (options) => {
46
- const env = (0, options_1.ensureEnvObject)(options);
47
- if (options.watch) {
48
- (0, options_1.setBuiltinEnvArg)(env, "WATCH", true);
49
- }
50
- else {
51
- (0, options_1.setBuiltinEnvArg)(env, "BUNDLE", true);
52
- (0, options_1.setBuiltinEnvArg)(env, "BUILD", true);
53
- }
54
- const logger = cli.getLogger();
55
- let createJsonStringifyStream;
56
- if (options.json) {
57
- const jsonExt = await import("@discoveryjs/json-ext");
58
- createJsonStringifyStream = jsonExt.default.stringifyStream;
59
- }
60
- const errorHandler = (error, stats) => {
61
- if (error) {
62
- logger.error(error);
63
- process.exit(2);
64
- }
65
- if (stats?.hasErrors()) {
66
- process.exitCode = 1;
67
- }
68
- if (!compiler || !stats) {
69
- return;
70
- }
71
- const statsOptions = cli.isMultipleCompiler(compiler)
72
- ? {
73
- children: compiler.compilers.map(compiler => compiler.options ? compiler.options.stats : undefined)
74
- }
75
- : compiler.options
76
- ? compiler.options.stats
77
- : undefined;
78
- if (options.json && createJsonStringifyStream) {
79
- const handleWriteError = (error) => {
80
- logger.error(error);
81
- process.exit(2);
82
- };
83
- if (options.json === true) {
84
- createJsonStringifyStream(stats.toJson(statsOptions))
85
- .on("error", handleWriteError)
86
- .pipe(process.stdout)
87
- .on("error", handleWriteError)
88
- .on("close", () => process.stdout.write("\n"));
89
- }
90
- else if (typeof options.json === "string") {
91
- createJsonStringifyStream(stats.toJson(statsOptions))
92
- .on("error", handleWriteError)
93
- .pipe(fs.createWriteStream(options.json))
94
- .on("error", handleWriteError)
95
- // Use stderr to logging
96
- .on("close", () => {
97
- process.stderr.write(`[rspack-cli] ${cli.colors.green(`stats are successfully stored as json to ${options.json}`)}\n`);
98
- });
99
- }
100
- }
101
- else {
102
- const printedStats = stats.toString(statsOptions);
103
- // Avoid extra empty line when `stats: 'none'`
104
- if (printedStats) {
105
- logger.raw(printedStats);
106
- }
107
- }
108
- };
109
- const rspackOptions = { ...options, argv: { ...options } };
110
- const compiler = await cli.createCompiler(rspackOptions, "build", errorHandler);
111
- if (!compiler || cli.isWatch(compiler)) {
112
- return;
113
- }
114
- compiler.run((error, stats) => {
115
- // If there is a compilation error, the close method should not be called,
116
- // Otherwise Rspack may generate invalid caches.
117
- if (error || stats?.hasErrors()) {
118
- errorHandler(error, stats);
119
- }
120
- else {
121
- compiler.close(closeErr => {
122
- if (closeErr) {
123
- logger.error(closeErr);
124
- }
125
- errorHandler(error, stats);
126
- });
127
- }
128
- });
129
- });
130
- }
131
- }
132
- exports.BuildCommand = BuildCommand;
@@ -1,73 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.PreviewCommand = void 0;
7
- const node_path_1 = __importDefault(require("node:path"));
8
- const core_1 = require("@rspack/core");
9
- const options_1 = require("../utils/options");
10
- const defaultRoot = "dist";
11
- class PreviewCommand {
12
- async apply(cli) {
13
- cli.program.command(["preview [dir]", "preview", "p"], "run the rspack server for build output", options_1.previewOptions, async (options) => {
14
- // config、configName are necessary for loadConfig
15
- const rspackOptions = {
16
- config: options.config,
17
- configName: options.configName,
18
- argv: {
19
- ...options
20
- }
21
- };
22
- const { RspackDevServer } = await import("@rspack/dev-server");
23
- let config = await cli.loadConfig(rspackOptions);
24
- config = await getPreviewConfig(config, options);
25
- if (!Array.isArray(config)) {
26
- config = [config];
27
- }
28
- config = config;
29
- // find the possible devServer config
30
- config = config.find(item => item.devServer) || config[0];
31
- const devServerOptions = config.devServer;
32
- try {
33
- const compiler = (0, core_1.rspack)({ entry: {} });
34
- if (!compiler)
35
- return;
36
- const server = new RspackDevServer(devServerOptions, compiler);
37
- await server.start();
38
- }
39
- catch (error) {
40
- const logger = cli.getLogger();
41
- logger.error(error);
42
- process.exit(2);
43
- }
44
- });
45
- }
46
- }
47
- exports.PreviewCommand = PreviewCommand;
48
- // get the devServerOptions from the config
49
- async function getPreviewConfig(item, options) {
50
- const internalPreviewConfig = async (item) => {
51
- // all of the options that a preview static server needs(maybe not all)
52
- item.devServer = {
53
- static: {
54
- directory: options.dir
55
- ? node_path_1.default.join(item.context ?? process.cwd(), options.dir)
56
- : item.output?.path ??
57
- node_path_1.default.join(item.context ?? process.cwd(), defaultRoot),
58
- publicPath: options.publicPath ?? "/"
59
- },
60
- port: options.port ?? 8080,
61
- proxy: item.devServer?.proxy,
62
- host: options.host ?? item.devServer?.host,
63
- open: options.open ?? item.devServer?.open,
64
- server: options.server ?? item.devServer?.server,
65
- historyApiFallback: item.devServer?.historyApiFallback
66
- };
67
- return item;
68
- };
69
- if (Array.isArray(item)) {
70
- return Promise.all(item.map(internalPreviewConfig));
71
- }
72
- return internalPreviewConfig(item);
73
- }