@rsbuild/plugin-source-build 1.0.2 → 1.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/README.md CHANGED
@@ -122,6 +122,18 @@ If the sub-project uses [exports](https://nodejs.org/api/packages.html#package-e
122
122
  }
123
123
  ```
124
124
 
125
+ ### Customizing Source Field
126
+
127
+ Although the plugin uses the `source` field by default to specify the source file, we recommend configuring a custom field through the [sourceField](#sourceField) option (for example, `@custom/source`, where `custom` can be replaced with any scope name).
128
+
129
+ ```ts
130
+ pluginSourceBuild({
131
+ sourceField: "@custom/source",
132
+ });
133
+ ```
134
+
135
+ This is because some packages (such as Mobx) also define a `source` field in their `package.json`. If not distinguished, Rsbuild may mistakenly resolve the source files of those dependencies, leading to unexpected build results or type issues. By using a custom field, you can avoid such conflicts and ensure that Rsbuild behaves predictably when resolving dependencies.
136
+
125
137
  ## Configure Project Reference
126
138
 
127
139
  In a TypeScript project, you need to use the capability provided by TypeScript called [Project Reference](https://typescriptlang.org/docs/handbook/project-references). It helps you develop source code more effectively.
@@ -154,7 +166,7 @@ At the same time, we need to set `composite` to `true` in the lib project's `tsc
154
166
  {
155
167
  "compilerOptions": {
156
168
  "composite": true
157
- },
169
+ }
158
170
  }
159
171
  ```
160
172
 
@@ -173,24 +185,24 @@ Note that the above example is a simplified one. In real monorepo projects, ther
173
185
 
174
186
  Used to configure the resolve field of the source code files.
175
187
 
176
- For example, when configured as `my-source`:
188
+ For example, when configured as `@custom/source`:
177
189
 
178
190
  ```ts
179
191
  pluginSourceBuild({
180
- sourceField: "my-source",
192
+ sourceField: "@custom/source",
181
193
  });
182
194
  ```
183
195
 
184
- In `package.json`, the source code file path can be specified using `my-source`:
196
+ In `package.json`, the source code file path can be specified using `@custom/source`:
185
197
 
186
198
  ```json title="package.json"
187
199
  {
188
200
  "name": "lib",
189
201
  "main": "./dist/index.js",
190
- "my-source": "./src/index.ts",
202
+ "@custom/source": "./src/index.ts",
191
203
  "exports": {
192
204
  ".": {
193
- "my-source": "./src/index.ts",
205
+ "@custom/source": "./src/index.ts",
194
206
  "default": "./dist/index.js"
195
207
  }
196
208
  }
package/README.zh-CN.md CHANGED
@@ -120,6 +120,18 @@ monorepo
120
120
  }
121
121
  ```
122
122
 
123
+ ### 自定义 source 字段
124
+
125
+ 虽然插件默认使用 `source` 字段来指定源代码文件,但我们更推荐通过 [sourceField](#sourceField) 选项配置一个自定义字段(例如 `@custom/source`,其中 `custom` 可以替换为任意 scope 名称)。
126
+
127
+ ```ts
128
+ pluginSourceBuild({
129
+ sourceField: "@custom/source",
130
+ });
131
+ ```
132
+
133
+ 这是因为某些三方库(例如 Mobx)的 `package.json` 中也包含 `source` 字段,如果不进行区分,Rsbuild 可能会错误地解析这些库的源文件路径,从而导致意料之外的构建结果或类型问题。使用自定义字段可以避免此类冲突,确保 Rsbuild 在解析依赖关系时保持可控的行为。
134
+
123
135
  ## 配置 Project Reference
124
136
 
125
137
  在 TypeScript 项目中,你需要使用 TypeScript 提供的 [Project Reference](https://typescriptlang.org/docs/handbook/project-references) 能力,它可以帮助你更好地使用源码开发。
@@ -152,7 +164,7 @@ Project reference 提供了以下能力:
152
164
  {
153
165
  "compilerOptions": {
154
166
  "composite": true
155
- },
167
+ }
156
168
  }
157
169
  ```
158
170
 
@@ -171,24 +183,24 @@ Project reference 提供了以下能力:
171
183
 
172
184
  用于配置源代码文件对应的解析字段。
173
185
 
174
- 比如配置为 `my-source`:
186
+ 比如配置为 `@custom/source`:
175
187
 
176
188
  ```ts
177
189
  pluginSourceBuild({
178
- sourceField: "my-source",
190
+ sourceField: "@custom/source",
179
191
  });
180
192
  ```
181
193
 
182
- 在 `package.json` 中,即可通过 `my-source` 指定源代码文件的路径:
194
+ 在 `package.json` 中,即可通过 `@custom/source` 指定源代码文件的路径:
183
195
 
184
196
  ```json title="package.json"
185
197
  {
186
198
  "name": "lib",
187
199
  "main": "./dist/index.js",
188
- "my-source": "./src/index.ts",
200
+ "@custom/source": "./src/index.ts",
189
201
  "exports": {
190
202
  ".": {
191
- "my-source": "./src/index.ts",
203
+ "@custom/source": "./src/index.ts",
192
204
  "default": "./dist/index.js"
193
205
  }
194
206
  }
package/dist/index.cjs CHANGED
@@ -1,59 +1,27 @@
1
1
  "use strict";
2
- var __webpack_modules__ = {
3
- yaml: function(module) {
4
- module.exports = import("yaml");
5
- }
6
- };
7
- /************************************************************************/ // The module cache
8
- var __webpack_module_cache__ = {};
9
- // The require function
10
- function __webpack_require__(moduleId) {
11
- // Check if module is in cache
12
- var cachedModule = __webpack_module_cache__[moduleId];
13
- if (void 0 !== cachedModule) return cachedModule.exports;
14
- // Create a new module (and put it into the cache)
15
- var module = __webpack_module_cache__[moduleId] = {
16
- exports: {}
17
- };
18
- // Execute the module function
19
- __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
20
- // Return the exports of the module
21
- return module.exports;
22
- }
23
- /************************************************************************/ // webpack/runtime/compat_get_default_export
2
+ var __webpack_require__ = {};
24
3
  (()=>{
25
- // getDefaultExport function for compatibility with non-ESM modules
26
- __webpack_require__.n = function(module) {
27
- var getter = module && module.__esModule ? function() {
28
- return module['default'];
29
- } : function() {
30
- return module;
31
- };
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
32
6
  __webpack_require__.d(getter, {
33
7
  a: getter
34
8
  });
35
9
  return getter;
36
10
  };
37
11
  })();
38
- // webpack/runtime/define_property_getters
39
12
  (()=>{
40
- __webpack_require__.d = function(exports1, definition) {
13
+ __webpack_require__.d = (exports1, definition)=>{
41
14
  for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
42
15
  enumerable: true,
43
16
  get: definition[key]
44
17
  });
45
18
  };
46
19
  })();
47
- // webpack/runtime/has_own_property
48
20
  (()=>{
49
- __webpack_require__.o = function(obj, prop) {
50
- return Object.prototype.hasOwnProperty.call(obj, prop);
51
- };
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
52
22
  })();
53
- // webpack/runtime/make_namespace_object
54
23
  (()=>{
55
- // define __esModule on exports
56
- __webpack_require__.r = function(exports1) {
24
+ __webpack_require__.r = (exports1)=>{
57
25
  if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
58
26
  value: 'Module'
59
27
  });
@@ -62,16 +30,14 @@ function __webpack_require__(moduleId) {
62
30
  });
63
31
  };
64
32
  })();
65
- /************************************************************************/ var __webpack_exports__ = {};
66
- // ESM COMPAT FLAG
33
+ var __webpack_exports__ = {};
67
34
  __webpack_require__.r(__webpack_exports__);
68
- // EXPORTS
69
35
  __webpack_require__.d(__webpack_exports__, {
70
- getMonorepoBaseData: ()=>/* reexport */ getMonorepoBaseData,
71
- PLUGIN_SOURCE_BUILD_NAME: ()=>/* reexport */ PLUGIN_SOURCE_BUILD_NAME,
72
- pluginSourceBuild: ()=>/* reexport */ pluginSourceBuild,
73
- getMonorepoSubProjects: ()=>/* reexport */ getMonorepoSubProjects,
74
- Project: ()=>/* reexport */ Project
36
+ Project: ()=>Project,
37
+ PLUGIN_SOURCE_BUILD_NAME: ()=>PLUGIN_SOURCE_BUILD_NAME,
38
+ getMonorepoSubProjects: ()=>getMonorepoSubProjects,
39
+ pluginSourceBuild: ()=>pluginSourceBuild,
40
+ getMonorepoBaseData: ()=>getMonorepoBaseData
75
41
  });
76
42
  const external_node_fs_namespaceObject = require("node:fs");
77
43
  var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
@@ -114,7 +80,6 @@ const isMonorepo = async (monorepoRootPath, otherMonorepoChecks)=>{
114
80
  };
115
81
  };
116
82
  const getMonorepoBaseData = async (starFindPath, otherMonorepoAnalyzer)=>{
117
- var _otherMonorepoAnalyzer_type;
118
83
  let repoIsMonorepo = false;
119
84
  let findPath = starFindPath;
120
85
  let type = '';
@@ -130,7 +95,6 @@ const getMonorepoBaseData = async (starFindPath, otherMonorepoAnalyzer)=>{
130
95
  ({ type } = result);
131
96
  break;
132
97
  }
133
- // find system root path
134
98
  if (findPath === external_node_path_default().dirname(findPath)) break;
135
99
  findPath = external_node_path_default().dirname(findPath);
136
100
  }
@@ -138,7 +102,7 @@ const getMonorepoBaseData = async (starFindPath, otherMonorepoAnalyzer)=>{
138
102
  isMonorepo: repoIsMonorepo,
139
103
  rootPath: repoIsMonorepo ? findPath : '',
140
104
  type,
141
- getProjects: null == otherMonorepoAnalyzer ? void 0 : null === (_otherMonorepoAnalyzer_type = otherMonorepoAnalyzer[type]) || void 0 === _otherMonorepoAnalyzer_type ? void 0 : _otherMonorepoAnalyzer_type.getProjects
105
+ getProjects: otherMonorepoAnalyzer?.[type]?.getProjects
142
106
  };
143
107
  };
144
108
  const external_fast_glob_namespaceObject = require("fast-glob");
@@ -178,11 +142,7 @@ function _define_property(obj, key, value) {
178
142
  else obj[key] = value;
179
143
  return obj;
180
144
  }
181
- var _getExportsSourceDirs = /*#__PURE__*/ new WeakSet(), /**
182
- *
183
- * @param paths normalize paths
184
- * @returns common root paths
185
- */ _getCommonRootPaths = /*#__PURE__*/ new WeakSet(), _getRootPath = /*#__PURE__*/ new WeakSet();
145
+ var _getExportsSourceDirs = /*#__PURE__*/ new WeakSet(), _getCommonRootPaths = /*#__PURE__*/ new WeakSet(), _getRootPath = /*#__PURE__*/ new WeakSet();
186
146
  class Project {
187
147
  async init() {
188
148
  this.metaData = await readPackageJson(external_node_path_default().join(this.dir, PACKAGE_JSON));
@@ -223,19 +183,11 @@ class Project {
223
183
  getSourceEntryPaths(options) {
224
184
  const { exports: checkExports = false, field: sourceField = 'source' } = options ?? {};
225
185
  const pkgJson = this.getMetaData();
226
- // normalize strings
227
186
  const sourceDirs = pkgJson[sourceField] ? [
228
187
  external_node_path_default().normalize(pkgJson[sourceField])
229
188
  ] : [];
230
189
  if (checkExports) {
231
- /**
232
- * analyze exports:
233
- * "exports": {
234
- * ".": {
235
- * "source": "./src/index.ts"
236
- * }
237
- * },
238
- */ const exportsSourceDirs = _class_private_method_get(this, _getExportsSourceDirs, getExportsSourceDirs).call(this, pkgJson.exports ?? {}, sourceField);
190
+ const exportsSourceDirs = _class_private_method_get(this, _getExportsSourceDirs, getExportsSourceDirs).call(this, pkgJson.exports ?? {}, sourceField);
239
191
  sourceDirs.push(...exportsSourceDirs);
240
192
  }
241
193
  if (!sourceDirs.length) throw new Error(`"${sourceField}" field is not found in ${this.name} package.json`);
@@ -256,7 +208,6 @@ function getExportsSourceDirs(exportsConfig, sourceField) {
256
208
  const exportsSourceDirs = [];
257
209
  if ('string' == typeof exportsConfig[sourceField]) exportsSourceDirs.push(external_node_path_default().normalize(exportsConfig[sourceField]));
258
210
  for (const moduleRules of Object.values(exportsConfig))if ('object' == typeof moduleRules && 'string' == typeof moduleRules[sourceField]) exportsSourceDirs.push(external_node_path_default().normalize(moduleRules[sourceField]));
259
- // normalize strings
260
211
  return exportsSourceDirs;
261
212
  }
262
213
  function getCommonRootPaths(paths) {
@@ -277,7 +228,7 @@ function getRootPath(p) {
277
228
  return p.split(external_node_path_default().sep)[0];
278
229
  }
279
230
  const getPatternsFromYaml = async (monorepoRoot)=>{
280
- const { parse } = await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "yaml"));
231
+ const { parse } = await import("yaml");
281
232
  const workspaceYamlFilePath = external_node_path_default().join(monorepoRoot, PNPM_WORKSPACE_FILE);
282
233
  const yamlContent = await external_node_fs_default().promises.readFile(workspaceYamlFilePath, 'utf8');
283
234
  const pnpmWorkspace = parse(yamlContent);
@@ -291,8 +242,6 @@ const getGlobOpts = (rootPath, patterns)=>{
291
242
  followSymbolicLinks: false
292
243
  };
293
244
  if (patterns.some((cfg)=>cfg.includes('**') || cfg.includes('*'))) globOpts.ignore = [
294
- // allow globs like "packages/**" or "packages/*",
295
- // but avoid picking up node_modules/**/package.json and dist/**/package.json
296
245
  '**/dist/**',
297
246
  '**/node_modules/**'
298
247
  ];
@@ -302,9 +251,7 @@ const makeFileFinder = (rootPath, patterns)=>{
302
251
  const globOpts = getGlobOpts(rootPath, patterns);
303
252
  return async (fileName, fileMapper)=>{
304
253
  let result = await external_fast_glob_default()(patterns.map((globPath)=>external_node_path_default().posix.join(globPath, fileName)), globOpts);
305
- // fast-glob does not respect pattern order, so we re-sort by absolute path
306
254
  result = result.sort();
307
- // POSIX results always need to be normalized
308
255
  result = normalize(result);
309
256
  return fileMapper(result);
310
257
  };
@@ -351,7 +298,6 @@ async function getDependentProjects_pathExists(path) {
351
298
  }
352
299
  const getDependentProjects = async (projectNameOrRootPath, options)=>{
353
300
  const { cwd = process.cwd(), recursive, filter, extraMonorepoStrategies } = options;
354
- // check if first argument is projectRootPath.
355
301
  const currentProjectPkgJsonPath = external_node_path_default().join(projectNameOrRootPath, 'package.json');
356
302
  let projectName;
357
303
  if (await getDependentProjects_pathExists(currentProjectPkgJsonPath)) ({ name: projectName } = await readPackageJson(currentProjectPkgJsonPath));
@@ -411,8 +357,6 @@ function pluginSourceBuild(options) {
411
357
  CHAIN_ID.RULE.JS
412
358
  ])if (chain.module.rules.get(ruleId)) {
413
359
  const rule = chain.module.rule(ruleId);
414
- // https://rspack.dev/config/resolve
415
- // when source is not exist, other mainFields will effect. // source > Rspack default mainFields.
416
360
  rule.resolve.mainFields.merge('source' === resolvePriority ? [
417
361
  sourceField,
418
362
  '...'
@@ -420,8 +364,6 @@ function pluginSourceBuild(options) {
420
364
  '...',
421
365
  sourceField
422
366
  ]);
423
- // `conditionNames` is not affected by `resolvePriority`.
424
- // The priority is controlled by the order of fields declared in `exports`.
425
367
  rule.resolve.conditionNames.add('...').add(sourceField);
426
368
  }
427
369
  });
@@ -431,7 +373,6 @@ function pluginSourceBuild(options) {
431
373
  const filePath = external_node_path_default().join(project.dir, 'tsconfig.json');
432
374
  if (external_node_fs_default().existsSync(filePath)) references.add(filePath);
433
375
  }
434
- // Add references in the current project's tsconfig.json
435
376
  const tsconfig = external_json5_default().parse(external_node_fs_default().readFileSync(tsconfigPath, 'utf-8'));
436
377
  const userReferences = [
437
378
  ...Array.isArray(rspackReferences) ? rspackReferences : [],
@@ -445,7 +386,6 @@ function pluginSourceBuild(options) {
445
386
  references.add(absolutePath);
446
387
  }
447
388
  }
448
- // avoid self reference, it will break the resolver
449
389
  references.delete(tsconfigPath);
450
390
  return Array.from(references);
451
391
  };
@@ -461,7 +401,7 @@ function pluginSourceBuild(options) {
461
401
  } : tsConfig;
462
402
  const references = await getReferences(tsconfigPath, configObject.references);
463
403
  config.resolve.tsConfig = {
464
- configFile: (null == configObject ? void 0 : configObject.configFile) || tsconfigPath,
404
+ configFile: configObject?.configFile || tsconfigPath,
465
405
  references: references
466
406
  };
467
407
  });
@@ -470,8 +410,6 @@ function pluginSourceBuild(options) {
470
410
  const { tsconfigPath } = environment;
471
411
  if (!chain.resolve.plugins.has(TS_CONFIG_PATHS) || !tsconfigPath) return;
472
412
  const references = await getReferences(tsconfigPath);
473
- // set references config
474
- // https://github.com/dividab/tsconfig-paths-webpack-plugin#options
475
413
  chain.resolve.plugin(TS_CONFIG_PATHS).tap((options)=>options.map((option)=>({
476
414
  ...option,
477
415
  references
@@ -480,8 +418,18 @@ function pluginSourceBuild(options) {
480
418
  }
481
419
  };
482
420
  }
483
- var __webpack_export_target__ = exports;
484
- for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
485
- if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
421
+ exports.PLUGIN_SOURCE_BUILD_NAME = __webpack_exports__.PLUGIN_SOURCE_BUILD_NAME;
422
+ exports.Project = __webpack_exports__.Project;
423
+ exports.getMonorepoBaseData = __webpack_exports__.getMonorepoBaseData;
424
+ exports.getMonorepoSubProjects = __webpack_exports__.getMonorepoSubProjects;
425
+ exports.pluginSourceBuild = __webpack_exports__.pluginSourceBuild;
426
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
427
+ "PLUGIN_SOURCE_BUILD_NAME",
428
+ "Project",
429
+ "getMonorepoBaseData",
430
+ "getMonorepoSubProjects",
431
+ "pluginSourceBuild"
432
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
433
+ Object.defineProperty(exports, '__esModule', {
486
434
  value: true
487
435
  });
package/dist/index.js CHANGED
@@ -1,19 +1,19 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE_node_fs__ from "node:fs";
2
- import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
3
- import * as __WEBPACK_EXTERNAL_MODULE_json5__ from "json5";
4
- import * as __WEBPACK_EXTERNAL_MODULE_fast_glob__ from "fast-glob";
1
+ import node_fs from "node:fs";
2
+ import node_path from "node:path";
3
+ import json5 from "json5";
4
+ import fast_glob from "fast-glob";
5
5
  const PNPM_WORKSPACE_FILE = 'pnpm-workspace.yaml';
6
6
  const RUSH_JSON_FILE = 'rush.json';
7
7
  const PACKAGE_JSON = 'package.json';
8
8
  async function pathExists(path) {
9
- return __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.access(path).then(()=>true).catch(()=>false);
9
+ return node_fs.promises.access(path).then(()=>true).catch(()=>false);
10
10
  }
11
11
  const isPnpmMonorepo = async (monorepoRootPath)=>{
12
- const existPnpmWorkspaceFile = await pathExists(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(monorepoRootPath, PNPM_WORKSPACE_FILE));
12
+ const existPnpmWorkspaceFile = await pathExists(node_path.join(monorepoRootPath, PNPM_WORKSPACE_FILE));
13
13
  return existPnpmWorkspaceFile;
14
14
  };
15
15
  const isRushMonorepo = async (monorepoRootPath)=>{
16
- const existRushJsonFile = await pathExists(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(monorepoRootPath, RUSH_JSON_FILE));
16
+ const existRushJsonFile = await pathExists(node_path.join(monorepoRootPath, RUSH_JSON_FILE));
17
17
  return existRushJsonFile;
18
18
  };
19
19
  const isMonorepo = async (monorepoRootPath, otherMonorepoChecks)=>{
@@ -37,7 +37,6 @@ const isMonorepo = async (monorepoRootPath, otherMonorepoChecks)=>{
37
37
  };
38
38
  };
39
39
  const getMonorepoBaseData = async (starFindPath, otherMonorepoAnalyzer)=>{
40
- var _otherMonorepoAnalyzer_type;
41
40
  let repoIsMonorepo = false;
42
41
  let findPath = starFindPath;
43
42
  let type = '';
@@ -53,29 +52,28 @@ const getMonorepoBaseData = async (starFindPath, otherMonorepoAnalyzer)=>{
53
52
  ({ type } = result);
54
53
  break;
55
54
  }
56
- // find system root path
57
- if (findPath === __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(findPath)) break;
58
- findPath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(findPath);
55
+ if (findPath === node_path.dirname(findPath)) break;
56
+ findPath = node_path.dirname(findPath);
59
57
  }
60
58
  return {
61
59
  isMonorepo: repoIsMonorepo,
62
60
  rootPath: repoIsMonorepo ? findPath : '',
63
61
  type,
64
- getProjects: null == otherMonorepoAnalyzer ? void 0 : null === (_otherMonorepoAnalyzer_type = otherMonorepoAnalyzer[type]) || void 0 === _otherMonorepoAnalyzer_type ? void 0 : _otherMonorepoAnalyzer_type.getProjects
62
+ getProjects: otherMonorepoAnalyzer?.[type]?.getProjects
65
63
  };
66
64
  };
67
65
  const readPackageJson = async (pkgJsonFilePath)=>readJson(pkgJsonFilePath);
68
66
  const readRushJson = async (rushJsonFilePath)=>{
69
- const rushJson = readJson(rushJsonFilePath.includes(RUSH_JSON_FILE) ? rushJsonFilePath : __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(rushJsonFilePath, RUSH_JSON_FILE));
67
+ const rushJson = readJson(rushJsonFilePath.includes(RUSH_JSON_FILE) ? rushJsonFilePath : node_path.join(rushJsonFilePath, RUSH_JSON_FILE));
70
68
  return rushJson;
71
69
  };
72
70
  async function utils_pathExists(path) {
73
- return __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.access(path).then(()=>true).catch(()=>false);
71
+ return node_fs.promises.access(path).then(()=>true).catch(()=>false);
74
72
  }
75
73
  const readJson = async (jsonFileAbsPath)=>{
76
74
  if (!await utils_pathExists(jsonFileAbsPath)) return {};
77
- const content = await __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.readFile(jsonFileAbsPath, 'utf-8');
78
- const json = __WEBPACK_EXTERNAL_MODULE_json5__["default"].parse(content);
75
+ const content = await node_fs.promises.readFile(jsonFileAbsPath, 'utf-8');
76
+ const json = json5.parse(content);
79
77
  return json;
80
78
  };
81
79
  function _check_private_redeclaration(obj, privateCollection) {
@@ -99,14 +97,10 @@ function _define_property(obj, key, value) {
99
97
  else obj[key] = value;
100
98
  return obj;
101
99
  }
102
- var _getExportsSourceDirs = /*#__PURE__*/ new WeakSet(), /**
103
- *
104
- * @param paths normalize paths
105
- * @returns common root paths
106
- */ _getCommonRootPaths = /*#__PURE__*/ new WeakSet(), _getRootPath = /*#__PURE__*/ new WeakSet();
100
+ var _getExportsSourceDirs = /*#__PURE__*/ new WeakSet(), _getCommonRootPaths = /*#__PURE__*/ new WeakSet(), _getRootPath = /*#__PURE__*/ new WeakSet();
107
101
  class Project {
108
102
  async init() {
109
- this.metaData = await readPackageJson(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(this.dir, PACKAGE_JSON));
103
+ this.metaData = await readPackageJson(node_path.join(this.dir, PACKAGE_JSON));
110
104
  }
111
105
  getMetaData() {
112
106
  if (null === this.metaData) throw new Error('The Project object needs to be initialized by executing the `init` function');
@@ -144,19 +138,11 @@ class Project {
144
138
  getSourceEntryPaths(options) {
145
139
  const { exports: checkExports = false, field: sourceField = 'source' } = options ?? {};
146
140
  const pkgJson = this.getMetaData();
147
- // normalize strings
148
141
  const sourceDirs = pkgJson[sourceField] ? [
149
- __WEBPACK_EXTERNAL_MODULE_node_path__["default"].normalize(pkgJson[sourceField])
142
+ node_path.normalize(pkgJson[sourceField])
150
143
  ] : [];
151
144
  if (checkExports) {
152
- /**
153
- * analyze exports:
154
- * "exports": {
155
- * ".": {
156
- * "source": "./src/index.ts"
157
- * }
158
- * },
159
- */ const exportsSourceDirs = _class_private_method_get(this, _getExportsSourceDirs, getExportsSourceDirs).call(this, pkgJson.exports ?? {}, sourceField);
145
+ const exportsSourceDirs = _class_private_method_get(this, _getExportsSourceDirs, getExportsSourceDirs).call(this, pkgJson.exports ?? {}, sourceField);
160
146
  sourceDirs.push(...exportsSourceDirs);
161
147
  }
162
148
  if (!sourceDirs.length) throw new Error(`"${sourceField}" field is not found in ${this.name} package.json`);
@@ -175,9 +161,8 @@ class Project {
175
161
  }
176
162
  function getExportsSourceDirs(exportsConfig, sourceField) {
177
163
  const exportsSourceDirs = [];
178
- if ('string' == typeof exportsConfig[sourceField]) exportsSourceDirs.push(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].normalize(exportsConfig[sourceField]));
179
- for (const moduleRules of Object.values(exportsConfig))if ('object' == typeof moduleRules && 'string' == typeof moduleRules[sourceField]) exportsSourceDirs.push(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].normalize(moduleRules[sourceField]));
180
- // normalize strings
164
+ if ('string' == typeof exportsConfig[sourceField]) exportsSourceDirs.push(node_path.normalize(exportsConfig[sourceField]));
165
+ for (const moduleRules of Object.values(exportsConfig))if ('object' == typeof moduleRules && 'string' == typeof moduleRules[sourceField]) exportsSourceDirs.push(node_path.normalize(moduleRules[sourceField]));
181
166
  return exportsSourceDirs;
182
167
  }
183
168
  function getCommonRootPaths(paths) {
@@ -185,26 +170,26 @@ function getCommonRootPaths(paths) {
185
170
  for (const p of paths){
186
171
  let dir;
187
172
  try {
188
- dir = __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].statSync(p).isDirectory() ? p : __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(p);
173
+ dir = node_fs.statSync(p).isDirectory() ? p : node_path.dirname(p);
189
174
  } catch {
190
- dir = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(p);
175
+ dir = node_path.dirname(p);
191
176
  }
192
177
  const rootPath = _class_private_method_get(this, _getRootPath, getRootPath).call(this, dir);
193
178
  if (!commonRootPathsSet.has(rootPath)) commonRootPathsSet.add(rootPath);
194
179
  }
195
- return Array.from(commonRootPathsSet).map((p)=>__WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(this.dir, p));
180
+ return Array.from(commonRootPathsSet).map((p)=>node_path.join(this.dir, p));
196
181
  }
197
182
  function getRootPath(p) {
198
- return p.split(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].sep)[0];
183
+ return p.split(node_path.sep)[0];
199
184
  }
200
185
  const getPatternsFromYaml = async (monorepoRoot)=>{
201
186
  const { parse } = await import("yaml");
202
- const workspaceYamlFilePath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(monorepoRoot, PNPM_WORKSPACE_FILE);
203
- const yamlContent = await __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.readFile(workspaceYamlFilePath, 'utf8');
187
+ const workspaceYamlFilePath = node_path.join(monorepoRoot, PNPM_WORKSPACE_FILE);
188
+ const yamlContent = await node_fs.promises.readFile(workspaceYamlFilePath, 'utf8');
204
189
  const pnpmWorkspace = parse(yamlContent);
205
190
  return pnpmWorkspace.packages || [];
206
191
  };
207
- const normalize = (results)=>results.map((fp)=>__WEBPACK_EXTERNAL_MODULE_node_path__["default"].normalize(fp));
192
+ const normalize = (results)=>results.map((fp)=>node_path.normalize(fp));
208
193
  const getGlobOpts = (rootPath, patterns)=>{
209
194
  const globOpts = {
210
195
  cwd: rootPath,
@@ -212,8 +197,6 @@ const getGlobOpts = (rootPath, patterns)=>{
212
197
  followSymbolicLinks: false
213
198
  };
214
199
  if (patterns.some((cfg)=>cfg.includes('**') || cfg.includes('*'))) globOpts.ignore = [
215
- // allow globs like "packages/**" or "packages/*",
216
- // but avoid picking up node_modules/**/package.json and dist/**/package.json
217
200
  '**/dist/**',
218
201
  '**/node_modules/**'
219
202
  ];
@@ -222,10 +205,8 @@ const getGlobOpts = (rootPath, patterns)=>{
222
205
  const makeFileFinder = (rootPath, patterns)=>{
223
206
  const globOpts = getGlobOpts(rootPath, patterns);
224
207
  return async (fileName, fileMapper)=>{
225
- let result = await (0, __WEBPACK_EXTERNAL_MODULE_fast_glob__["default"])(patterns.map((globPath)=>__WEBPACK_EXTERNAL_MODULE_node_path__["default"].posix.join(globPath, fileName)), globOpts);
226
- // fast-glob does not respect pattern order, so we re-sort by absolute path
208
+ let result = await fast_glob(patterns.map((globPath)=>node_path.posix.join(globPath, fileName)), globOpts);
227
209
  result = result.sort();
228
- // POSIX results always need to be normalized
229
210
  result = normalize(result);
230
211
  return fileMapper(result);
231
212
  };
@@ -235,7 +216,7 @@ const readPnpmProjects = async (monorepoRoot, patterns)=>{
235
216
  const mapper = async (pkgJsonFilePath)=>{
236
217
  const pkgJson = await readPackageJson(pkgJsonFilePath);
237
218
  return {
238
- dir: __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(pkgJsonFilePath),
219
+ dir: node_path.dirname(pkgJsonFilePath),
239
220
  manifest: pkgJson
240
221
  };
241
222
  };
@@ -255,7 +236,7 @@ const rush_getProjects = async (monorepoRoot)=>{
255
236
  const rushConfiguration = await readRushJson(monorepoRoot);
256
237
  const { projects = [] } = rushConfiguration;
257
238
  return Promise.all(projects.map(async (p)=>{
258
- const project = new Project(p.packageName, __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(monorepoRoot, p.projectFolder));
239
+ const project = new Project(p.packageName, node_path.join(monorepoRoot, p.projectFolder));
259
240
  await project.init();
260
241
  return project;
261
242
  }));
@@ -268,12 +249,11 @@ const getMonorepoSubProjects = async (monorepoBaseData)=>{
268
249
  return [];
269
250
  };
270
251
  async function getDependentProjects_pathExists(path) {
271
- return __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.access(path).then(()=>true).catch(()=>false);
252
+ return node_fs.promises.access(path).then(()=>true).catch(()=>false);
272
253
  }
273
254
  const getDependentProjects = async (projectNameOrRootPath, options)=>{
274
255
  const { cwd = process.cwd(), recursive, filter, extraMonorepoStrategies } = options;
275
- // check if first argument is projectRootPath.
276
- const currentProjectPkgJsonPath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(projectNameOrRootPath, 'package.json');
256
+ const currentProjectPkgJsonPath = node_path.join(projectNameOrRootPath, 'package.json');
277
257
  let projectName;
278
258
  if (await getDependentProjects_pathExists(currentProjectPkgJsonPath)) ({ name: projectName } = await readPackageJson(currentProjectPkgJsonPath));
279
259
  else projectName = projectNameOrRootPath;
@@ -332,8 +312,6 @@ function pluginSourceBuild(options) {
332
312
  CHAIN_ID.RULE.JS
333
313
  ])if (chain.module.rules.get(ruleId)) {
334
314
  const rule = chain.module.rule(ruleId);
335
- // https://rspack.dev/config/resolve
336
- // when source is not exist, other mainFields will effect. // source > Rspack default mainFields.
337
315
  rule.resolve.mainFields.merge('source' === resolvePriority ? [
338
316
  sourceField,
339
317
  '...'
@@ -341,32 +319,28 @@ function pluginSourceBuild(options) {
341
319
  '...',
342
320
  sourceField
343
321
  ]);
344
- // `conditionNames` is not affected by `resolvePriority`.
345
- // The priority is controlled by the order of fields declared in `exports`.
346
322
  rule.resolve.conditionNames.add('...').add(sourceField);
347
323
  }
348
324
  });
349
325
  const getReferences = async (tsconfigPath, rspackReferences)=>{
350
326
  const references = new Set();
351
327
  for (const project of projects || []){
352
- const filePath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(project.dir, 'tsconfig.json');
353
- if (__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].existsSync(filePath)) references.add(filePath);
328
+ const filePath = node_path.join(project.dir, 'tsconfig.json');
329
+ if (node_fs.existsSync(filePath)) references.add(filePath);
354
330
  }
355
- // Add references in the current project's tsconfig.json
356
- const tsconfig = __WEBPACK_EXTERNAL_MODULE_json5__["default"].parse(__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].readFileSync(tsconfigPath, 'utf-8'));
331
+ const tsconfig = json5.parse(node_fs.readFileSync(tsconfigPath, 'utf-8'));
357
332
  const userReferences = [
358
333
  ...Array.isArray(rspackReferences) ? rspackReferences : [],
359
334
  ...tsconfig.references ? tsconfig.references.map((item)=>item.path).filter(Boolean) : []
360
335
  ];
361
336
  if (userReferences.length) {
362
- const baseDir = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(tsconfigPath);
337
+ const baseDir = node_path.dirname(tsconfigPath);
363
338
  for (const item of userReferences){
364
339
  if (!item) continue;
365
- const absolutePath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].isAbsolute(item) ? item : __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(baseDir, item);
340
+ const absolutePath = node_path.isAbsolute(item) ? item : node_path.join(baseDir, item);
366
341
  references.add(absolutePath);
367
342
  }
368
343
  }
369
- // avoid self reference, it will break the resolver
370
344
  references.delete(tsconfigPath);
371
345
  return Array.from(references);
372
346
  };
@@ -382,7 +356,7 @@ function pluginSourceBuild(options) {
382
356
  } : tsConfig;
383
357
  const references = await getReferences(tsconfigPath, configObject.references);
384
358
  config.resolve.tsConfig = {
385
- configFile: (null == configObject ? void 0 : configObject.configFile) || tsconfigPath,
359
+ configFile: configObject?.configFile || tsconfigPath,
386
360
  references: references
387
361
  };
388
362
  });
@@ -391,8 +365,6 @@ function pluginSourceBuild(options) {
391
365
  const { tsconfigPath } = environment;
392
366
  if (!chain.resolve.plugins.has(TS_CONFIG_PATHS) || !tsconfigPath) return;
393
367
  const references = await getReferences(tsconfigPath);
394
- // set references config
395
- // https://github.com/dividab/tsconfig-paths-webpack-plugin#options
396
368
  chain.resolve.plugin(TS_CONFIG_PATHS).tap((options)=>options.map((option)=>({
397
369
  ...option,
398
370
  references
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-source-build",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "An Rsbuild plugin to provide support for monorepo source code referencing.",
5
- "repository": "https://github.com/rspack-contrib/rsbuild-plugin-source-build",
5
+ "repository": "https://github.com/rstackjs/rsbuild-plugin-source-build",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
8
  "exports": {
@@ -34,36 +34,36 @@
34
34
  ]
35
35
  },
36
36
  "dependencies": {
37
- "fast-glob": "^3.3.2",
37
+ "fast-glob": "^3.3.3",
38
38
  "json5": "^2.2.3",
39
- "yaml": "^2.6.1"
39
+ "yaml": "^2.8.2"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@biomejs/biome": "^1.9.4",
43
- "@playwright/test": "^1.49.0",
44
- "@rsbuild/core": "^1.1.6",
45
- "@rsbuild/plugin-react": "^1.0.7",
46
- "@rsbuild/plugin-type-check": "^1.1.0",
47
- "@rslib/core": "^0.1.1",
48
- "@types/node": "^22.10.1",
49
- "@types/react": "^18.3.12",
50
- "@types/react-dom": "^18.3.1",
51
- "nano-staged": "^0.8.0",
52
- "playwright": "^1.49.0",
53
- "react": "^18.3.1",
54
- "react-dom": "^18.3.1",
55
- "simple-git-hooks": "^2.11.1",
56
- "typescript": "^5.7.2"
43
+ "@playwright/test": "^1.57.0",
44
+ "@rsbuild/core": "^1.6.10",
45
+ "@rsbuild/plugin-react": "^1.4.2",
46
+ "@rsbuild/plugin-type-check": "^1.3.1",
47
+ "@rslib/core": "^0.18.2",
48
+ "@types/node": "^22.19.1",
49
+ "@types/react": "^19.2.7",
50
+ "@types/react-dom": "^19.2.3",
51
+ "nano-staged": "^0.9.0",
52
+ "playwright": "^1.57.0",
53
+ "react": "^19.2.0",
54
+ "react-dom": "^19.2.0",
55
+ "simple-git-hooks": "^2.13.1",
56
+ "typescript": "^5.9.3"
57
57
  },
58
58
  "peerDependencies": {
59
- "@rsbuild/core": "1.x"
59
+ "@rsbuild/core": "^1.0.0 || ^2.0.0-0"
60
60
  },
61
61
  "peerDependenciesMeta": {
62
62
  "@rsbuild/core": {
63
63
  "optional": true
64
64
  }
65
65
  },
66
- "packageManager": "pnpm@9.14.4",
66
+ "packageManager": "pnpm@10.24.0",
67
67
  "publishConfig": {
68
68
  "access": "public",
69
69
  "registry": "https://registry.npmjs.org/"