@rsbuild/core 2.1.3 → 2.1.4

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.
package/dist/756.js CHANGED
@@ -3500,7 +3500,7 @@ function createPublicContext(context) {
3500
3500
  async function createContext(options, userConfig, logger) {
3501
3501
  let { cwd } = options, rootPath = userConfig.root ? ensureAbsolutePath(cwd, userConfig.root) : cwd, rsbuildConfig = await withDefaultConfig(rootPath, userConfig), cachePath = join(rootPath, 'node_modules', '.cache'), specifiedEnvironments = options.environment && options.environment.length > 0 ? options.environment : void 0;
3502
3502
  return {
3503
- version: "2.1.3",
3503
+ version: "2.1.4",
3504
3504
  rootPath,
3505
3505
  distPath: '',
3506
3506
  cachePath,
@@ -9874,7 +9874,12 @@ let DEFAULT_CONFIG_FILE_NAMES = [
9874
9874
  'rsbuild.config.mjs',
9875
9875
  'rsbuild.config.cts',
9876
9876
  'rsbuild.config.cjs'
9877
- ], getConfigExport = (module)=>module && 'object' == typeof module && 'default' in module ? module.default : module, tryFreshImport = async (configFileURL)=>{
9877
+ ], canReadExport = (module)=>null !== module && ('object' == typeof module || 'function' == typeof module), getConfigExport = (configModule, exportName, configFilePath)=>{
9878
+ if (!1 === exportName) return {};
9879
+ if ('default' === exportName) return canReadExport(configModule) && 'default' in configModule ? configModule.default : configModule;
9880
+ if (canReadExport(configModule) && Object.hasOwn(configModule, exportName)) return configModule[exportName];
9881
+ throw Error(`Cannot find export ${color.yellow(exportName)} in config file: ${color.dim(configFilePath)}`);
9882
+ }, tryFreshImport = async (configFileURL)=>{
9878
9883
  try {
9879
9884
  let { freshImport } = await import("./fresh-import.js");
9880
9885
  return await freshImport(configFileURL);
@@ -9884,15 +9889,32 @@ let DEFAULT_CONFIG_FILE_NAMES = [
9884
9889
  }, loadConfigWithNative = async (configFilePath)=>{
9885
9890
  let configFileURL = pathToFileURL(configFilePath).href, freshImportResult = await tryFreshImport(configFileURL);
9886
9891
  return freshImportResult ? {
9887
- configExport: getConfigExport(freshImportResult.result),
9892
+ configModule: freshImportResult.result,
9888
9893
  dependencies: freshImportResult.dependencies.sort()
9889
9894
  } : {
9890
- configExport: getConfigExport(await import(`${configFileURL}?t=${Date.now()}`)),
9895
+ configModule: await import(`${configFileURL}?t=${Date.now()}`),
9896
+ dependencies: []
9897
+ };
9898
+ }, loadConfigWithJiti = async (configFilePath, exportName)=>{
9899
+ let { createJiti } = await import("../compiled/jiti/lib/jiti.mjs"), jiti = createJiti(import.meta.filename, {
9900
+ moduleCache: !1,
9901
+ interopDefault: !0,
9902
+ nativeModules: [
9903
+ "typescript"
9904
+ ]
9905
+ });
9906
+ return 'default' === exportName ? {
9907
+ configExport: await jiti.import(configFilePath, {
9908
+ default: !0
9909
+ }),
9910
+ dependencies: []
9911
+ } : {
9912
+ configExport: getConfigExport(await jiti.import(configFilePath), exportName, configFilePath),
9891
9913
  dependencies: []
9892
9914
  };
9893
9915
  };
9894
- async function loadConfig_loadConfig({ cwd = process.cwd(), path, configFileNames, envMode, meta, loader = 'auto', command } = {}) {
9895
- let configExport, configFilePath = ((root, customConfig, configFileNames = DEFAULT_CONFIG_FILE_NAMES)=>{
9916
+ async function loadConfig_loadConfig({ cwd = process.cwd(), path, configFileNames, envMode, meta, loader = 'auto', command, exportName = 'default' } = {}) {
9917
+ let loadedConfig, configFilePath = ((root, customConfig, configFileNames = DEFAULT_CONFIG_FILE_NAMES)=>{
9896
9918
  if (customConfig) {
9897
9919
  let customConfigPath = external_node_path_isAbsolute(customConfig) ? customConfig : join(root, customConfig);
9898
9920
  if (node_fs.existsSync(customConfigPath)) return customConfigPath;
@@ -9909,30 +9931,29 @@ async function loadConfig_loadConfig({ cwd = process.cwd(), path, configFileName
9909
9931
  filePath: configFilePath,
9910
9932
  dependencies: []
9911
9933
  };
9912
- let dependencies = [], applyMetaInfo = (config)=>(config._privateMeta = {
9934
+ let applyMetaInfo = (config)=>(config._privateMeta = {
9913
9935
  configFilePath
9914
9936
  }, config);
9915
- if ('native' === loader || 'auto' === loader && (process.features.typescript || process.versions.bun || process.versions.deno) || /\.(?:js|mjs|cjs)$/.test(configFilePath)) try {
9916
- ({ configExport, dependencies } = await loadConfigWithNative(configFilePath));
9917
- } catch (err) {
9918
- let errorMessage = `Failed to load file with native loader: ${color.dim(configFilePath)}`;
9919
- if ('native' === loader) throw src_logger.error(errorMessage), err;
9920
- src_logger.debug(`${errorMessage}, fallback to jiti.`), src_logger.debug(err);
9921
- }
9922
- if (void 0 === configExport) try {
9923
- let { createJiti } = await import("../compiled/jiti/lib/jiti.mjs"), jiti = createJiti(import.meta.filename, {
9924
- moduleCache: !1,
9925
- interopDefault: !0,
9926
- nativeModules: [
9927
- "typescript"
9928
- ]
9929
- });
9930
- configExport = await jiti.import(configFilePath, {
9931
- default: !0
9937
+ if ('native' === loader || 'auto' === loader && (process.features.typescript || process.versions.bun || process.versions.deno) || /\.(?:js|mjs|cjs)$/.test(configFilePath)) {
9938
+ let result;
9939
+ try {
9940
+ result = await loadConfigWithNative(configFilePath);
9941
+ } catch (err) {
9942
+ let errorMessage = `Failed to load file with native loader: ${color.dim(configFilePath)}`;
9943
+ if ('native' === loader) throw src_logger.error(errorMessage), err;
9944
+ src_logger.debug(`${errorMessage}, fallback to jiti.`), src_logger.debug(err);
9945
+ }
9946
+ result && (loadedConfig = {
9947
+ configExport: getConfigExport(result.configModule, exportName, configFilePath),
9948
+ dependencies: result.dependencies
9932
9949
  });
9950
+ }
9951
+ if (!loadedConfig) try {
9952
+ loadedConfig = await loadConfigWithJiti(configFilePath, exportName);
9933
9953
  } catch (err) {
9934
9954
  throw src_logger.error(`Failed to load file with jiti: ${color.dim(configFilePath)}`), err;
9935
9955
  }
9956
+ let { configExport, dependencies } = loadedConfig;
9936
9957
  if ('function' == typeof configExport) {
9937
9958
  let nodeEnv = process.env.NODE_ENV || '', configParams = {
9938
9959
  env: nodeEnv,
@@ -9940,14 +9961,14 @@ async function loadConfig_loadConfig({ cwd = process.cwd(), path, configFileName
9940
9961
  envMode: envMode || nodeEnv,
9941
9962
  meta
9942
9963
  }, result = await configExport(configParams);
9943
- if (void 0 === result) throw Error(`${color.dim('[rsbuild:loadConfig]')} The config function must return a config object.`);
9964
+ if (void 0 === result) throw Error('The config function must return a config object.');
9944
9965
  return {
9945
9966
  content: applyMetaInfo(result),
9946
9967
  filePath: configFilePath,
9947
9968
  dependencies
9948
9969
  };
9949
9970
  }
9950
- if (!isObject(configExport)) throw Error(`${color.dim('[rsbuild:loadConfig]')} The config must be an object or a function that returns an object, get ${color.yellow(configExport)}`);
9971
+ if (!isObject(configExport)) throw Error(`The config must be an object or a function that returns an object, get ${color.yellow(String(configExport))}`);
9951
9972
  return src_logger.debug('configuration loaded from:', configFilePath), {
9952
9973
  content: applyMetaInfo(configExport),
9953
9974
  filePath: configFilePath,
@@ -10086,7 +10107,7 @@ let applyServerOptions = (command)=>{
10086
10107
  };
10087
10108
  function setupCommands(argv) {
10088
10109
  let cli = ((name = "")=>new CAC(name))('rsbuild');
10089
- cli.version("2.1.3"), cli.option('--base <base>', 'Set the base path of the server').option('-c, --config <config>', 'Set the configuration file (relative or absolute path)').option('--config-loader <loader>', 'Set the config file loader (auto | jiti | native)', {
10110
+ cli.version("2.1.4"), cli.option('--base <base>', 'Set the base path of the server').option('-c, --config <config>', 'Set the configuration file (relative or absolute path)').option('--config-loader <loader>', 'Set the config file loader (auto | jiti | native)', {
10090
10111
  default: 'auto'
10091
10112
  }).option('--dist-path <dir>', 'Set the root directory of output files').option('--source-map', 'Enable source map').option('--env-dir <dir>', 'Set the directory for loading `.env` files').option('--env-mode <mode>', 'Set the env mode to load the `.env.[mode]` file').option('--environment <name>', 'Set the environment name(s) to build', {
10092
10113
  type: [
@@ -10150,7 +10171,7 @@ function setupCommands(argv) {
10150
10171
  }
10151
10172
  function showGreeting() {
10152
10173
  let { npm_execpath, npm_lifecycle_event, NODE_RUN_SCRIPT_NAME } = process.env, isBun = npm_execpath?.includes('.bun');
10153
- src_logger.greet(`${'npx' === npm_lifecycle_event || isBun || NODE_RUN_SCRIPT_NAME ? '\n' : ''}Rsbuild v2.1.3\n`);
10174
+ src_logger.greet(`${'npx' === npm_lifecycle_event || isBun || NODE_RUN_SCRIPT_NAME ? '\n' : ''}Rsbuild v2.1.4\n`);
10154
10175
  }
10155
10176
  function setupLogLevel(argv) {
10156
10177
  if (argv.length <= 3) return;
@@ -10172,5 +10193,5 @@ function runCLI({ argv = process.argv } = {}) {
10172
10193
  src_logger.error('Failed to start Rsbuild CLI.'), src_logger.error(err), process.exit(1);
10173
10194
  }
10174
10195
  }
10175
- let src_version = "2.1.3";
10196
+ let src_version = "2.1.4";
10176
10197
  export { PLUGIN_CSS_NAME, PLUGIN_SWC_NAME, core_rspack as rspack, createRsbuild, defaultAllowedOrigins, defineConfig, ensureAssetPrefix, loadConfig_loadConfig as loadConfig, loadEnv, logger_createLogger as createLogger, mergeRsbuildConfig, mrmime_lookup, runCLI, src_logger as logger, src_version as version };
@@ -39,11 +39,16 @@ export type LoadConfigOptions = {
39
39
  * The command passed to the config function.
40
40
  * @default process.argv[2]
41
41
  */ command?: string;
42
+ /**
43
+ * The export name to read from the config file.
44
+ * Set to `false` to execute the config file without reading exports.
45
+ * @default 'default'
46
+ */ exportName?: string | false;
42
47
  };
43
- export type LoadConfigResult = {
48
+ export type LoadConfigResult<Config = RsbuildConfig> = {
44
49
  /**
45
50
  * The loaded configuration object.
46
- */ content: RsbuildConfig;
51
+ */ content: Config;
47
52
  /**
48
53
  * The path to the loaded configuration file.
49
54
  * Return `null` if the configuration file is not found.
@@ -61,4 +66,4 @@ export declare function defineConfig(config: RsbuildConfigSyncFn): RsbuildConfig
61
66
  export declare function defineConfig(config: RsbuildConfigAsyncFn): RsbuildConfigAsyncFn;
62
67
  export declare function defineConfig(config: RsbuildConfigDefinition): RsbuildConfigDefinition;
63
68
  export type ConfigLoader = 'auto' | 'jiti' | 'native';
64
- export declare function loadConfig({ cwd, path, configFileNames, envMode, meta, loader, command }?: LoadConfigOptions): Promise<LoadConfigResult>;
69
+ export declare function loadConfig<Config = RsbuildConfig>({ cwd, path, configFileNames, envMode, meta, loader, command, exportName }?: LoadConfigOptions): Promise<LoadConfigResult<Config>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/core",
3
- "version": "2.1.3",
3
+ "version": "2.1.4",
4
4
  "description": "The Rspack-based build tool.",
5
5
  "homepage": "https://rsbuild.rs",
6
6
  "bugs": {