@rslib/core 0.0.3 → 0.0.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
@@ -473,6 +473,25 @@ const readPackageJson = (rootPath)=>{
473
473
  }
474
474
  };
475
475
  const isObject = (obj)=>Object.prototype.toString.call(obj) === '[object Object]';
476
+ function omitDeep(obj, keys) {
477
+ if (typeof obj === 'string' || typeof obj !== 'object' || obj === null) return obj;
478
+ if (Array.isArray(obj)) {
479
+ return obj.map((item)=>omitDeep(item, keys));
480
+ }
481
+ const clone = {};
482
+ for(const property in obj){
483
+ if (keys.includes(property)) {
484
+ continue;
485
+ }
486
+ const value = obj[property];
487
+ if (value && typeof value === 'object') {
488
+ clone[property] = omitDeep(value, keys);
489
+ } else {
490
+ clone[property] = value;
491
+ }
492
+ }
493
+ return clone;
494
+ }
476
495
 
477
496
 
478
497
  ;// CONCATENATED MODULE: ./src/utils/logger.ts
@@ -525,7 +544,7 @@ function prepareCli() {
525
544
  if (!npm_execpath || npm_execpath.includes('npx-cli.js') || npm_execpath.includes('.bun')) {
526
545
  console.log();
527
546
  }
528
- dist_logger.greet(` ${`Rslib v${"0.0.3"}`}\n`);
547
+ dist_logger.greet(` ${`Rslib v${"0.0.4"}`}\n`);
529
548
  }
530
549
 
531
550
  ;// CONCATENATED MODULE: external "../compiled/commander/index.js"
@@ -766,6 +785,87 @@ async function loadConfig({ cwd = process.cwd(), path, envMode }) {
766
785
  });
767
786
  return content;
768
787
  }
788
+ const composeExternalsWarnConfig = (format, ...externalsArray)=>{
789
+ if (format !== 'esm') {
790
+ return {};
791
+ }
792
+ const externals = [];
793
+ for (const e of externalsArray.filter(Boolean)){
794
+ if (Array.isArray(e)) {
795
+ externals.push(...e);
796
+ } else {
797
+ // @ts-ignore
798
+ externals.push(e);
799
+ }
800
+ }
801
+ // Match logic is derived from https://github.com/webpack/webpack/blob/94aba382eccf3de1004d235045d4462918dfdbb7/lib/ExternalModuleFactoryPlugin.js#L166-L293.
802
+ const matchUserExternals = (externals, request, callback)=>{
803
+ if (typeof externals === 'string') {
804
+ if (externals === request) {
805
+ callback(true);
806
+ return;
807
+ }
808
+ } else if (Array.isArray(externals)) {
809
+ let i = 0;
810
+ const next = ()=>{
811
+ let asyncFlag;
812
+ const handleExternalsAndCallback = (matched)=>{
813
+ if (!matched) {
814
+ if (asyncFlag) {
815
+ asyncFlag = false;
816
+ return;
817
+ }
818
+ return next();
819
+ }
820
+ callback(matched);
821
+ };
822
+ do {
823
+ asyncFlag = true;
824
+ if (i >= externals.length) {
825
+ return callback();
826
+ }
827
+ matchUserExternals(externals[i++], request, handleExternalsAndCallback);
828
+ }while (!asyncFlag);
829
+ asyncFlag = false;
830
+ };
831
+ next();
832
+ return;
833
+ } else if (externals instanceof RegExp) {
834
+ if (externals.test(request)) {
835
+ callback(true);
836
+ return;
837
+ }
838
+ } else if (typeof externals === 'function') {} else // TODO: Support function
839
+ if (typeof externals === 'object') {
840
+ if (Object.prototype.hasOwnProperty.call(externals, request)) {
841
+ callback(true);
842
+ return;
843
+ }
844
+ }
845
+ callback();
846
+ };
847
+ return {
848
+ output: {
849
+ externals: [
850
+ ({ request, dependencyType, contextInfo }, callback)=>{
851
+ let externalized = false;
852
+ const _callback = (matched)=>{
853
+ if (matched) {
854
+ externalized = true;
855
+ }
856
+ };
857
+ if (contextInfo.issuer && dependencyType === 'commonjs') {
858
+ matchUserExternals(externals, request, _callback);
859
+ if (externalized) {
860
+ dist_logger.warn(composeModuleImportWarn(request));
861
+ }
862
+ }
863
+ callback();
864
+ }
865
+ ]
866
+ }
867
+ };
868
+ };
769
869
  const composeAutoExternalConfig = (options)=>{
770
870
  const { autoExternal, pkgJson, userExternals } = options;
771
871
  if (!autoExternal) {
@@ -806,7 +906,7 @@ const composeAutoExternalConfig = (options)=>{
806
906
  }
807
907
  } : {};
808
908
  };
809
- async function createInternalRsbuildConfig() {
909
+ async function createConstantRsbuildConfig() {
810
910
  return (0,core_namespaceObject.defineConfig)({
811
911
  mode: 'production',
812
912
  dev: {
@@ -824,13 +924,54 @@ async function createInternalRsbuildConfig() {
824
924
  force: false
825
925
  }
826
926
  }
927
+ },
928
+ // TypeScript-specific behavior: if the extension is ".js" or ".jsx", try replacing it with ".ts" or ".tsx"
929
+ // see https://github.com/web-infra-dev/rslib/issues/41
930
+ resolve: {
931
+ extensionAlias: {
932
+ '.js': [
933
+ '.ts',
934
+ '.tsx',
935
+ '.js',
936
+ '.jsx'
937
+ ],
938
+ '.jsx': [
939
+ '.tsx',
940
+ '.jsx'
941
+ ],
942
+ '.mjs': [
943
+ '.mts',
944
+ '.mjs'
945
+ ],
946
+ '.cjs': [
947
+ '.cts',
948
+ '.cjs'
949
+ ]
950
+ }
827
951
  }
828
952
  }
829
953
  },
830
954
  output: {
831
955
  filenameHash: false,
832
- // TODO: easy to development at the moment
833
- minify: false,
956
+ minify: {
957
+ js: true,
958
+ css: false,
959
+ jsOptions: {
960
+ minimizerOptions: {
961
+ mangle: false,
962
+ minify: false,
963
+ compress: {
964
+ defaults: false,
965
+ unused: true,
966
+ dead_code: true,
967
+ toplevel: true
968
+ },
969
+ format: {
970
+ comments: 'all'
971
+ }
972
+ }
973
+ }
974
+ },
834
975
  distPath: {
835
976
  js: './'
836
977
  }
@@ -843,7 +984,6 @@ const composeFormatConfig = (format)=>{
843
984
  return {
844
985
  tools: {
845
986
  rspack: {
846
- externalsType: 'module-import',
847
987
  output: {
848
988
  module: true,
849
989
  chunkFormat: 'module',
@@ -871,7 +1011,6 @@ const composeFormatConfig = (format)=>{
871
1011
  return {
872
1012
  tools: {
873
1013
  rspack: {
874
- externalsType: 'commonjs',
875
1014
  output: {
876
1015
  iife: false,
877
1016
  chunkFormat: 'commonjs',
@@ -886,7 +1025,6 @@ const composeFormatConfig = (format)=>{
886
1025
  return {
887
1026
  tools: {
888
1027
  rspack: {
889
- externalsType: 'umd',
890
1028
  output: {
891
1029
  library: {
892
1030
  type: 'umd'
@@ -899,6 +1037,36 @@ const composeFormatConfig = (format)=>{
899
1037
  throw new Error(`Unsupported format: ${format}`);
900
1038
  }
901
1039
  };
1040
+ const composeModuleImportWarn = (request)=>{
1041
+ return `The externalized commonjs request ${index_js_namespaceObject["default"].green(`"${request}"`)} will use ${index_js_namespaceObject["default"].blue('"module"')} external type in ESM format. If you want to specify other external type, considering set the request and type with ${index_js_namespaceObject["default"].blue('"output.externals"')}.`;
1042
+ };
1043
+ const composeExternalsConfig = (format, externals)=>{
1044
+ // TODO: Define the internal externals config in Rsbuild's externals instead
1045
+ // Rspack's externals as they will not be merged from different fields. All externals
1046
+ // should to be unified and merged together in the future.
1047
+ const externalsTypeMap = {
1048
+ esm: 'module-import',
1049
+ cjs: 'commonjs',
1050
+ umd: 'umd'
1051
+ };
1052
+ switch(format){
1053
+ case 'esm':
1054
+ case 'cjs':
1055
+ case 'umd':
1056
+ return {
1057
+ output: externals ? {
1058
+ externals
1059
+ } : {},
1060
+ tools: {
1061
+ rspack: {
1062
+ externalsType: externalsTypeMap[format]
1063
+ }
1064
+ }
1065
+ };
1066
+ default:
1067
+ throw new Error(`Unsupported format: ${format}`);
1068
+ }
1069
+ };
902
1070
  const composeAutoExtensionConfig = (config, autoExtension, pkgJson)=>{
903
1071
  const { jsExtension, dtsExtension } = getDefaultExtension({
904
1072
  format: config.format,
@@ -1028,8 +1196,16 @@ const composeBundleConfig = (jsExtension, bundle = true)=>{
1028
1196
  // Prevent from externalizing entry modules here.
1029
1197
  if (data.contextInfo.issuer) {
1030
1198
  // Node.js ECMAScript module loader does no extension searching.
1031
- // So we add a file extension here when data.request is a relative path
1032
- return callback(null, data.request[0] === '.' ? `${data.request}${jsExtension}` : data.request);
1199
+ // Add a file extension according to autoExtension config
1200
+ // when data.request is a relative path and do not have an extension.
1201
+ // If data.request already have an extension, we replace it with new extension
1202
+ // This may result in a change in semantics,
1203
+ // user should use copy to keep origin file or use another separate entry to deal this
1204
+ let request = data.request;
1205
+ if (request[0] === '.') {
1206
+ request = (0,external_node_path_namespaceObject.extname)(request) ? request.replace(/\.[^.]+$/, jsExtension) : `${request}${jsExtension}`;
1207
+ }
1208
+ return callback(null, request);
1033
1209
  }
1034
1210
  callback();
1035
1211
  }
@@ -1100,12 +1276,12 @@ const composeTargetConfig = (target = 'web')=>{
1100
1276
  throw new Error(`Unsupported platform: ${target}`);
1101
1277
  }
1102
1278
  };
1103
- async function composeLibRsbuildConfig(libConfig, rsbuildConfig, configPath) {
1104
- const config = (0,core_namespaceObject.mergeRsbuildConfig)(rsbuildConfig, libConfig);
1279
+ async function composeLibRsbuildConfig(config, configPath) {
1105
1280
  const rootPath = (0,external_node_path_namespaceObject.dirname)(configPath);
1106
1281
  const pkgJson = readPackageJson(rootPath);
1107
1282
  const { format, autoExtension = true, autoExternal = true } = config;
1108
1283
  const formatConfig = composeFormatConfig(format);
1284
+ const externalsConfig = composeExternalsConfig(format, config.output?.externals);
1109
1285
  const { config: autoExtensionConfig, jsExtension, dtsExtension } = composeAutoExtensionConfig(config, autoExtension, pkgJson);
1110
1286
  const bundleConfig = composeBundleConfig(jsExtension, config.bundle);
1111
1287
  const targetConfig = composeTargetConfig(config.output?.target);
@@ -1113,35 +1289,52 @@ async function composeLibRsbuildConfig(libConfig, rsbuildConfig, configPath) {
1113
1289
  const autoExternalConfig = composeAutoExternalConfig({
1114
1290
  autoExternal,
1115
1291
  pkgJson,
1116
- userExternals: rsbuildConfig.output?.externals
1292
+ userExternals: config.output?.externals
1117
1293
  });
1118
1294
  const entryConfig = await composeEntryConfig(config.source?.entry, config.bundle, (0,external_node_path_namespaceObject.dirname)(configPath));
1119
1295
  const dtsConfig = await composeDtsConfig(config, dtsExtension);
1120
- return (0,core_namespaceObject.mergeRsbuildConfig)(formatConfig, autoExtensionConfig, autoExternalConfig, syntaxConfig, bundleConfig, targetConfig, entryConfig, dtsConfig);
1296
+ const externalsWarnConfig = composeExternalsWarnConfig(format, autoExternalConfig?.output?.externals, externalsConfig?.output?.externals);
1297
+ return (0,core_namespaceObject.mergeRsbuildConfig)(formatConfig, // externalsWarnConfig should before other externals config
1298
+ externalsWarnConfig, externalsConfig, autoExternalConfig, autoExtensionConfig, syntaxConfig, bundleConfig, targetConfig, entryConfig, dtsConfig);
1121
1299
  }
1122
1300
  async function composeCreateRsbuildConfig(rslibConfig, path) {
1123
- const internalRsbuildConfig = await createInternalRsbuildConfig();
1301
+ const constantRsbuildConfig = await createConstantRsbuildConfig();
1124
1302
  const configPath = path ?? rslibConfig._privateMeta?.configFilePath;
1125
1303
  const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig;
1126
1304
  if (!libConfigsArray) {
1127
1305
  throw new Error(`Expect lib field to be an array, but got ${libConfigsArray}.`);
1128
1306
  }
1129
1307
  const libConfigPromises = libConfigsArray.map(async (libConfig)=>{
1130
- const { format, ...overrideRsbuildConfig } = libConfig;
1131
- const baseRsbuildConfig = (0,core_namespaceObject.mergeRsbuildConfig)(sharedRsbuildConfig, overrideRsbuildConfig);
1308
+ const userConfig = (0,core_namespaceObject.mergeRsbuildConfig)(sharedRsbuildConfig, libConfig);
1132
1309
  // Merge the configuration of each environment based on the shared Rsbuild
1133
1310
  // configuration and Lib configuration in the settings.
1134
- const libRsbuildConfig = await composeLibRsbuildConfig(libConfig, baseRsbuildConfig, configPath);
1311
+ const libRsbuildConfig = await composeLibRsbuildConfig(userConfig, configPath);
1135
1312
  // Reset certain fields because they will be completely overridden by the upcoming merge.
1136
1313
  // We don't want to retain them in the final configuration.
1137
1314
  // The reset process should occur after merging the library configuration.
1138
- baseRsbuildConfig.source ??= {};
1139
- baseRsbuildConfig.source.entry = {};
1315
+ userConfig.source ??= {};
1316
+ userConfig.source.entry = {};
1317
+ // Already manually sort and merge the externals configuration.
1318
+ userConfig.output ??= {};
1319
+ delete userConfig.output.externals;
1140
1320
  return {
1141
- format: format,
1142
- config: (0,core_namespaceObject.mergeRsbuildConfig)(baseRsbuildConfig, libRsbuildConfig, // Merge order matters, keep `internalRsbuildConfig` at the last position
1143
- // to ensure that the internal config is not overridden by user's config.
1144
- internalRsbuildConfig)
1321
+ format: libConfig.format,
1322
+ // The merge order represents the priority of the configuration
1323
+ // The priorities from high to low are as follows:
1324
+ // 1 - userConfig: users can configure any Rsbuild and Rspack config
1325
+ // 2 - libRsbuildConfig: the configuration that we compose from Rslib unique config and userConfig from 1
1326
+ // 3 - constantRsbuildConfig: the built-in best practice Rsbuild configuration we provide in Rslib
1327
+ // We should state in the document that the built-in configuration should not be changed optionally
1328
+ // In compose process of 2, we may read some config from 1, and reassemble the related config,
1329
+ // so before final mergeRsbuildConfig, we reset some specified fields
1330
+ config: (0,core_namespaceObject.mergeRsbuildConfig)(constantRsbuildConfig, libRsbuildConfig, omitDeep(userConfig, [
1331
+ 'bundle',
1332
+ 'format',
1333
+ 'autoExtension',
1334
+ 'autoExternal',
1335
+ 'syntax',
1336
+ 'dts'
1337
+ ]))
1145
1338
  };
1146
1339
  });
1147
1340
  const composedRsbuildConfig = await Promise.all(libConfigPromises);
@@ -1190,7 +1383,7 @@ const applyCommonOptions = (command)=>{
1190
1383
  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');
1191
1384
  };
1192
1385
  function runCli() {
1193
- commander_index_js_namespaceObject.program.name('rslib').usage('<command> [options]').version("0.0.3");
1386
+ commander_index_js_namespaceObject.program.name('rslib').usage('<command> [options]').version("0.0.4");
1194
1387
  const buildCommand = commander_index_js_namespaceObject.program.command('build');
1195
1388
  const inspectCommand = commander_index_js_namespaceObject.program.command('inspect');
1196
1389
  [
@@ -1239,6 +1432,6 @@ function runCli() {
1239
1432
 
1240
1433
 
1241
1434
 
1242
- const src_version = "0.0.3";
1435
+ const src_version = "0.0.4";
1243
1436
 
1244
1437
  export { build, defineConfig, loadConfig, dist_logger as logger, prepareCli, runCli, src_version as version };
@@ -18,7 +18,8 @@ export declare const composeAutoExternalConfig: (options: {
18
18
  pkgJson?: PkgJson;
19
19
  userExternals?: NonNullable<RsbuildConfig["output"]>["externals"];
20
20
  }) => RsbuildConfig;
21
- export declare function createInternalRsbuildConfig(): Promise<RsbuildConfig>;
21
+ export declare function createConstantRsbuildConfig(): Promise<RsbuildConfig>;
22
+ export declare const composeModuleImportWarn: (request: string) => string;
22
23
  export declare function composeCreateRsbuildConfig(rslibConfig: RslibConfig, path?: string): Promise<{
23
24
  format: Format;
24
25
  config: RsbuildConfig;
@@ -8,4 +8,8 @@ export declare const nodeBuiltInModules: Array<string | RegExp>;
8
8
  export declare function calcLongestCommonPath(absPaths: string[]): Promise<string | null>;
9
9
  export declare const readPackageJson: (rootPath: string) => undefined | PkgJson;
10
10
  export declare const isObject: (obj: unknown) => obj is Record<string, any>;
11
+ type OmitDeep<T, K extends string[]> = T extends (infer U)[] ? OmitDeep<U, K>[] : T extends Record<any, any> ? {
12
+ [P in keyof T as P extends K[number] ? never : P]: OmitDeep<T[P], K>;
13
+ } : T;
14
+ export declare function omitDeep<T extends object, K extends string[]>(obj: T, keys: K): OmitDeep<T, K>;
11
15
  export { color };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rslib/core",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "The Rspack-based library build tool.",
5
5
  "homepage": "https://rslib.dev",
6
6
  "bugs": {
@@ -32,8 +32,8 @@
32
32
  "compiled"
33
33
  ],
34
34
  "dependencies": {
35
- "@rsbuild/core": "1.0.1-beta.16",
36
- "rsbuild-plugin-dts": "0.0.3"
35
+ "@rsbuild/core": "1.0.1-rc.0",
36
+ "rsbuild-plugin-dts": "0.0.4"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/fs-extra": "^11.0.4",
@@ -42,8 +42,8 @@
42
42
  "fs-extra": "^11.2.0",
43
43
  "memfs": "^4.11.1",
44
44
  "picocolors": "1.0.1",
45
- "prebundle": "1.1.0",
46
- "rslib": "npm:@rslib/core@0.0.2",
45
+ "prebundle": "1.2.2",
46
+ "rslib": "npm:@rslib/core@0.0.3",
47
47
  "rslog": "^1.2.2",
48
48
  "typescript": "^5.5.4",
49
49
  "@rslib/tsconfig": "0.0.1"