@rslib/core 0.1.2 → 0.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/index.js CHANGED
@@ -128,10 +128,13 @@ function isPluginIncluded(pluginName, plugins) {
128
128
  return false;
129
129
  }));
130
130
  }
131
- function checkMFPlugin(config) {
131
+ function checkMFPlugin(config, sharedPlugins) {
132
132
  if ('mf' !== config.format) return true;
133
133
  // https://github.com/module-federation/core/blob/4e5c4b96ee45899f3ba5904b8927768980d5ad0e/packages/rsbuild-plugin/src/cli/index.ts#L17
134
- const added = isPluginIncluded('rsbuild:module-federation-enhanced', config.plugins);
134
+ const added = isPluginIncluded('rsbuild:module-federation-enhanced', [
135
+ ...sharedPlugins || [],
136
+ ...config.plugins || []
137
+ ]);
135
138
  if (!added) {
136
139
  __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.warn(`${__WEBPACK_EXTERNAL_MODULE__compiled_picocolors_index_js__["default"].green('format: "mf"')} should be used with ${__WEBPACK_EXTERNAL_MODULE__compiled_picocolors_index_js__["default"].blue('@module-federation/rsbuild-plugin')}", consider installing and adding it to plugins. Check the documentation (https://module-federation.io/guide/basic/rsbuild.html#rslib-module) to get started with "mf" output.`);
137
140
  process.exit(1);
@@ -150,6 +153,7 @@ function debounce(func, wait) {
150
153
  /**
151
154
  * Check if running in a TTY context
152
155
  */ const isTTY = (type = 'stdout')=>('stdin' === type ? process.stdin.isTTY : process.stdout.isTTY) && !process.env.CI;
156
+ const isIntermediateOutputFormat = (format)=>'cjs' === format || 'esm' === format;
153
157
  // setup the logger level
154
158
  if (process.env.DEBUG) __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.level = 'verbose';
155
159
  function initNodeEnv() {
@@ -166,7 +170,7 @@ function prepareCli() {
166
170
  // Some package managers automatically output a blank line, some do not.
167
171
  const { npm_execpath } = process.env;
168
172
  if (!npm_execpath || npm_execpath.includes('npx-cli.js') || npm_execpath.includes('.bun')) console.log();
169
- __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.greet(` Rslib v0.1.2\n`);
173
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.greet(` Rslib v0.1.4\n`);
170
174
  }
171
175
  const DEFAULT_CONFIG_NAME = 'rslib.config';
172
176
  const DEFAULT_CONFIG_EXTENSIONS = [
@@ -1519,9 +1523,11 @@ const composeExternalsWarnConfig = (format, ...externalsArray)=>{
1519
1523
  }
1520
1524
  };
1521
1525
  };
1526
+ const getAutoExternalDefaultValue = (format, autoExternal)=>autoExternal ?? isIntermediateOutputFormat(format);
1522
1527
  const composeAutoExternalConfig = (options)=>{
1523
- const { autoExternal, pkgJson, userExternals } = options;
1524
- if (!autoExternal) return {};
1528
+ const { format, pkgJson, userExternals } = options;
1529
+ const autoExternal = getAutoExternalDefaultValue(format, options.autoExternal);
1530
+ if (false === autoExternal) return {};
1525
1531
  if (!pkgJson) {
1526
1532
  __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.warn('autoExternal configuration will not be applied due to read package.json failed');
1527
1533
  return {};
@@ -1582,7 +1588,8 @@ function composeMinifyConfig(config) {
1582
1588
  toplevel: 'mf' !== format
1583
1589
  },
1584
1590
  format: {
1585
- comments: 'all'
1591
+ comments: 'some',
1592
+ preserve_annotations: true
1586
1593
  }
1587
1594
  }
1588
1595
  }
@@ -1807,6 +1814,9 @@ const composeFormatConfig = ({ format, bundle = true, umdName, pkgJson })=>{
1807
1814
  } : {
1808
1815
  type: 'umd'
1809
1816
  }
1817
+ },
1818
+ optimization: {
1819
+ nodeEnv: process.env.NODE_ENV
1810
1820
  }
1811
1821
  }
1812
1822
  }
@@ -2099,7 +2109,7 @@ const composeBundleConfig = (jsExtension, redirect, cssModulesAuto, bundle)=>{
2099
2109
  };
2100
2110
  };
2101
2111
  const composeDtsConfig = async (libConfig, dtsExtension)=>{
2102
- const { autoExternal, banner, footer } = libConfig;
2112
+ const { format, autoExternal, banner, footer } = libConfig;
2103
2113
  let { dts } = libConfig;
2104
2114
  if (false === dts || void 0 === dts) return {};
2105
2115
  // DTS default to bundleless whether js is bundle or not
@@ -2116,7 +2126,7 @@ const composeDtsConfig = async (libConfig, dtsExtension)=>{
2116
2126
  build: dts?.build,
2117
2127
  abortOnError: dts?.abortOnError,
2118
2128
  dtsExtension: dts?.autoExtension ? dtsExtension : '.d.ts',
2119
- autoExternal,
2129
+ autoExternal: getAutoExternalDefaultValue(format, autoExternal),
2120
2130
  banner: banner?.dts,
2121
2131
  footer: footer?.dts
2122
2132
  })
@@ -2201,14 +2211,14 @@ const composeExternalHelpersConfig = (externalHelpers, pkgJson)=>{
2201
2211
  }
2202
2212
  return defaultConfig;
2203
2213
  };
2204
- async function composeLibRsbuildConfig(config) {
2205
- checkMFPlugin(config);
2214
+ async function composeLibRsbuildConfig(config, sharedPlugins) {
2215
+ checkMFPlugin(config, sharedPlugins);
2206
2216
  // Get the absolute path of the root directory to align with Rsbuild's default behavior
2207
2217
  const rootPath = config.root ? getAbsolutePath(process.cwd(), config.root) : process.cwd();
2208
2218
  const pkgJson = readPackageJson(rootPath);
2209
2219
  const { compilerOptions } = await loadTsconfig(rootPath, config.source?.tsconfigPath);
2210
2220
  const cssModulesAuto = config.output?.cssModules?.auto ?? true;
2211
- const { format, shims, bundle = true, banner = {}, footer = {}, autoExtension = true, autoExternal = true, externalHelpers = false, redirect = {}, umdName } = config;
2221
+ const { format, shims, bundle = true, banner = {}, footer = {}, autoExtension = true, autoExternal, externalHelpers = false, redirect = {}, umdName } = config;
2212
2222
  const { rsbuildConfig: shimsConfig, enabledShims } = composeShimsConfig(format, shims);
2213
2223
  const formatConfig = composeFormatConfig({
2214
2224
  format: format,
@@ -2223,6 +2233,7 @@ async function composeLibRsbuildConfig(config) {
2223
2233
  const { config: targetConfig, target } = composeTargetConfig(config.output?.target, format);
2224
2234
  const syntaxConfig = composeSyntaxConfig(target, config?.syntax);
2225
2235
  const autoExternalConfig = composeAutoExternalConfig({
2236
+ format: format,
2226
2237
  autoExternal,
2227
2238
  pkgJson,
2228
2239
  userExternals: config.output?.externals
@@ -2241,13 +2252,13 @@ async function composeLibRsbuildConfig(config) {
2241
2252
  }
2242
2253
  async function composeCreateRsbuildConfig(rslibConfig) {
2243
2254
  const constantRsbuildConfig = await createConstantRsbuildConfig();
2244
- const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig;
2255
+ const { lib: libConfigsArray, plugins: sharedPlugins, ...sharedRsbuildConfig } = rslibConfig;
2245
2256
  if (!libConfigsArray) throw new Error(`Expect lib field to be an array, but got ${libConfigsArray}.`);
2246
2257
  const libConfigPromises = libConfigsArray.map(async (libConfig)=>{
2247
2258
  const userConfig = (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.mergeRsbuildConfig)(sharedRsbuildConfig, libConfig);
2248
2259
  // Merge the configuration of each environment based on the shared Rsbuild
2249
2260
  // configuration and Lib configuration in the settings.
2250
- const libRsbuildConfig = await composeLibRsbuildConfig(userConfig);
2261
+ const libRsbuildConfig = await composeLibRsbuildConfig(userConfig, sharedPlugins);
2251
2262
  // Reset certain fields because they will be completely overridden by the upcoming merge.
2252
2263
  // We don't want to retain them in the final configuration.
2253
2264
  // The reset process should occur after merging the library configuration.
@@ -2358,6 +2369,7 @@ async function build(config, options = {}) {
2358
2369
  const environments = await composeRsbuildEnvironments(config);
2359
2370
  const rsbuildInstance = await (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.createRsbuild)({
2360
2371
  rsbuildConfig: {
2372
+ plugins: config.plugins,
2361
2373
  environments: pruneEnvironments(environments, options.lib)
2362
2374
  }
2363
2375
  });
@@ -2368,19 +2380,43 @@ async function build(config, options = {}) {
2368
2380
  else await buildInstance.close();
2369
2381
  return rsbuildInstance;
2370
2382
  }
2371
- async function loadRslibConfig(options) {
2383
+ const getEnvDir = (cwd, envDir)=>{
2384
+ if (envDir) return __WEBPACK_EXTERNAL_MODULE_node_path__["default"].isAbsolute(envDir) ? envDir : __WEBPACK_EXTERNAL_MODULE_node_path__["default"].resolve(cwd, envDir);
2385
+ return cwd;
2386
+ };
2387
+ async function init(options) {
2372
2388
  const cwd = process.cwd();
2373
2389
  const root = options.root ? getAbsolutePath(cwd, options.root) : cwd;
2374
- return loadConfig({
2390
+ const envs = (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.loadEnv)({
2391
+ cwd: getEnvDir(root, options.envDir),
2392
+ mode: options.envMode
2393
+ });
2394
+ onBeforeRestart(envs.cleanup);
2395
+ const { content: config, filePath: configFilePath } = await loadConfig({
2375
2396
  cwd: root,
2376
2397
  path: options.config,
2377
2398
  envMode: options.envMode
2378
2399
  });
2400
+ config.source ||= {};
2401
+ config.source.define = {
2402
+ ...envs.publicVars,
2403
+ ...config.source.define
2404
+ };
2405
+ if (options.root) config.root = root;
2406
+ return {
2407
+ config,
2408
+ configFilePath,
2409
+ watchFiles: [
2410
+ configFilePath,
2411
+ ...envs.filePaths
2412
+ ]
2413
+ };
2379
2414
  }
2380
2415
  async function inspect(config, options = {}) {
2381
2416
  const environments = await composeRsbuildEnvironments(config);
2382
2417
  const rsbuildInstance = await (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.createRsbuild)({
2383
2418
  rsbuildConfig: {
2419
+ plugins: config.plugins,
2384
2420
  environments: pruneEnvironments(environments, options.lib)
2385
2421
  }
2386
2422
  });
@@ -2403,7 +2439,13 @@ async function initMFRsbuild(rslibConfig) {
2403
2439
  return;
2404
2440
  mfRsbuildConfig.config = changeEnvToDev(mfRsbuildConfig.config);
2405
2441
  const rsbuildInstance = await (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.createRsbuild)({
2406
- rsbuildConfig: mfRsbuildConfig.config
2442
+ rsbuildConfig: {
2443
+ ...mfRsbuildConfig.config,
2444
+ plugins: [
2445
+ ...rslibConfig.plugins || [],
2446
+ ...mfRsbuildConfig.config.plugins || []
2447
+ ]
2448
+ }
2407
2449
  });
2408
2450
  const devServer = await rsbuildInstance.startDevServer();
2409
2451
  onBeforeRestart(devServer.server.close);
@@ -2426,13 +2468,13 @@ function changeEnvToDev(rsbuildConfig) {
2426
2468
  });
2427
2469
  }
2428
2470
  const applyCommonOptions = (command)=>{
2429
- command.option('-c --config <config>', 'specify the configuration file, can be a relative or absolute path').option('-r --root <root>', 'specify the project root directory, can be an absolute path or a path relative to cwd').option('--env-mode <mode>', 'specify the env mode to load the `.env.[mode]` file');
2471
+ command.option('-c --config <config>', 'specify the configuration file, can be a relative or absolute path').option('-r --root <root>', 'specify the project root directory, can be an absolute path or a path relative to cwd').option('--env-mode <mode>', 'specify the env mode to load the `.env.[mode]` file').option('--env-dir <dir>', 'specify the directory to load `.env` files');
2430
2472
  };
2431
2473
  const repeatableOption = (value, previous)=>(previous ?? []).concat([
2432
2474
  value
2433
2475
  ]);
2434
2476
  function runCli() {
2435
- __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.name('rslib').usage('<command> [options]').version("0.1.2");
2477
+ __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.name('rslib').usage('<command> [options]').version("0.1.4");
2436
2478
  const buildCommand = __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.command('build');
2437
2479
  const inspectCommand = __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.command('inspect');
2438
2480
  const mfDevCommand = __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.command('mf dev');
@@ -2444,11 +2486,9 @@ function runCli() {
2444
2486
  buildCommand.option('--lib <id>', 'build the specified library (may be repeated)', repeatableOption).option('-w --watch', 'turn on watch mode, watch for changes and rebuild').description('build the library for production').action(async (options)=>{
2445
2487
  try {
2446
2488
  const cliBuild = async ()=>{
2447
- const { content: rslibConfig, filePath } = await loadRslibConfig(options);
2448
- await build(rslibConfig, options);
2449
- if (options.watch) watchFilesForRestart([
2450
- filePath
2451
- ], async ()=>{
2489
+ const { config, watchFiles } = await init(options);
2490
+ await build(config, options);
2491
+ if (options.watch) watchFilesForRestart(watchFiles, async ()=>{
2452
2492
  await cliBuild();
2453
2493
  });
2454
2494
  };
@@ -2462,8 +2502,8 @@ function runCli() {
2462
2502
  inspectCommand.description('inspect the Rsbuild / Rspack configs of Rslib projects').option('--lib <id>', 'inspect the specified library (may be repeated)', repeatableOption).option('--output <output>', 'specify inspect content output path', '.rsbuild').option('--verbose', 'show full function definitions in output').action(async (options)=>{
2463
2503
  try {
2464
2504
  // TODO: inspect should output Rslib's config
2465
- const { content: rslibConfig } = await loadRslibConfig(options);
2466
- await inspect(rslibConfig, {
2505
+ const { config } = await init(options);
2506
+ await inspect(config, {
2467
2507
  lib: options.lib,
2468
2508
  mode: options.mode,
2469
2509
  output: options.output,
@@ -2478,12 +2518,10 @@ function runCli() {
2478
2518
  mfDevCommand.description('start Rsbuild dev server of Module Federation format').action(async (options)=>{
2479
2519
  try {
2480
2520
  const cliMfDev = async ()=>{
2481
- const { content: rslibConfig, filePath } = await loadRslibConfig(options);
2521
+ const { config, watchFiles } = await init(options);
2482
2522
  // TODO: support lib option in mf dev server
2483
- await startMFDevServer(rslibConfig);
2484
- watchFilesForRestart([
2485
- filePath
2486
- ], async ()=>{
2523
+ await startMFDevServer(config);
2524
+ watchFilesForRestart(watchFiles, async ()=>{
2487
2525
  await cliMfDev();
2488
2526
  });
2489
2527
  };
@@ -2496,6 +2534,6 @@ function runCli() {
2496
2534
  });
2497
2535
  __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.parse();
2498
2536
  }
2499
- const src_rslib_entry_version = "0.1.2";
2537
+ const src_rslib_entry_version = "0.1.4";
2500
2538
  var __webpack_exports__logger = __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger;
2501
2539
  export { build, defineConfig, inspect, loadConfig, prepareCli, runCli, startMFDevServer, composeCreateRsbuildConfig as unstable_composeCreateRsbuildConfig, src_rslib_entry_version as version, __webpack_exports__logger as logger };
@@ -2,6 +2,7 @@ import type { RsbuildMode } from '@rsbuild/core';
2
2
  export type CommonOptions = {
3
3
  root?: string;
4
4
  config?: string;
5
+ envDir?: string;
5
6
  envMode?: string;
6
7
  lib?: string[];
7
8
  };
@@ -1,6 +1,7 @@
1
1
  import type { RslibConfig } from '../types';
2
2
  import type { CommonOptions } from './commands';
3
- export declare function loadRslibConfig(options: CommonOptions): Promise<{
4
- content: RslibConfig;
5
- filePath: string;
3
+ export declare function init(options: CommonOptions): Promise<{
4
+ config: RslibConfig;
5
+ configFilePath: string;
6
+ watchFiles: string[];
6
7
  }>;
@@ -1,5 +1,5 @@
1
1
  import { type EnvironmentConfig, type RsbuildConfig } from '@rsbuild/core';
2
- import type { AutoExternal, BannerAndFooter, LibConfig, PkgJson, RsbuildConfigEntry, RsbuildConfigWithLibInfo, RslibConfig, RslibConfigAsyncFn, RslibConfigExport, RslibConfigSyncFn } from './types';
2
+ import type { AutoExternal, BannerAndFooter, Format, LibConfig, PkgJson, RsbuildConfigEntry, RsbuildConfigWithLibInfo, RslibConfig, RslibConfigAsyncFn, RslibConfigExport, RslibConfigSyncFn } from './types';
3
3
  /**
4
4
  * This function helps you to autocomplete configuration types.
5
5
  * It accepts a Rslib config object, or a function that returns a config.
@@ -17,7 +17,8 @@ export declare function loadConfig({ cwd, path, envMode, }: {
17
17
  filePath: string;
18
18
  }>;
19
19
  export declare const composeAutoExternalConfig: (options: {
20
- autoExternal: AutoExternal;
20
+ format: Format;
21
+ autoExternal?: AutoExternal;
21
22
  pkgJson?: PkgJson;
22
23
  userExternals?: NonNullable<RsbuildConfig["output"]>["externals"];
23
24
  }) => RsbuildConfig;
@@ -67,7 +67,7 @@ export interface LibConfig extends RsbuildConfig {
67
67
  autoExtension?: boolean;
68
68
  /**
69
69
  * Whether to automatically externalize dependencies of different dependency types and do not bundle them.
70
- * @defaultValue `true`
70
+ * @defaultValue `true` when {@link format} is `cjs` or `esm`, `false` when {@link format} is `umd` or `mf`.
71
71
  * @see {@link https://lib.rsbuild.dev/config/lib/auto-external}
72
72
  */
73
73
  autoExternal?: AutoExternal;
@@ -90,13 +90,13 @@ export interface LibConfig extends RsbuildConfig {
90
90
  */
91
91
  externalHelpers?: boolean;
92
92
  /**
93
- * Inject content into the top of each JS, CSS or DTS file.
93
+ * Inject content into the top of each JavaScript, CSS or DTS file.
94
94
  * @defaultValue `{}`
95
95
  * @see {@link https://lib.rsbuild.dev/config/lib/banner}
96
96
  */
97
97
  banner?: BannerAndFooter;
98
98
  /**
99
- * Inject content into the bottom of each JS, CSS or DTS file.
99
+ * Inject content into the bottom of each JavaScript, CSS or DTS file.
100
100
  * @defaultValue `{}`
101
101
  * @see {@link https://lib.rsbuild.dev/config/lib/footer}
102
102
  */
@@ -1,6 +1,6 @@
1
1
  import type { RsbuildPlugins } from '@rsbuild/core';
2
2
  import color from 'picocolors';
3
- import type { LibConfig, PkgJson } from '../types';
3
+ import type { Format, LibConfig, PkgJson } from '../types';
4
4
  /**
5
5
  * Node.js built-in modules.
6
6
  * Copied from https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L12-L72
@@ -14,10 +14,11 @@ export declare const isEmptyObject: (obj: object) => boolean;
14
14
  export declare function pick<T, U extends keyof T>(obj: T, keys: ReadonlyArray<U>): Pick<T, U>;
15
15
  export declare function omit<T extends object, U extends keyof T>(obj: T, keysObj: Record<U, boolean>): Omit<T, keyof U>;
16
16
  export declare function isPluginIncluded(pluginName: string, plugins?: RsbuildPlugins): boolean;
17
- export declare function checkMFPlugin(config: LibConfig): boolean;
17
+ export declare function checkMFPlugin(config: LibConfig, sharedPlugins?: RsbuildPlugins): boolean;
18
18
  export declare function debounce<T extends (...args: any[]) => void>(func: T, wait: number): (...args: Parameters<T>) => void;
19
19
  /**
20
20
  * Check if running in a TTY context
21
21
  */
22
22
  export declare const isTTY: (type?: "stdin" | "stdout") => boolean;
23
+ export declare const isIntermediateOutputFormat: (format: Format) => boolean;
23
24
  export { color };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rslib/core",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "The Rsbuild-based library development tool.",
5
5
  "homepage": "https://lib.rsbuild.dev",
6
6
  "bugs": {
@@ -32,19 +32,19 @@
32
32
  "compiled"
33
33
  ],
34
34
  "dependencies": {
35
- "@rsbuild/core": "~1.1.7",
35
+ "@rsbuild/core": "~1.1.10",
36
36
  "tinyglobby": "^0.2.10",
37
- "rsbuild-plugin-dts": "0.1.2"
37
+ "rsbuild-plugin-dts": "0.1.4"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@types/fs-extra": "^11.0.4",
41
41
  "chokidar": "^4.0.1",
42
42
  "commander": "^12.1.0",
43
43
  "fs-extra": "^11.2.0",
44
- "memfs": "^4.14.1",
44
+ "memfs": "^4.15.0",
45
45
  "picocolors": "1.1.1",
46
46
  "prebundle": "1.2.5",
47
- "rslib": "npm:@rslib/core@0.1.1",
47
+ "rslib": "npm:@rslib/core@0.1.3",
48
48
  "rslog": "^1.2.3",
49
49
  "tsconfck": "3.1.4",
50
50
  "typescript": "^5.6.3",