@rspack/cli 2.0.0-rc.1 → 2.0.0-rc.3

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.
@@ -72,17 +72,10 @@ var __webpack_require__temp = __webpack_require__;
72
72
  });
73
73
  };
74
74
  })();
75
- (()=>{
76
- __webpack_require__.nmd = (module)=>{
77
- module.paths = [];
78
- if (!module.children) module.children = [];
79
- return module;
80
- };
81
- })();
82
75
  (()=>{
83
76
  var installedChunks = {
84
- 410: 0,
85
- 697: 0
77
+ 162: 0,
78
+ 410: 0
86
79
  };
87
80
  var installChunk = (data)=>{
88
81
  var __rspack_esm_ids = data.__rspack_esm_ids;
@@ -100,77 +93,8 @@ var __webpack_require__temp = __webpack_require__;
100
93
  __webpack_require__.C = installChunk;
101
94
  })();
102
95
  __webpack_require__.add({
103
- "../../node_modules/.pnpm/pirates@4.0.7/node_modules/pirates/lib/index.js" (module, exports, __webpack_require__) {
104
- module = __webpack_require__.nmd(module);
105
- const BuiltinModule = __webpack_require__("module");
106
- const path = __webpack_require__("path");
107
- const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/;
108
- const Module = module.constructor.length > 1 ? module.constructor : BuiltinModule;
109
- const HOOK_RETURNED_NOTHING_ERROR_MESSAGE = "[Pirates] A hook returned a non-string, or nothing at all! This is a violation of intergalactic law!\n--------------------\nIf you have no idea what this means or what Pirates is, let me explain: Pirates is a module that makes it easy to implement require hooks. One of the require hooks you're using uses it. One of these require hooks didn't return anything from it's handler, so we don't know what to do. You might want to debug this.";
110
- function shouldCompile(filename, exts, matcher, ignoreNodeModules) {
111
- if ('string' != typeof filename) return false;
112
- if (-1 === exts.indexOf(path.extname(filename))) return false;
113
- const resolvedFilename = path.resolve(filename);
114
- if (ignoreNodeModules && nodeModulesRegex.test(resolvedFilename)) return false;
115
- if (matcher && 'function' == typeof matcher) return !!matcher(resolvedFilename);
116
- return true;
117
- }
118
- function addHook(hook, opts = {}) {
119
- let reverted = false;
120
- const loaders = [];
121
- const oldLoaders = [];
122
- let exts;
123
- const originalJSLoader = Module._extensions['.js'];
124
- const matcher = opts.matcher || null;
125
- const ignoreNodeModules = false !== opts.ignoreNodeModules;
126
- exts = opts.extensions || opts.exts || opts.extension || opts.ext || [
127
- '.js'
128
- ];
129
- if (!Array.isArray(exts)) exts = [
130
- exts
131
- ];
132
- exts.forEach((ext)=>{
133
- if ('string' != typeof ext) throw new TypeError(`Invalid Extension: ${ext}`);
134
- const oldLoader = Module._extensions[ext] || originalJSLoader;
135
- oldLoaders[ext] = Module._extensions[ext];
136
- loaders[ext] = Module._extensions[ext] = function(mod, filename) {
137
- let compile;
138
- if (!reverted) {
139
- if (shouldCompile(filename, exts, matcher, ignoreNodeModules)) {
140
- compile = mod._compile;
141
- mod._compile = function(code) {
142
- mod._compile = compile;
143
- const newCode = hook(code, filename);
144
- if ('string' != typeof newCode) throw new Error(HOOK_RETURNED_NOTHING_ERROR_MESSAGE);
145
- return mod._compile(newCode, filename);
146
- };
147
- }
148
- }
149
- oldLoader(mod, filename);
150
- };
151
- });
152
- return function() {
153
- if (reverted) return;
154
- reverted = true;
155
- exts.forEach((ext)=>{
156
- if (Module._extensions[ext] === loaders[ext]) if (oldLoaders[ext]) Module._extensions[ext] = oldLoaders[ext];
157
- else delete Module._extensions[ext];
158
- });
159
- };
160
- }
161
- exports.addHook = addHook;
162
- },
163
- module (module) {
164
- module.exports = __rspack_createRequire_require("module");
165
- },
166
- path (module) {
167
- module.exports = __rspack_createRequire_require("path");
168
- },
169
96
  stream (module) {
170
97
  module.exports = __rspack_createRequire_require("stream");
171
- },
172
- util (module) {
173
- module.exports = __rspack_createRequire_require("util");
174
98
  }
175
99
  });
176
100
  function toArr(any) {
@@ -725,8 +649,8 @@ const commonOptions = (command)=>command.option('-c, --config <path>', 'config f
725
649
  String
726
650
  ],
727
651
  default: []
728
- }).option('--config-loader <loader>', 'Specify the loader to load the config file, can be `native` or `register`.', {
729
- default: 'register'
652
+ }).option('--config-loader <loader>', 'Specify the loader to load the config file, can be `auto`, `jiti` or `native`.', {
653
+ default: 'auto'
730
654
  }).option('--env <env>', 'env passed to config function', {
731
655
  type: [
732
656
  String
@@ -899,6 +823,7 @@ async function getPreviewConfig(item, options, dir) {
899
823
  directory: dir ? node_path.join(item.context ?? process.cwd(), dir) : item.output?.path ?? node_path.join(item.context ?? process.cwd(), DEFAULT_ROOT),
900
824
  publicPath: options.publicPath ?? '/'
901
825
  },
826
+ hot: false,
902
827
  port: options.port ?? 8080,
903
828
  proxy: devServer?.proxy,
904
829
  host: options.host ?? devServer?.host,
@@ -1014,105 +939,83 @@ class ServeCommand {
1014
939
  }));
1015
940
  }
1016
941
  }
1017
- const readPackageUp = (cwd = process.cwd())=>{
1018
- let currentDir = node_path.resolve(cwd);
1019
- let packageJsonPath = node_path.join(currentDir, 'package.json');
1020
- while(!node_fs.existsSync(packageJsonPath)){
1021
- const parentDir = node_path.dirname(currentDir);
1022
- if (parentDir === currentDir) return null;
1023
- currentDir = parentDir;
1024
- packageJsonPath = node_path.join(currentDir, 'package.json');
1025
- }
1026
- try {
1027
- return JSON.parse(node_fs.readFileSync(packageJsonPath, 'utf8'));
1028
- } catch {
1029
- return null;
1030
- }
1031
- };
1032
- const utils_readPackageUp = readPackageUp;
1033
- const isEsmFile = (filePath)=>{
1034
- if (/\.(mjs|mts)$/.test(filePath)) return true;
1035
- if (/\.(cjs|cts)$/.test(filePath)) return false;
1036
- const packageJson = utils_readPackageUp(node_path.dirname(filePath));
1037
- return packageJson?.type === 'module';
1038
- };
1039
- const crossImport_require = createRequire(import.meta.url);
1040
- const crossImport = async (path)=>{
1041
- if (isEsmFile(path)) {
1042
- const url = pathToFileURL(path).href;
1043
- const { default: config } = await import(url);
1044
- return config;
1045
- }
1046
- let result = crossImport_require(path);
1047
- if (result && 'object' == typeof result && 'default' in result) result = result.default || {};
1048
- return result;
1049
- };
1050
942
  const DEFAULT_EXTENSIONS = [
1051
- '.js',
1052
943
  '.ts',
1053
- '.mjs',
944
+ '.js',
1054
945
  '.mts',
1055
- '.cjs',
1056
- '.cts'
946
+ '.mjs',
947
+ '.cts',
948
+ '.cjs'
1057
949
  ];
1058
950
  const findConfig = (basePath)=>DEFAULT_EXTENSIONS.map((ext)=>basePath + ext).find(node_fs.existsSync);
1059
951
  const utils_findConfig = findConfig;
1060
- const TS_EXTENSION = [
1061
- '.ts',
1062
- '.cts',
1063
- '.mts'
952
+ const loadConfig_require = createRequire(import.meta.url);
953
+ const loadConfig_DEFAULT_CONFIG_NAME = 'rspack.config';
954
+ const JS_CONFIG_EXTENSION_REGEXP = /\.(?:js|mjs|cjs)$/;
955
+ const CONFIG_LOADER_VALUES = [
956
+ 'auto',
957
+ 'jiti',
958
+ 'native'
1064
959
  ];
1065
- const isTsFile = (configPath)=>{
1066
- const ext = node_path.extname(configPath);
1067
- return TS_EXTENSION.includes(ext);
960
+ const PREBUNDLED_JITI_PATH = new URL('../compiled/jiti/index.js', import.meta.url).href;
961
+ const supportsNativeTypeScript = ()=>{
962
+ const features = process.features;
963
+ return Boolean(features.typescript || process.versions.bun || process.versions.deno);
1068
964
  };
1069
- const utils_isTsFile = isTsFile;
1070
- const lib = __webpack_require__("../../node_modules/.pnpm/pirates@4.0.7/node_modules/pirates/lib/index.js");
1071
- const loadConfig_require = createRequire(import.meta.url);
1072
- const injectInlineSourceMap = ({ code, map })=>{
1073
- if (map) {
1074
- const base64Map = Buffer.from(map, 'utf8').toString('base64');
1075
- const sourceMapContent = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}`;
1076
- return `${code}\n${sourceMapContent}`;
1077
- }
1078
- return code;
965
+ const normalizeConfigLoader = (configLoader)=>{
966
+ const normalizedLoader = configLoader ?? 'auto';
967
+ if (CONFIG_LOADER_VALUES.includes(normalizedLoader)) return normalizedLoader;
968
+ throw new Error(`config loader "${normalizedLoader}" is not supported. Expected one of: ${CONFIG_LOADER_VALUES.join(', ')}.`);
1079
969
  };
1080
- function compile(sourcecode, filename) {
1081
- const { code, map } = rspack.experiments.swc.transformSync(sourcecode, {
1082
- jsc: {
1083
- parser: {
1084
- syntax: "typescript",
1085
- tsx: false,
1086
- decorators: true,
1087
- dynamicImport: true
1088
- }
1089
- },
1090
- filename: filename,
1091
- module: {
1092
- type: 'commonjs'
1093
- },
1094
- sourceMaps: true,
1095
- isModule: true
1096
- });
1097
- return injectInlineSourceMap({
1098
- code,
1099
- map
970
+ const resolveDefaultExport = (result)=>result && 'object' == typeof result && 'default' in result ? result.default : result;
971
+ const loadConfigWithNativeLoader = async (configPath)=>{
972
+ const configFileURL = pathToFileURL(configPath).href;
973
+ const loadedModule = await import(`${configFileURL}?t=${Date.now()}`);
974
+ return resolveDefaultExport(loadedModule);
975
+ };
976
+ let jitiInstancePromise;
977
+ const getJiti = async ()=>{
978
+ if (!jitiInstancePromise) jitiInstancePromise = import(PREBUNDLED_JITI_PATH).then((module)=>{
979
+ const createJiti = 'createJiti' in module ? module.createJiti : module.default;
980
+ return createJiti(import.meta.filename, {
981
+ moduleCache: false,
982
+ interopDefault: true,
983
+ nativeModules: [
984
+ "typescript"
985
+ ]
986
+ });
1100
987
  });
1101
- }
1102
- const loadConfig_DEFAULT_CONFIG_NAME = 'rspack.config';
1103
- const registerLoader = (configPath)=>{
1104
- if (isEsmFile(configPath) && utils_isTsFile(configPath)) return;
1105
- if (!utils_isTsFile(configPath)) throw new Error(`config file "${configPath}" is not supported.`);
1106
- (0, lib.addHook)((code, filename)=>{
1107
- try {
1108
- return compile(code, filename);
1109
- } catch (err) {
1110
- throw new Error(`Failed to transform file "${filename}" when loading TypeScript config file:\n ${err instanceof Error ? err.message : String(err)}`);
1111
- }
1112
- }, {
1113
- exts: TS_EXTENSION
988
+ return jitiInstancePromise;
989
+ };
990
+ const loadConfigWithJiti = async (configPath)=>{
991
+ const jiti = await getJiti();
992
+ return jiti.import(configPath, {
993
+ default: true
1114
994
  });
1115
995
  };
996
+ const loadConfigByPath = async (configPath, options)=>{
997
+ const configLoader = normalizeConfigLoader(options.configLoader);
998
+ const useNative = Boolean('native' === configLoader || 'auto' === configLoader && supportsNativeTypeScript());
999
+ if (useNative || JS_CONFIG_EXTENSION_REGEXP.test(configPath)) try {
1000
+ return await loadConfigWithNativeLoader(configPath);
1001
+ } catch (error) {
1002
+ if ('native' === configLoader) throw error;
1003
+ }
1004
+ return loadConfigWithJiti(configPath);
1005
+ };
1006
+ const isConfigObject = (value)=>Boolean(value) && 'object' == typeof value && !Array.isArray(value);
1007
+ const isRspackConfig = (value)=>Array.isArray(value) || isConfigObject(value);
1008
+ const resolveRspackConfigExport = async (configExport, options)=>{
1009
+ let loadedConfig = configExport;
1010
+ if ('function' == typeof loadedConfig) {
1011
+ let functionResult = loadedConfig(options.env, options);
1012
+ if ('function' == typeof functionResult.then) functionResult = await functionResult;
1013
+ if (void 0 === functionResult) throw new Error('[rspack-cli:loadConfig] The config function must return a config object.');
1014
+ loadedConfig = functionResult;
1015
+ }
1016
+ if (!isRspackConfig(loadedConfig)) throw new Error(`[rspack-cli:loadConfig] The config must be an object, an array, or a function that returns one, get ${String(loadedConfig)}`);
1017
+ return loadedConfig;
1018
+ };
1116
1019
  const checkIsMultiRspackOptions = (config)=>Array.isArray(config);
1117
1020
  async function loadExtendedConfig(config, configPath, cwd, options) {
1118
1021
  if (checkIsMultiRspackOptions(config)) {
@@ -1165,13 +1068,9 @@ async function loadExtendedConfig(config, configPath, cwd, options) {
1165
1068
  throw new Error(`Cannot find module '${extendPath}' to extend from.`);
1166
1069
  }
1167
1070
  if (!node_fs.existsSync(resolvedPath)) throw new Error(`Extended configuration file "${resolvedPath}" not found.`);
1168
- if (utils_isTsFile(resolvedPath) && 'register' === options.configLoader) registerLoader(resolvedPath);
1169
- let loadedConfig = await crossImport(resolvedPath);
1170
- if ('function' == typeof loadedConfig) {
1171
- loadedConfig = loadedConfig(options.env, options);
1172
- if ('function' == typeof loadedConfig.then) loadedConfig = await loadedConfig;
1173
- }
1174
- const { config: extendedConfig, pathMap: extendedPathMap } = await loadExtendedConfig(loadedConfig, resolvedPath, cwd, options);
1071
+ const loadedConfig = await loadConfigByPath(resolvedPath, options);
1072
+ const resolvedConfig = await resolveRspackConfigExport(loadedConfig, options);
1073
+ const { config: extendedConfig, pathMap: extendedPathMap } = await loadExtendedConfig(resolvedConfig, resolvedPath, cwd, options);
1175
1074
  const configPaths = [
1176
1075
  ...pathMap.get(resultConfig) || [],
1177
1076
  ...extendedPathMap.get(extendedConfig) || []
@@ -1194,8 +1093,7 @@ async function loadRspackConfig(options, cwd = process.cwd()) {
1194
1093
  if (!defaultConfig) return null;
1195
1094
  configPath = defaultConfig;
1196
1095
  }
1197
- if (utils_isTsFile(configPath) && 'register' === options.configLoader) registerLoader(configPath);
1198
- const loadedConfig = await crossImport(configPath);
1096
+ const loadedConfig = await loadConfigByPath(configPath, options);
1199
1097
  return {
1200
1098
  loadedConfig,
1201
1099
  configPath
@@ -1233,7 +1131,7 @@ class RspackCLI {
1233
1131
  this.colors = this.createColors();
1234
1132
  this.program = program;
1235
1133
  program.help();
1236
- program.version("2.0.0-rc.1");
1134
+ program.version("2.0.0-rc.3");
1237
1135
  }
1238
1136
  wrapAction(fn) {
1239
1137
  return (...args)=>{
@@ -1362,13 +1260,9 @@ class RspackCLI {
1362
1260
  config: this.filterConfig(options, {}),
1363
1261
  pathMap: new WeakMap()
1364
1262
  };
1365
- let { loadedConfig, configPath } = config;
1366
- if ('function' == typeof loadedConfig) {
1367
- let functionResult = loadedConfig(options.env, options);
1368
- if ('function' == typeof functionResult.then) functionResult = await functionResult;
1369
- loadedConfig = functionResult;
1370
- }
1371
- const { config: extendedConfig, pathMap } = await loadExtendedConfig(loadedConfig, configPath, process.cwd(), options);
1263
+ const { loadedConfig, configPath } = config;
1264
+ const resolvedConfig = await resolveRspackConfigExport(loadedConfig, options);
1265
+ const { config: extendedConfig, pathMap } = await loadExtendedConfig(resolvedConfig, configPath, process.cwd(), options);
1372
1266
  return {
1373
1267
  config: this.filterConfig(options, extendedConfig),
1374
1268
  pathMap
@@ -1,3 +1,3 @@
1
1
  export declare const DEFAULT_CONFIG_NAME: "rspack.config";
2
- export declare const DEFAULT_EXTENSIONS: readonly [".js", ".ts", ".mjs", ".mts", ".cjs", ".cts"];
2
+ export declare const DEFAULT_EXTENSIONS: readonly [".ts", ".js", ".mts", ".mjs", ".cts", ".cjs"];
3
3
  export declare const DEFAULT_SERVER_HOT = true;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { RspackCLI, defineConfig, definePlugin } from "./697.js";
1
+ export { RspackCLI, defineConfig, definePlugin } from "./162.js";
package/dist/json-ext.js CHANGED
@@ -1,5 +1,6 @@
1
- import { __webpack_require__ } from "./697.js";
2
- import "./697.js";
1
+ import { __webpack_require__ } from "./162.js";
2
+ import "./162.js";
3
+ import * as __rspack_external_util from "util";
3
4
  __webpack_require__.add({
4
5
  "../../node_modules/.pnpm/@discoveryjs+json-ext@0.5.7/node_modules/@discoveryjs/json-ext/src/index.js" (module, __unused_rspack_exports, __webpack_require__) {
5
6
  module.exports = {
@@ -763,6 +764,9 @@ __webpack_require__.add({
763
764
  "../../node_modules/.pnpm/@discoveryjs+json-ext@0.5.7/node_modules/@discoveryjs/json-ext/src/version.js" (module, __unused_rspack_exports, __webpack_require__) {
764
765
  module.exports = __webpack_require__("../../node_modules/.pnpm/@discoveryjs+json-ext@0.5.7/node_modules/@discoveryjs/json-ext/package.json").version;
765
766
  },
767
+ util (module) {
768
+ module.exports = __rspack_external_util;
769
+ },
766
770
  "../../node_modules/.pnpm/@discoveryjs+json-ext@0.5.7/node_modules/@discoveryjs/json-ext/package.json" (module) {
767
771
  module.exports = {
768
772
  version: "0.5.7"
@@ -1,7 +1,7 @@
1
1
  import type { MultiRspackOptions, RspackOptions } from '@rspack/core';
2
2
  import type { CommonOptions } from './options';
3
- export declare function compile(sourcecode: string, filename: string): string;
4
- export type LoadedRspackConfig = undefined | RspackOptions | MultiRspackOptions | ((env: Record<string, any>, argv?: Record<string, any>) => RspackOptions | MultiRspackOptions);
3
+ export type LoadedRspackConfig = undefined | RspackOptions | MultiRspackOptions | ((env: Record<string, any>, argv?: Record<string, any>) => RspackOptions | MultiRspackOptions | Promise<RspackOptions | MultiRspackOptions>);
4
+ export declare const resolveRspackConfigExport: (configExport: LoadedRspackConfig, options: CommonOptions) => Promise<RspackOptions | MultiRspackOptions>;
5
5
  /**
6
6
  * Loads and merges configurations from the 'extends' property
7
7
  * @param config The configuration object that may contain an 'extends' property
@@ -1,4 +1,5 @@
1
1
  import type { Command } from 'cac';
2
+ export type ConfigLoader = 'auto' | 'jiti' | 'native';
2
3
  /**
3
4
  * Apply common options for all commands
4
5
  */
@@ -6,7 +7,7 @@ export declare const commonOptions: (command: Command) => Command;
6
7
  export type CommonOptions = {
7
8
  config?: string;
8
9
  configName?: string[];
9
- configLoader?: string;
10
+ configLoader?: ConfigLoader;
10
11
  env?: Record<string, unknown> | string[];
11
12
  nodeEnv?: string;
12
13
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspack/cli",
3
- "version": "2.0.0-rc.1",
3
+ "version": "2.0.0-rc.3",
4
4
  "description": "CLI for rspack",
5
5
  "homepage": "https://rspack.rs",
6
6
  "bugs": "https://github.com/web-infra-dev/rspack/issues",
@@ -24,22 +24,23 @@
24
24
  },
25
25
  "files": [
26
26
  "bin",
27
+ "compiled",
27
28
  "dist"
28
29
  ],
29
30
  "devDependencies": {
30
31
  "@discoveryjs/json-ext": "^0.5.7",
31
- "@rslib/core": "0.20.3",
32
+ "@rslib/core": "0.21.0",
32
33
  "@rspack/dev-server": "2.0.0-rc.2",
33
34
  "cac": "^7.0.0",
34
35
  "concat-stream": "^2.0.0",
35
36
  "cross-env": "^10.1.0",
36
37
  "execa": "^5.1.1",
37
38
  "exit-hook": "^4.0.0",
38
- "pirates": "^4.0.7",
39
- "ts-node": "^10.9.2",
39
+ "jiti": "^2.6.1",
40
+ "prebundle": "^1.6.4",
40
41
  "typescript": "^6.0.2",
41
- "@rspack/core": "2.0.0-rc.1",
42
- "@rspack/test-tools": "2.0.0-rc.1"
42
+ "@rspack/test-tools": "2.0.0-rc.3",
43
+ "@rspack/core": "2.0.0-rc.3"
43
44
  },
44
45
  "peerDependencies": {
45
46
  "@rspack/core": "^2.0.0-0",
@@ -1 +0,0 @@
1
- export declare const crossImport: <T = any>(path: string) => Promise<T>;
@@ -1 +0,0 @@
1
- export declare const isEsmFile: (filePath: string) => boolean;
@@ -1,4 +0,0 @@
1
- declare const readPackageUp: (cwd?: string) => {
2
- type?: "module";
3
- } | null;
4
- export default readPackageUp;