@rslib/core 0.0.16 → 0.0.17

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/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # Rslib
6
6
 
7
- Rslib is a library build tool powered by [Rsbuild](https://rsbuild.dev). It allows library developers to leverage the knowledge and ecosystem of webpack and Rspack.
7
+ Rslib is a library development tool powered by [Rsbuild](https://rsbuild.dev). It allows library developers to leverage the knowledge and ecosystem of webpack and Rspack.
8
8
 
9
9
  ## Documentation
10
10
 
package/dist/index.js CHANGED
@@ -3,8 +3,8 @@ import * as __WEBPACK_EXTERNAL_MODULE_node_fs__ from "node:fs";
3
3
  import * as __WEBPACK_EXTERNAL_MODULE_node_fs_promises__ from "node:fs/promises";
4
4
  import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
5
5
  import * as __WEBPACK_EXTERNAL_MODULE__compiled_picocolors_index_js__ from "../compiled/picocolors/index.js";
6
- import * as __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__ from "../compiled/commander/index.js";
7
6
  import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core__ from "@rsbuild/core";
7
+ import * as __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__ from "../compiled/commander/index.js";
8
8
  import * as __WEBPACK_EXTERNAL_MODULE_tinyglobby__ from "tinyglobby";
9
9
  import * as __WEBPACK_EXTERNAL_MODULE_node_module__ from "node:module";
10
10
  import * as __WEBPACK_EXTERNAL_MODULE_module__ from "module";
@@ -150,7 +150,7 @@ function prepareCli() {
150
150
  // Some package managers automatically output a blank line, some do not.
151
151
  const { npm_execpath } = process.env;
152
152
  if (!npm_execpath || npm_execpath.includes('npx-cli.js') || npm_execpath.includes('.bun')) console.log();
153
- __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.greet(` Rslib v0.0.16\n`);
153
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.greet(` Rslib v0.0.17\n`);
154
154
  }
155
155
  const DEFAULT_CONFIG_NAME = 'rslib.config';
156
156
  const DEFAULT_CONFIG_EXTENSIONS = [
@@ -392,10 +392,6 @@ const LATEST_TARGET_VERSIONS = {
392
392
  }
393
393
  };
394
394
  const calcEsnextBrowserslistByTarget = (target)=>{
395
- if (!target) return [
396
- ...LATEST_TARGET_VERSIONS.node,
397
- ...LATEST_TARGET_VERSIONS.web
398
- ];
399
395
  if ('node' === target) return LATEST_TARGET_VERSIONS.node;
400
396
  return LATEST_TARGET_VERSIONS.web;
401
397
  };
@@ -1565,6 +1561,7 @@ async function createConstantRsbuildConfig() {
1565
1561
  }
1566
1562
  },
1567
1563
  output: {
1564
+ target: 'node',
1568
1565
  filenameHash: false,
1569
1566
  distPath: {
1570
1567
  js: './'
@@ -1783,7 +1780,7 @@ const composeAutoExtensionConfig = (config, autoExtension, pkgJson)=>{
1783
1780
  dtsExtension
1784
1781
  };
1785
1782
  };
1786
- const composeSyntaxConfig = (syntax, target)=>{
1783
+ const composeSyntaxConfig = (target, syntax)=>{
1787
1784
  // Defaults to ESNext, Rslib will assume all of the latest JavaScript and CSS features are supported.
1788
1785
  if (syntax) return {
1789
1786
  tools: {
@@ -1933,34 +1930,40 @@ const composeDtsConfig = async (libConfig, dtsExtension)=>{
1933
1930
  ]
1934
1931
  };
1935
1932
  };
1936
- const composeTargetConfig = (target = 'web')=>{
1933
+ const composeTargetConfig = (target = 'node')=>{
1937
1934
  switch(target){
1938
1935
  case 'web':
1939
1936
  return {
1940
- tools: {
1941
- rspack: {
1942
- target: [
1943
- 'web'
1944
- ]
1937
+ config: {
1938
+ tools: {
1939
+ rspack: {
1940
+ target: [
1941
+ 'web'
1942
+ ]
1943
+ }
1945
1944
  }
1946
- }
1945
+ },
1946
+ target: 'web'
1947
1947
  };
1948
1948
  case 'node':
1949
1949
  return {
1950
- tools: {
1951
- rspack: {
1952
- target: [
1953
- 'node'
1954
- ]
1950
+ config: {
1951
+ tools: {
1952
+ rspack: {
1953
+ target: [
1954
+ 'node'
1955
+ ]
1956
+ }
1957
+ },
1958
+ output: {
1959
+ // When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
1960
+ // Simply override the built-in modules to make them external.
1961
+ // https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
1962
+ externals: nodeBuiltInModules,
1963
+ target: 'node'
1955
1964
  }
1956
1965
  },
1957
- output: {
1958
- // When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
1959
- // Simply override the built-in modules to make them external.
1960
- // https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
1961
- externals: nodeBuiltInModules,
1962
- target: 'node'
1963
- }
1966
+ target: 'node'
1964
1967
  };
1965
1968
  // TODO: Support `neutral` target, however Rsbuild don't list it as an option in the target field.
1966
1969
  // case 'neutral':
@@ -2021,8 +2024,8 @@ async function composeLibRsbuildConfig(config, configPath) {
2021
2024
  const externalsConfig = composeExternalsConfig(format, config.output?.externals);
2022
2025
  const { config: autoExtensionConfig, jsExtension, dtsExtension } = composeAutoExtensionConfig(config, autoExtension, pkgJson);
2023
2026
  const bundleConfig = composeBundleConfig(jsExtension, redirect, cssModulesAuto, bundle);
2024
- const targetConfig = composeTargetConfig(config.output?.target);
2025
- const syntaxConfig = composeSyntaxConfig(config?.syntax, config.output?.target);
2027
+ const { config: targetConfig, target } = composeTargetConfig(config.output?.target);
2028
+ const syntaxConfig = composeSyntaxConfig(target, config?.syntax);
2026
2029
  const autoExternalConfig = composeAutoExternalConfig({
2027
2030
  autoExternal,
2028
2031
  pkgJson,
@@ -2084,7 +2087,7 @@ async function composeCreateRsbuildConfig(rslibConfig, path) {
2084
2087
  const composedRsbuildConfig = await Promise.all(libConfigPromises);
2085
2088
  return composedRsbuildConfig;
2086
2089
  }
2087
- async function initRsbuild(rslibConfig) {
2090
+ async function composeRsbuildEnvironments(rslibConfig) {
2088
2091
  const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig);
2089
2092
  const environments = {};
2090
2093
  const formatCount = rsbuildConfigObject.reduce((acc, { format })=>{
@@ -2102,31 +2105,72 @@ async function initRsbuild(rslibConfig) {
2102
2105
  const currentFormatIndex = formatIndex[format]++;
2103
2106
  environments[1 === currentFormatCount ? format : `${format}${currentFormatIndex}`] = config;
2104
2107
  }
2105
- return (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.createRsbuild)({
2108
+ return environments;
2109
+ }
2110
+ const pruneEnvironments = (environments, libs)=>{
2111
+ if (!libs) return environments;
2112
+ return Object.fromEntries(Object.entries(environments).filter(([name])=>libs.includes(name)));
2113
+ };
2114
+ async function build(config, options) {
2115
+ const environments = await composeRsbuildEnvironments(config);
2116
+ const rsbuildInstance = await (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.createRsbuild)({
2106
2117
  rsbuildConfig: {
2107
- environments
2118
+ environments: pruneEnvironments(environments, options?.lib)
2108
2119
  }
2109
2120
  });
2110
- }
2111
- async function build(config, options) {
2112
- const rsbuildInstance = await initRsbuild(config);
2113
2121
  await rsbuildInstance.build({
2114
2122
  watch: options?.watch
2115
2123
  });
2116
2124
  return rsbuildInstance;
2117
2125
  }
2126
+ async function startMFDevServer(config) {
2127
+ const rsbuildInstance = await initMFRsbuild(config);
2128
+ return rsbuildInstance;
2129
+ }
2130
+ async function initMFRsbuild(rslibConfig) {
2131
+ const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig);
2132
+ const mfRsbuildConfig = rsbuildConfigObject.find((config)=>'mf' === config.format);
2133
+ if (!mfRsbuildConfig) // no mf format, return.
2134
+ return;
2135
+ mfRsbuildConfig.config = changeEnvToDev(mfRsbuildConfig.config);
2136
+ const rsbuildInstance = await (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.createRsbuild)({
2137
+ rsbuildConfig: mfRsbuildConfig.config
2138
+ });
2139
+ await rsbuildInstance.startDevServer();
2140
+ return rsbuildInstance;
2141
+ }
2142
+ function changeEnvToDev(rsbuildConfig) {
2143
+ return (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.mergeRsbuildConfig)(rsbuildConfig, {
2144
+ mode: 'development',
2145
+ dev: {
2146
+ writeToDisk: true
2147
+ },
2148
+ tools: {
2149
+ rspack: {
2150
+ optimization: {
2151
+ nodeEnv: 'development'
2152
+ }
2153
+ }
2154
+ }
2155
+ });
2156
+ }
2118
2157
  const applyCommonOptions = (command)=>{
2119
2158
  command.option('-c --config <config>', 'specify the configuration file, can be a relative or absolute path').option('--env-mode <mode>', 'specify the env mode to load the `.env.[mode]` file');
2120
2159
  };
2160
+ const repeatableOption = (value, previous)=>(previous ?? []).concat([
2161
+ value
2162
+ ]);
2121
2163
  function runCli() {
2122
- __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.name('rslib').usage('<command> [options]').version("0.0.16");
2164
+ __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.name('rslib').usage('<command> [options]').version("0.0.17");
2123
2165
  const buildCommand = __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.command('build');
2124
2166
  const inspectCommand = __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.command('inspect');
2167
+ const mfDevCommand = __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.command('mf dev');
2125
2168
  [
2126
2169
  buildCommand,
2127
- inspectCommand
2170
+ inspectCommand,
2171
+ mfDevCommand
2128
2172
  ].forEach(applyCommonOptions);
2129
- buildCommand.option('-w --watch', 'turn on watch mode, watch for changes and rebuild').description('build the library for production').action(async (options)=>{
2173
+ buildCommand.option('--lib <name>', '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)=>{
2130
2174
  try {
2131
2175
  const rslibConfig = await loadConfig({
2132
2176
  path: options.config,
@@ -2139,14 +2183,19 @@ function runCli() {
2139
2183
  process.exit(1);
2140
2184
  }
2141
2185
  });
2142
- inspectCommand.description('inspect the Rsbuild / Rspack configs of Rslib projects').option('--output <output>', 'specify inspect content output path', '.rsbuild').option('--verbose', 'show full function definitions in output').action(async (options)=>{
2186
+ inspectCommand.description('inspect the Rsbuild / Rspack configs of Rslib projects').option('--lib <name>', '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)=>{
2143
2187
  try {
2144
2188
  // TODO: inspect should output Rslib's config
2145
2189
  const rslibConfig = await loadConfig({
2146
2190
  path: options.config,
2147
2191
  envMode: options.envMode
2148
2192
  });
2149
- const rsbuildInstance = await initRsbuild(rslibConfig);
2193
+ const environments = await composeRsbuildEnvironments(rslibConfig);
2194
+ const rsbuildInstance = await (0, __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.createRsbuild)({
2195
+ rsbuildConfig: {
2196
+ environments: pruneEnvironments(environments, options.lib)
2197
+ }
2198
+ });
2150
2199
  await rsbuildInstance.inspectConfig({
2151
2200
  mode: options.mode,
2152
2201
  verbose: options.verbose,
@@ -2159,8 +2208,21 @@ function runCli() {
2159
2208
  process.exit(1);
2160
2209
  }
2161
2210
  });
2211
+ mfDevCommand.description('start Rsbuild dev server of Module Federation format').action(async (options)=>{
2212
+ try {
2213
+ const rslibConfig = await loadConfig({
2214
+ path: options.config,
2215
+ envMode: options.envMode
2216
+ });
2217
+ await startMFDevServer(rslibConfig);
2218
+ } catch (err) {
2219
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.error('Failed to start mf dev.');
2220
+ __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger.error(err);
2221
+ process.exit(1);
2222
+ }
2223
+ });
2162
2224
  __WEBPACK_EXTERNAL_MODULE__compiled_commander_index_js__.program.parse();
2163
2225
  }
2164
- const src_version = "0.0.16";
2226
+ const src_version = "0.0.17";
2165
2227
  var __webpack_exports__logger = __WEBPACK_EXTERNAL_MODULE__compiled_rslog_index_js__.logger;
2166
2228
  export { build, defineConfig, loadConfig, prepareCli, runCli, composeCreateRsbuildConfig as unstable_composeCreateRsbuildConfig, src_version as version, __webpack_exports__logger as logger };
@@ -1,4 +1,4 @@
1
- import type { RsbuildInstance } from '@rsbuild/core';
1
+ import { type RsbuildInstance } from '@rsbuild/core';
2
2
  import type { BuildOptions } from './cli/commands';
3
3
  import type { RslibConfig } from './types/config';
4
4
  export declare function build(config: RslibConfig, options?: BuildOptions): Promise<RsbuildInstance>;
@@ -1,7 +1,8 @@
1
- import type { RsbuildMode } from '@rsbuild/core';
1
+ import { type RsbuildMode } from '@rsbuild/core';
2
2
  export type CommonOptions = {
3
3
  config?: string;
4
4
  envMode?: string;
5
+ lib?: string[];
5
6
  };
6
7
  export type BuildOptions = CommonOptions & {
7
8
  watch?: boolean;
@@ -1,4 +1,4 @@
1
- import { type RsbuildConfig, type RsbuildInstance } from '@rsbuild/core';
1
+ import { type EnvironmentConfig, type RsbuildConfig } from '@rsbuild/core';
2
2
  import type { AutoExternal, BannerAndFooter, Format, LibConfig, PkgJson, RslibConfig, RslibConfigAsyncFn, RslibConfigExport, RslibConfigSyncFn } from './types';
3
3
  /**
4
4
  * This function helps you to autocomplete configuration types.
@@ -27,4 +27,5 @@ export declare function composeCreateRsbuildConfig(rslibConfig: RslibConfig, pat
27
27
  format: Format;
28
28
  config: RsbuildConfig;
29
29
  }[]>;
30
- export declare function initRsbuild(rslibConfig: RslibConfig): Promise<RsbuildInstance>;
30
+ export declare function composeRsbuildEnvironments(rslibConfig: RslibConfig): Promise<Record<string, EnvironmentConfig>>;
31
+ export declare const pruneEnvironments: (environments: Record<string, EnvironmentConfig>, libs?: string[]) => Record<string, EnvironmentConfig>;
@@ -0,0 +1,3 @@
1
+ import type { RsbuildInstance } from '@rsbuild/core';
2
+ import type { RslibConfig } from './types';
3
+ export declare function startMFDevServer(config: RslibConfig): Promise<RsbuildInstance | undefined>;
@@ -11,6 +11,7 @@ export type Dts = (Pick<PluginDtsOptions, 'bundle' | 'distPath' | 'abortOnError'
11
11
  }) | boolean;
12
12
  export type AutoExternal = boolean | {
13
13
  dependencies?: boolean;
14
+ optionalDependencies?: boolean;
14
15
  devDependencies?: boolean;
15
16
  peerDependencies?: boolean;
16
17
  };
@@ -33,18 +34,79 @@ export type Redirect = {
33
34
  style?: boolean;
34
35
  };
35
36
  export interface LibConfig extends RsbuildConfig {
36
- bundle?: boolean;
37
+ /**
38
+ * Output format for the generated JavaScript files.
39
+ * @default undefined
40
+ */
37
41
  format?: Format;
42
+ /**
43
+ * Whether to bundle the library.
44
+ * @default true
45
+ */
46
+ bundle?: boolean;
47
+ /**
48
+ * Whether to automatically set the file extension based on the `format` option in the JavaScript output files.
49
+ * @default true
50
+ */
38
51
  autoExtension?: boolean;
52
+ /**
53
+ * Whether to automatically externalize dependencies of different dependency types and do not bundle them.
54
+ * @default true
55
+ */
39
56
  autoExternal?: AutoExternal;
57
+ /**
58
+ * Configure the redirect of the import paths, applicable when `bundle: false`.
59
+ * @default {}
60
+ */
40
61
  redirect?: Redirect;
41
- /** Support esX and browserslist query */
62
+ /**
63
+ * Configure the syntax to which JavaScript and CSS will be downgraded.
64
+ * Support ECMAScript version and browserslist query.
65
+ * @default 'esnext'
66
+ */
42
67
  syntax?: Syntax;
68
+ /**
69
+ * Whether to import SWC helper functions from `@swc/helpers` instead of inlining them.
70
+ * @default false
71
+ */
43
72
  externalHelpers?: boolean;
73
+ /**
74
+ * Inject content into the top of each JS, CSS or DTS file.
75
+ * @default {}
76
+ */
44
77
  banner?: BannerAndFooter;
78
+ /**
79
+ * Inject content into the bottom of each JS, CSS or DTS file.
80
+ * @default {}
81
+ */
45
82
  footer?: BannerAndFooter;
83
+ /**
84
+ * Configure the shims for CommonJS and ESM output.
85
+ *
86
+ * @default
87
+ * ```js
88
+ * const defaultShims = {
89
+ * cjs: {
90
+ * 'import.meta.url': true,
91
+ * },
92
+ * esm: {
93
+ * __filename: false,
94
+ * __dirname: false,
95
+ * require: false,
96
+ * },
97
+ * };
98
+ * ```
99
+ */
46
100
  shims?: Shims;
101
+ /**
102
+ * Configure the generation of the TypeScript declaration files.
103
+ * @default false
104
+ */
47
105
  dts?: Dts;
106
+ /**
107
+ * The export name of the UMD bundle.
108
+ * @default undefined
109
+ */
48
110
  umdName?: string;
49
111
  }
50
112
  export type LibOnlyConfig = Omit<LibConfig, keyof RsbuildConfig>;
@@ -10,4 +10,4 @@ export declare const LATEST_TARGET_VERSIONS: Record<NonNullable<RsbuildConfigOut
10
10
  */
11
11
  export declare const ESX_TO_BROWSERSLIST: Record<FixedEcmaVersions, Record<string, string | string[]>> & Record<LatestEcmaVersions, (target: RsbuildConfigOutputTarget) => string[]>;
12
12
  export declare function transformSyntaxToRspackTarget(syntax: Syntax): Rspack.Configuration['target'];
13
- export declare function transformSyntaxToBrowserslist(syntax: Syntax, target?: NonNullable<RsbuildConfig['output']>['target']): NonNullable<NonNullable<RsbuildConfig['output']>['overrideBrowserslist']>;
13
+ export declare function transformSyntaxToBrowserslist(syntax: Syntax, target: NonNullable<RsbuildConfig['output']>['target']): NonNullable<NonNullable<RsbuildConfig['output']>['overrideBrowserslist']>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rslib/core",
3
- "version": "0.0.16",
4
- "description": "The Rspack-based library build tool.",
3
+ "version": "0.0.17",
4
+ "description": "The Rsbuild-based library development tool.",
5
5
  "homepage": "https://lib.rsbuild.dev",
6
6
  "bugs": {
7
7
  "url": "https://github.com/web-infra-dev/rslib/issues"
@@ -32,9 +32,9 @@
32
32
  "compiled"
33
33
  ],
34
34
  "dependencies": {
35
- "@rsbuild/core": "~1.0.19",
35
+ "@rsbuild/core": "~1.1.0",
36
36
  "tinyglobby": "^0.2.10",
37
- "rsbuild-plugin-dts": "0.0.16"
37
+ "rsbuild-plugin-dts": "0.0.17"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@rspack/core": "1.0.8",
@@ -44,7 +44,7 @@
44
44
  "memfs": "^4.14.0",
45
45
  "picocolors": "1.1.1",
46
46
  "prebundle": "1.2.5",
47
- "rslib": "npm:@rslib/core@0.0.15",
47
+ "rslib": "npm:@rslib/core@0.0.16",
48
48
  "rslog": "^1.2.3",
49
49
  "tsconfck": "3.1.4",
50
50
  "typescript": "^5.6.3",