@rsbuild/plugin-source-build 0.7.0-beta.3 → 0.7.0-beta.5

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.cjs CHANGED
@@ -5,6 +5,10 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __esm = (fn, res) => function __init() {
10
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
11
+ };
8
12
  var __export = (target, all) => {
9
13
  for (var name in all)
10
14
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -26,19 +30,492 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
30
  mod
27
31
  ));
28
32
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
+ var __publicField = (obj, key, value) => {
34
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
35
+ return value;
36
+ };
37
+ var __accessCheck = (obj, member, msg) => {
38
+ if (!member.has(obj))
39
+ throw TypeError("Cannot " + msg);
40
+ };
41
+ var __privateAdd = (obj, member, value) => {
42
+ if (member.has(obj))
43
+ throw TypeError("Cannot add the same private member more than once");
44
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
45
+ };
46
+ var __privateMethod = (obj, member, method) => {
47
+ __accessCheck(obj, member, "access private method");
48
+ return method;
49
+ };
50
+
51
+ // src/constants.ts
52
+ var PNPM_WORKSPACE_FILE, RUSH_JSON_FILE, PACKAGE_JSON;
53
+ var init_constants = __esm({
54
+ "src/constants.ts"() {
55
+ "use strict";
56
+ PNPM_WORKSPACE_FILE = "pnpm-workspace.yaml";
57
+ RUSH_JSON_FILE = "rush.json";
58
+ PACKAGE_JSON = "package.json";
59
+ }
60
+ });
61
+
62
+ // src/utils.ts
63
+ var import_node_path3, import_shared2, import_json5, readPackageJson, readRushJson, readJson;
64
+ var init_utils = __esm({
65
+ "src/utils.ts"() {
66
+ "use strict";
67
+ import_node_path3 = __toESM(require("path"));
68
+ import_shared2 = require("@rsbuild/shared");
69
+ import_json5 = __toESM(require("@rsbuild/shared/json5"));
70
+ init_constants();
71
+ readPackageJson = async (pkgJsonFilePath) => {
72
+ return readJson(pkgJsonFilePath);
73
+ };
74
+ readRushJson = async (rushJsonFilePath) => {
75
+ const rushJson = readJson(
76
+ rushJsonFilePath.includes(RUSH_JSON_FILE) ? rushJsonFilePath : import_node_path3.default.join(rushJsonFilePath, RUSH_JSON_FILE)
77
+ );
78
+ return rushJson;
79
+ };
80
+ readJson = async (jsonFileAbsPath) => {
81
+ if (!await import_shared2.fse.pathExists(jsonFileAbsPath)) {
82
+ return {};
83
+ }
84
+ const content = await import_shared2.fse.readFile(jsonFileAbsPath, "utf-8");
85
+ const json = import_json5.default.parse(content);
86
+ return json;
87
+ };
88
+ }
89
+ });
90
+
91
+ // src/project/project.ts
92
+ var import_node_fs, import_node_path4, _getExportsSourceDirs, getExportsSourceDirs_fn, _getCommonRootPaths, getCommonRootPaths_fn, _getRootPath, getRootPath_fn, Project;
93
+ var init_project = __esm({
94
+ "src/project/project.ts"() {
95
+ "use strict";
96
+ import_node_fs = __toESM(require("fs"));
97
+ import_node_path4 = __toESM(require("path"));
98
+ init_constants();
99
+ init_utils();
100
+ Project = class {
101
+ constructor(name, dir) {
102
+ __privateAdd(this, _getExportsSourceDirs);
103
+ /**
104
+ *
105
+ * @param paths normalize paths
106
+ * @returns common root paths
107
+ */
108
+ __privateAdd(this, _getCommonRootPaths);
109
+ __privateAdd(this, _getRootPath);
110
+ __publicField(this, "name");
111
+ __publicField(this, "dir");
112
+ __publicField(this, "metaData");
113
+ this.name = name;
114
+ this.dir = dir;
115
+ }
116
+ async init() {
117
+ this.metaData = await readPackageJson(import_node_path4.default.join(this.dir, PACKAGE_JSON));
118
+ }
119
+ getMetaData() {
120
+ if (this.metaData === null) {
121
+ throw new Error(
122
+ "The Project object needs to be initialized by executing the `init` function"
123
+ );
124
+ }
125
+ return this.metaData;
126
+ }
127
+ getDependentProjects(monorepoProjects, options) {
128
+ const { recursive } = options ?? { recursive: false };
129
+ const allProjectMap = /* @__PURE__ */ new Map();
130
+ for (const project of monorepoProjects) {
131
+ allProjectMap.set(project.name, project);
132
+ }
133
+ if (!recursive) {
134
+ return this.getDirectDependentProjects(allProjectMap);
135
+ }
136
+ const computedSet = /* @__PURE__ */ new Set();
137
+ computedSet.add(this.name);
138
+ const queue = this.getDirectDependentProjects(allProjectMap).filter(
139
+ (p) => !computedSet.has(p.name)
140
+ );
141
+ const result = [];
142
+ while (queue.length > 0) {
143
+ const item = queue.shift();
144
+ if (computedSet.has(item.name)) {
145
+ continue;
146
+ }
147
+ result.push(item);
148
+ computedSet.add(item.name);
149
+ const newDeps = item.getDirectDependentProjects(allProjectMap);
150
+ if (newDeps.length > 0) {
151
+ queue.push(...newDeps);
152
+ }
153
+ }
154
+ return result;
155
+ }
156
+ getDirectDependentProjects(allProjectMap) {
157
+ const pkgJson = this.getMetaData();
158
+ const { dependencies = {}, devDependencies = {} } = pkgJson;
159
+ const projects = [];
160
+ for (const d of Object.keys(dependencies)) {
161
+ if (allProjectMap.has(d)) {
162
+ projects.push(allProjectMap.get(d));
163
+ }
164
+ }
165
+ for (const d of Object.keys(devDependencies)) {
166
+ if (allProjectMap.has(d)) {
167
+ projects.push(allProjectMap.get(d));
168
+ }
169
+ }
170
+ return projects;
171
+ }
172
+ getSourceEntryPaths(options) {
173
+ const { exports: checkExports = false, field: sourceField = "source" } = options ?? {};
174
+ const pkgJson = this.getMetaData();
175
+ const sourceDirs = pkgJson[sourceField] ? [import_node_path4.default.normalize(pkgJson[sourceField])] : [];
176
+ if (checkExports) {
177
+ const exportsSourceDirs = __privateMethod(this, _getExportsSourceDirs, getExportsSourceDirs_fn).call(this, pkgJson.exports ?? {}, sourceField);
178
+ sourceDirs.push(...exportsSourceDirs);
179
+ }
180
+ if (!sourceDirs.length) {
181
+ throw new Error(
182
+ `"${sourceField}" field is not found in ${this.name} package.json`
183
+ );
184
+ }
185
+ return __privateMethod(this, _getCommonRootPaths, getCommonRootPaths_fn).call(this, sourceDirs);
186
+ }
187
+ };
188
+ _getExportsSourceDirs = new WeakSet();
189
+ getExportsSourceDirs_fn = function(exportsConfig, sourceField) {
190
+ const exportsSourceDirs = [];
191
+ for (const moduleRules of Object.values(exportsConfig)) {
192
+ if (typeof moduleRules === "object" && typeof moduleRules[sourceField] === "string") {
193
+ exportsSourceDirs.push(
194
+ import_node_path4.default.normalize(moduleRules[sourceField])
195
+ );
196
+ }
197
+ }
198
+ return exportsSourceDirs;
199
+ };
200
+ _getCommonRootPaths = new WeakSet();
201
+ getCommonRootPaths_fn = function(paths) {
202
+ const commonRootPathsSet = /* @__PURE__ */ new Set();
203
+ for (const p of paths) {
204
+ let dir;
205
+ try {
206
+ dir = import_node_fs.default.statSync(p).isDirectory() ? p : import_node_path4.default.dirname(p);
207
+ } catch {
208
+ dir = import_node_path4.default.dirname(p);
209
+ }
210
+ const rootPath = __privateMethod(this, _getRootPath, getRootPath_fn).call(this, dir);
211
+ if (!commonRootPathsSet.has(rootPath)) {
212
+ commonRootPathsSet.add(rootPath);
213
+ }
214
+ }
215
+ return Array.from(commonRootPathsSet).map((p) => import_node_path4.default.join(this.dir, p));
216
+ };
217
+ _getRootPath = new WeakSet();
218
+ getRootPath_fn = function(p) {
219
+ return p.split(import_node_path4.default.sep)[0];
220
+ };
221
+ }
222
+ });
223
+
224
+ // src/common/pnpm.ts
225
+ var pnpm_exports = {};
226
+ __export(pnpm_exports, {
227
+ getPatternsFromYaml: () => getPatternsFromYaml,
228
+ getProjects: () => getProjects,
229
+ makeFileFinder: () => makeFileFinder,
230
+ normalize: () => normalize,
231
+ readPnpmProjects: () => readPnpmProjects
232
+ });
233
+ var import_node_path5, import_shared3, import_yaml, import_fast_glob, getPatternsFromYaml, normalize, getGlobOpts, makeFileFinder, readPnpmProjects, getProjects;
234
+ var init_pnpm = __esm({
235
+ "src/common/pnpm.ts"() {
236
+ "use strict";
237
+ import_node_path5 = __toESM(require("path"));
238
+ import_shared3 = require("@rsbuild/shared");
239
+ import_yaml = require("@rsbuild/shared/yaml");
240
+ import_fast_glob = __toESM(require("fast-glob"));
241
+ init_constants();
242
+ init_project();
243
+ init_utils();
244
+ getPatternsFromYaml = async (monorepoRoot) => {
245
+ const workspaceYamlFilePath = import_node_path5.default.join(monorepoRoot, PNPM_WORKSPACE_FILE);
246
+ const yamlContent = await import_shared3.fse.readFile(workspaceYamlFilePath, "utf8");
247
+ const pnpmWorkspace = (0, import_yaml.parse)(yamlContent);
248
+ return pnpmWorkspace.packages || [];
249
+ };
250
+ normalize = (results) => results.map((fp) => import_node_path5.default.normalize(fp));
251
+ getGlobOpts = (rootPath, patterns) => {
252
+ const globOpts = {
253
+ cwd: rootPath,
254
+ absolute: true,
255
+ followSymbolicLinks: false
256
+ };
257
+ if (patterns.some((cfg) => cfg.includes("**") || cfg.includes("*"))) {
258
+ globOpts.ignore = [
259
+ // allow globs like "packages/**" or "packages/*",
260
+ // but avoid picking up node_modules/**/package.json and dist/**/package.json
261
+ "**/dist/**",
262
+ "**/node_modules/**"
263
+ ];
264
+ }
265
+ return globOpts;
266
+ };
267
+ makeFileFinder = (rootPath, patterns) => {
268
+ const globOpts = getGlobOpts(rootPath, patterns);
269
+ return async (fileName, fileMapper) => {
270
+ let result = await (0, import_fast_glob.default)(
271
+ patterns.map((globPath) => import_node_path5.default.posix.join(globPath, fileName)),
272
+ globOpts
273
+ );
274
+ result = result.sort();
275
+ result = normalize(result);
276
+ return fileMapper(result);
277
+ };
278
+ };
279
+ readPnpmProjects = async (monorepoRoot, patterns) => {
280
+ const finder = makeFileFinder(monorepoRoot, patterns);
281
+ const mapper = async (pkgJsonFilePath) => {
282
+ const pkgJson = await readPackageJson(pkgJsonFilePath);
283
+ return {
284
+ dir: import_node_path5.default.dirname(pkgJsonFilePath),
285
+ manifest: pkgJson
286
+ };
287
+ };
288
+ const projects = await finder(
289
+ PACKAGE_JSON,
290
+ (filePaths) => Promise.all(filePaths.map(mapper))
291
+ );
292
+ return projects;
293
+ };
294
+ getProjects = async (monorepoRoot) => {
295
+ const patterns = await getPatternsFromYaml(monorepoRoot);
296
+ const pnpmProjects = await readPnpmProjects(monorepoRoot, patterns);
297
+ return Promise.all(
298
+ pnpmProjects.filter((p) => p.manifest.name).map(async (p) => {
299
+ const project = new Project(p.manifest.name, p.dir);
300
+ await project.init();
301
+ return project;
302
+ })
303
+ );
304
+ };
305
+ }
306
+ });
307
+
308
+ // src/common/rush.ts
309
+ var rush_exports = {};
310
+ __export(rush_exports, {
311
+ getProjects: () => getProjects2
312
+ });
313
+ var import_node_path6, getProjects2;
314
+ var init_rush = __esm({
315
+ "src/common/rush.ts"() {
316
+ "use strict";
317
+ import_node_path6 = __toESM(require("path"));
318
+ init_project();
319
+ init_utils();
320
+ getProjects2 = async (monorepoRoot) => {
321
+ const rushConfiguration = await readRushJson(monorepoRoot);
322
+ const { projects = [] } = rushConfiguration;
323
+ return Promise.all(
324
+ projects.map(async (p) => {
325
+ const project = new Project(
326
+ p.packageName,
327
+ import_node_path6.default.join(monorepoRoot, p.projectFolder)
328
+ );
329
+ await project.init();
330
+ return project;
331
+ })
332
+ );
333
+ };
334
+ }
335
+ });
29
336
 
30
337
  // src/index.ts
31
338
  var src_exports = {};
32
339
  __export(src_exports, {
33
340
  PLUGIN_SOURCE_BUILD_NAME: () => PLUGIN_SOURCE_BUILD_NAME,
34
- getSourceInclude: () => getSourceInclude,
341
+ Project: () => Project,
342
+ getMonorepoBaseData: () => getMonorepoBaseData,
343
+ getMonorepoSubProjects: () => getMonorepoSubProjects,
35
344
  pluginSourceBuild: () => pluginSourceBuild
36
345
  });
37
346
  module.exports = __toCommonJS(src_exports);
38
- var import_node_fs = __toESM(require("fs"));
347
+
348
+ // src/plugin.ts
349
+ var import_node_fs2 = __toESM(require("fs"));
350
+ var import_node_path8 = __toESM(require("path"));
351
+ var import_shared5 = require("@rsbuild/shared");
352
+
353
+ // src/project-utils/getDependentProjects.ts
354
+ var import_node_path7 = __toESM(require("path"));
355
+ var import_shared4 = require("@rsbuild/shared");
356
+
357
+ // src/common/getBaseData.ts
358
+ var import_node_path2 = __toESM(require("path"));
359
+
360
+ // src/common/isMonorepo.ts
39
361
  var import_node_path = __toESM(require("path"));
40
- var import_monorepo_utils = require("@rsbuild/monorepo-utils");
41
362
  var import_shared = require("@rsbuild/shared");
363
+ init_constants();
364
+ var isPnpmMonorepo = async (monorepoRootPath) => {
365
+ const existPnpmWorkspaceFile = await import_shared.fse.pathExists(
366
+ import_node_path.default.join(monorepoRootPath, PNPM_WORKSPACE_FILE)
367
+ );
368
+ return existPnpmWorkspaceFile;
369
+ };
370
+ var isRushMonorepo = async (monorepoRootPath) => {
371
+ const existRushJsonFile = await import_shared.fse.pathExists(
372
+ import_node_path.default.join(monorepoRootPath, RUSH_JSON_FILE)
373
+ );
374
+ return existRushJsonFile;
375
+ };
376
+ var isMonorepo = async (monorepoRootPath, otherMonorepoChecks) => {
377
+ if (typeof otherMonorepoChecks === "object") {
378
+ for (const [monorepoType, monorepoCheck] of Object.entries(
379
+ otherMonorepoChecks
380
+ )) {
381
+ if (typeof monorepoCheck === "function" && await monorepoCheck(monorepoRootPath)) {
382
+ return {
383
+ isMonorepo: true,
384
+ type: monorepoType
385
+ };
386
+ }
387
+ }
388
+ }
389
+ if (await isPnpmMonorepo(monorepoRootPath)) {
390
+ return {
391
+ isMonorepo: true,
392
+ type: "pnpm"
393
+ };
394
+ }
395
+ if (await isRushMonorepo(monorepoRootPath)) {
396
+ return {
397
+ isMonorepo: true,
398
+ type: "rush"
399
+ };
400
+ }
401
+ return {
402
+ isMonorepo: false,
403
+ type: ""
404
+ };
405
+ };
406
+
407
+ // src/common/getBaseData.ts
408
+ var getMonorepoBaseData = async (starFindPath, otherMonorepoAnalyzer) => {
409
+ let repoIsMonorepo = false;
410
+ let findPath = starFindPath;
411
+ let type = "";
412
+ let otherMonorepoChecks;
413
+ if (otherMonorepoAnalyzer) {
414
+ otherMonorepoChecks = otherMonorepoChecks ?? {};
415
+ for (const [monoType, analyzer] of Object.entries(otherMonorepoAnalyzer)) {
416
+ otherMonorepoChecks[monoType] = analyzer.check;
417
+ }
418
+ }
419
+ while (true) {
420
+ const result = await isMonorepo(findPath, otherMonorepoChecks);
421
+ if (result.isMonorepo) {
422
+ repoIsMonorepo = true;
423
+ ({ type } = result);
424
+ break;
425
+ }
426
+ if (findPath === import_node_path2.default.dirname(findPath)) {
427
+ break;
428
+ }
429
+ findPath = import_node_path2.default.dirname(findPath);
430
+ }
431
+ return {
432
+ isMonorepo: repoIsMonorepo,
433
+ rootPath: repoIsMonorepo ? findPath : "",
434
+ type,
435
+ getProjects: otherMonorepoAnalyzer?.[type]?.getProjects
436
+ };
437
+ };
438
+
439
+ // src/common/getProjects.ts
440
+ var getMonorepoSubProjects = async (monorepoBaseData) => {
441
+ const { type, rootPath, getProjects: getProjects3 } = monorepoBaseData;
442
+ if (type === "pnpm") {
443
+ const { getProjects: getPnpmMonorepoSubProjects } = await Promise.resolve().then(() => (init_pnpm(), pnpm_exports));
444
+ return getPnpmMonorepoSubProjects(rootPath);
445
+ }
446
+ if (type === "rush") {
447
+ const { getProjects: getRushMonorepoSubProjects } = await Promise.resolve().then(() => (init_rush(), rush_exports));
448
+ return getRushMonorepoSubProjects(rootPath);
449
+ }
450
+ if (getProjects3) {
451
+ return getProjects3(rootPath);
452
+ }
453
+ return [];
454
+ };
455
+
456
+ // src/common/index.ts
457
+ init_pnpm();
458
+ init_rush();
459
+
460
+ // src/project-utils/getDependentProjects.ts
461
+ init_utils();
462
+
463
+ // src/project-utils/filter.ts
464
+ function hasExportsSourceField(exportsConfig, sourceField) {
465
+ return Object.values(exportsConfig).some(
466
+ (moduleRules) => typeof moduleRules === "object" && typeof moduleRules[sourceField] === "string"
467
+ );
468
+ }
469
+ var defaultFilter = (projects) => projects;
470
+ var filterByField = (fieldName, checkExports) => (projects) => {
471
+ return projects.filter((p) => {
472
+ return fieldName in p.metaData || checkExports && hasExportsSourceField(p.metaData.exports || {}, fieldName);
473
+ });
474
+ };
475
+
476
+ // src/project-utils/getDependentProjects.ts
477
+ var filterProjects = async (projects, filter) => {
478
+ if (!filter) {
479
+ return defaultFilter(projects);
480
+ }
481
+ return filter(projects);
482
+ };
483
+ var getDependentProjects = async (projectNameOrRootPath, options) => {
484
+ const {
485
+ cwd = process.cwd(),
486
+ recursive,
487
+ filter,
488
+ extraMonorepoStrategies
489
+ } = options;
490
+ const currentProjectPkgJsonPath = import_node_path7.default.join(
491
+ projectNameOrRootPath,
492
+ "package.json"
493
+ );
494
+ let projectName;
495
+ if (await import_shared4.fse.pathExists(currentProjectPkgJsonPath)) {
496
+ ({ name: projectName } = await readPackageJson(currentProjectPkgJsonPath));
497
+ } else {
498
+ projectName = projectNameOrRootPath;
499
+ }
500
+ const monoBaseData = await getMonorepoBaseData(cwd, extraMonorepoStrategies);
501
+ if (!monoBaseData.isMonorepo) {
502
+ return [];
503
+ }
504
+ const projects = await getMonorepoSubProjects(monoBaseData);
505
+ const currentProject = projects.find(
506
+ (project) => project.name === projectName
507
+ );
508
+ if (!currentProject) {
509
+ return [];
510
+ }
511
+ let dependentProjects = currentProject.getDependentProjects(projects, {
512
+ recursive
513
+ });
514
+ dependentProjects = await filterProjects(dependentProjects, filter);
515
+ return dependentProjects;
516
+ };
517
+
518
+ // src/plugin.ts
42
519
  var PLUGIN_SOURCE_BUILD_NAME = "rsbuild:source-build";
43
520
  var getSourceInclude = async (options) => {
44
521
  const { projects, sourceField } = options;
@@ -63,10 +540,10 @@ function pluginSourceBuild(options) {
63
540
  const projectRootPath = api.context.rootPath;
64
541
  let projects = [];
65
542
  api.modifyRsbuildConfig(async (config) => {
66
- projects = await (0, import_monorepo_utils.getDependentProjects)(projectName || projectRootPath, {
543
+ projects = await getDependentProjects(projectName || projectRootPath, {
67
544
  cwd: projectRootPath,
68
545
  recursive: true,
69
- filter: (0, import_monorepo_utils.filterByField)(sourceField, true),
546
+ filter: filterByField(sourceField, true),
70
547
  extraMonorepoStrategies
71
548
  });
72
549
  const includes = await getSourceInclude({
@@ -92,11 +569,11 @@ function pluginSourceBuild(options) {
92
569
  }
93
570
  });
94
571
  const getReferences = async () => {
95
- const refers = projects.map((project) => import_node_path.default.join(project.dir, import_shared.TS_CONFIG_FILE)).filter((filePath) => import_node_fs.default.existsSync(filePath));
572
+ const refers = projects.map((project) => import_node_path8.default.join(project.dir, import_shared5.TS_CONFIG_FILE)).filter((filePath) => import_node_fs2.default.existsSync(filePath));
96
573
  if (api.context.tsconfigPath) {
97
- const { default: json5 } = await import("@rsbuild/shared/json5");
98
- const { references } = json5.parse(
99
- import_shared.fse.readFileSync(api.context.tsconfigPath, "utf-8")
574
+ const { default: json52 } = await import("@rsbuild/shared/json5");
575
+ const { references } = json52.parse(
576
+ import_shared5.fse.readFileSync(api.context.tsconfigPath, "utf-8")
100
577
  );
101
578
  return Array.isArray(references) ? references.map((r) => r.path).filter(Boolean).concat(refers) : refers;
102
579
  }
@@ -132,9 +609,14 @@ function pluginSourceBuild(options) {
132
609
  }
133
610
  };
134
611
  }
612
+
613
+ // src/project/index.ts
614
+ init_project();
135
615
  // Annotate the CommonJS export names for ESM import in node:
136
616
  0 && (module.exports = {
137
617
  PLUGIN_SOURCE_BUILD_NAME,
138
- getSourceInclude,
618
+ Project,
619
+ getMonorepoBaseData,
620
+ getMonorepoSubProjects,
139
621
  pluginSourceBuild
140
622
  });
package/dist/index.d.ts CHANGED
@@ -1,11 +1,193 @@
1
1
  import { RsbuildPlugin } from '@rsbuild/core';
2
- import { Project, ExtraMonorepoStrategies } from '@rsbuild/monorepo-utils';
2
+
3
+ /**
4
+ * The following code is modified based on
5
+ * https://github.com/microsoft/rushstack/blob/main/libraries/node-core-library/src/IPackageJson.ts
6
+ *
7
+ * MIT Licensed
8
+ * Copyright (c) Microsoft Corporation
9
+ * https://github.com/microsoft/rushstack/blob/main/LICENSE
10
+ */
11
+ /**
12
+ * This interface is part of the {@link IPackageJson} file format. It is used for the
13
+ * "dependencies", "optionalDependencies", and "devDependencies" fields.
14
+ * @public
15
+ */
16
+ interface IPackageJsonDependencyTable {
17
+ /**
18
+ * The key is the name of a dependency. The value is a Semantic Versioning (SemVer)
19
+ * range specifier.
20
+ */
21
+ [dependencyName: string]: string;
22
+ }
23
+ interface IPackageJsonRepository {
24
+ /**
25
+ * The source control type for the repository that hosts the project. This is typically "git".
26
+ */
27
+ type: string;
28
+ /**
29
+ * The URL of the repository that hosts the project.
30
+ */
31
+ url: string;
32
+ /**
33
+ * If the project does not exist at the root of the repository, its path is specified here.
34
+ */
35
+ directory?: string;
36
+ }
37
+ /**
38
+ * This interface is part of the {@link IPackageJson} file format. It is used for the
39
+ * "scripts" field.
40
+ * @public
41
+ */
42
+ interface IPackageJsonScriptTable {
43
+ /**
44
+ * The key is the name of the script hook. The value is the script body which may
45
+ * be a file path or shell script command.
46
+ */
47
+ [scriptName: string]: string;
48
+ }
49
+ /**
50
+ * This interface is part of the {@link IPackageJson} file format. It is used for the
51
+ * "peerDependenciesMeta" field.
52
+ * @public
53
+ */
54
+ interface IPeerDependenciesMetaTable {
55
+ [dependencyName: string]: {
56
+ optional?: boolean;
57
+ };
58
+ }
59
+ type ExportsModuleRules = string | Record<string, string> | Record<string, string | Record<string, string>>;
60
+ interface ExportsConfig {
61
+ [modulePath: string]: ExportsModuleRules;
62
+ }
63
+ interface INodePackageJson {
64
+ /**
65
+ * The name of the package.
66
+ */
67
+ name: string;
68
+ /**
69
+ * A version number conforming to the Semantic Versioning (SemVer) standard.
70
+ */
71
+ version?: string;
72
+ /**
73
+ * Indicates whether this package is allowed to be published or not.
74
+ */
75
+ private?: boolean;
76
+ /**
77
+ * A brief description of the package.
78
+ */
79
+ description?: string;
80
+ /**
81
+ * The URL of the project's repository.
82
+ */
83
+ repository?: string | IPackageJsonRepository;
84
+ /**
85
+ * The URL to the project's web page.
86
+ */
87
+ homepage?: string;
88
+ /**
89
+ * The name of the license.
90
+ */
91
+ license?: string;
92
+ /**
93
+ * The path to the module file that will act as the main entry point.
94
+ */
95
+ main?: string;
96
+ exports?: ExportsConfig;
97
+ /**
98
+ * The path to the TypeScript *.d.ts file describing the module file
99
+ * that will act as the main entry point.
100
+ */
101
+ types?: string;
102
+ /**
103
+ * Alias for `types`
104
+ */
105
+ typings?: string;
106
+ /**
107
+ * The path to the TSDoc metadata file.
108
+ * This is still being standardized: https://github.com/microsoft/tsdoc/issues/7#issuecomment-442271815
109
+ * @beta
110
+ */
111
+ tsdocMetadata?: string;
112
+ /**
113
+ * The main entry point for the package.
114
+ */
115
+ bin?: string;
116
+ /**
117
+ * An array of dependencies that must always be installed for this package.
118
+ */
119
+ dependencies?: IPackageJsonDependencyTable;
120
+ /**
121
+ * An array of optional dependencies that may be installed for this package.
122
+ */
123
+ optionalDependencies?: IPackageJsonDependencyTable;
124
+ /**
125
+ * An array of dependencies that must only be installed for developers who will
126
+ * build this package.
127
+ */
128
+ devDependencies?: IPackageJsonDependencyTable;
129
+ /**
130
+ * An array of dependencies that must be installed by a consumer of this package,
131
+ * but which will not be automatically installed by this package.
132
+ */
133
+ peerDependencies?: IPackageJsonDependencyTable;
134
+ /**
135
+ * An array of metadata about peer dependencies.
136
+ */
137
+ peerDependenciesMeta?: IPeerDependenciesMetaTable;
138
+ /**
139
+ * A table of script hooks that a package manager or build tool may invoke.
140
+ */
141
+ scripts?: IPackageJsonScriptTable;
142
+ /**
143
+ * A table of package version resolutions. This feature is only implemented by the Yarn package manager.
144
+ *
145
+ * @remarks
146
+ * See the {@link https://github.com/yarnpkg/rfcs/blob/master/implemented/0000-selective-versions-resolutions.md
147
+ * | 0000-selective-versions-resolutions.md RFC} for details.
148
+ */
149
+ resolutions?: Record<string, string>;
150
+ }
151
+
152
+ declare class Project {
153
+ #private;
154
+ name: string;
155
+ dir: string;
156
+ metaData: INodePackageJson;
157
+ constructor(name: string, dir: string);
158
+ init(): Promise<void>;
159
+ getMetaData(): INodePackageJson;
160
+ getDependentProjects(monorepoProjects: Project[], options?: {
161
+ recursive?: boolean;
162
+ }): Project[];
163
+ getDirectDependentProjects(allProjectMap: Map<string, Project>): Project[];
164
+ getSourceEntryPaths(options?: {
165
+ field?: string;
166
+ exports?: boolean;
167
+ }): string[];
168
+ }
169
+
170
+ interface IMonorepoBaseData {
171
+ isMonorepo: boolean;
172
+ type: string;
173
+ rootPath: string;
174
+ getProjects?: GetProjectsFunc;
175
+ }
176
+ declare const getMonorepoBaseData: (starFindPath: string, otherMonorepoAnalyzer?: Record<string, MonorepoAnalyzer>) => Promise<IMonorepoBaseData>;
177
+
178
+ type GetProjectsFunc = (rootPath: string) => Promise<Project[]> | Project[];
179
+ declare const getMonorepoSubProjects: (monorepoBaseData: IMonorepoBaseData) => Promise<Project[]>;
180
+
181
+ type IsMonorepoFn = (monorepoRootPath: string) => Promise<boolean> | boolean;
182
+
183
+ interface MonorepoAnalyzer {
184
+ check: IsMonorepoFn;
185
+ getProjects: GetProjectsFunc;
186
+ }
187
+
188
+ type ExtraMonorepoStrategies = Record<string, MonorepoAnalyzer>;
3
189
 
4
190
  declare const PLUGIN_SOURCE_BUILD_NAME = "rsbuild:source-build";
5
- declare const getSourceInclude: (options: {
6
- projects: Project[];
7
- sourceField: string;
8
- }) => Promise<string[]>;
9
191
  interface PluginSourceBuildOptions {
10
192
  /**
11
193
  * Used to configure the resolve field of the source code files.
@@ -22,4 +204,4 @@ interface PluginSourceBuildOptions {
22
204
  }
23
205
  declare function pluginSourceBuild(options?: PluginSourceBuildOptions): RsbuildPlugin;
24
206
 
25
- export { PLUGIN_SOURCE_BUILD_NAME, type PluginSourceBuildOptions, getSourceInclude, pluginSourceBuild };
207
+ export { type MonorepoAnalyzer, PLUGIN_SOURCE_BUILD_NAME, type PluginSourceBuildOptions, Project, getMonorepoBaseData, getMonorepoSubProjects, pluginSourceBuild };
package/dist/index.js CHANGED
@@ -1,19 +1,520 @@
1
1
  import { createRequire } from 'module';
2
2
  var require = createRequire(import.meta['url']);
3
3
 
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __esm = (fn, res) => function __init() {
8
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
9
+ };
10
+ var __export = (target, all) => {
11
+ for (var name in all)
12
+ __defProp(target, name, { get: all[name], enumerable: true });
13
+ };
14
+ var __publicField = (obj, key, value) => {
15
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
16
+ return value;
17
+ };
18
+ var __accessCheck = (obj, member, msg) => {
19
+ if (!member.has(obj))
20
+ throw TypeError("Cannot " + msg);
21
+ };
22
+ var __privateAdd = (obj, member, value) => {
23
+ if (member.has(obj))
24
+ throw TypeError("Cannot add the same private member more than once");
25
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
26
+ };
27
+ var __privateMethod = (obj, member, method) => {
28
+ __accessCheck(obj, member, "access private method");
29
+ return method;
30
+ };
4
31
 
5
32
  // ../../node_modules/.pnpm/@modern-js+module-tools@2.49.3_eslint@8.57.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js
6
33
  import { fileURLToPath } from "url";
7
34
  import path from "path";
35
+ var init_esm = __esm({
36
+ "../../node_modules/.pnpm/@modern-js+module-tools@2.49.3_eslint@8.57.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js"() {
37
+ "use strict";
38
+ }
39
+ });
8
40
 
9
- // src/index.ts
41
+ // src/constants.ts
42
+ var PNPM_WORKSPACE_FILE, RUSH_JSON_FILE, PACKAGE_JSON;
43
+ var init_constants = __esm({
44
+ "src/constants.ts"() {
45
+ "use strict";
46
+ init_esm();
47
+ PNPM_WORKSPACE_FILE = "pnpm-workspace.yaml";
48
+ RUSH_JSON_FILE = "rush.json";
49
+ PACKAGE_JSON = "package.json";
50
+ }
51
+ });
52
+
53
+ // src/utils.ts
54
+ import path4 from "path";
55
+ import { fse as fse2 } from "@rsbuild/shared";
56
+ import json5 from "@rsbuild/shared/json5";
57
+ var readPackageJson, readRushJson, readJson;
58
+ var init_utils = __esm({
59
+ "src/utils.ts"() {
60
+ "use strict";
61
+ init_esm();
62
+ init_constants();
63
+ readPackageJson = async (pkgJsonFilePath) => {
64
+ return readJson(pkgJsonFilePath);
65
+ };
66
+ readRushJson = async (rushJsonFilePath) => {
67
+ const rushJson = readJson(
68
+ rushJsonFilePath.includes(RUSH_JSON_FILE) ? rushJsonFilePath : path4.join(rushJsonFilePath, RUSH_JSON_FILE)
69
+ );
70
+ return rushJson;
71
+ };
72
+ readJson = async (jsonFileAbsPath) => {
73
+ if (!await fse2.pathExists(jsonFileAbsPath)) {
74
+ return {};
75
+ }
76
+ const content = await fse2.readFile(jsonFileAbsPath, "utf-8");
77
+ const json = json5.parse(content);
78
+ return json;
79
+ };
80
+ }
81
+ });
82
+
83
+ // src/project/project.ts
10
84
  import fs from "fs";
85
+ import path5 from "path";
86
+ var _getExportsSourceDirs, getExportsSourceDirs_fn, _getCommonRootPaths, getCommonRootPaths_fn, _getRootPath, getRootPath_fn, Project;
87
+ var init_project = __esm({
88
+ "src/project/project.ts"() {
89
+ "use strict";
90
+ init_esm();
91
+ init_constants();
92
+ init_utils();
93
+ Project = class {
94
+ constructor(name, dir) {
95
+ __privateAdd(this, _getExportsSourceDirs);
96
+ /**
97
+ *
98
+ * @param paths normalize paths
99
+ * @returns common root paths
100
+ */
101
+ __privateAdd(this, _getCommonRootPaths);
102
+ __privateAdd(this, _getRootPath);
103
+ __publicField(this, "name");
104
+ __publicField(this, "dir");
105
+ __publicField(this, "metaData");
106
+ this.name = name;
107
+ this.dir = dir;
108
+ }
109
+ async init() {
110
+ this.metaData = await readPackageJson(path5.join(this.dir, PACKAGE_JSON));
111
+ }
112
+ getMetaData() {
113
+ if (this.metaData === null) {
114
+ throw new Error(
115
+ "The Project object needs to be initialized by executing the `init` function"
116
+ );
117
+ }
118
+ return this.metaData;
119
+ }
120
+ getDependentProjects(monorepoProjects, options) {
121
+ const { recursive } = options ?? { recursive: false };
122
+ const allProjectMap = /* @__PURE__ */ new Map();
123
+ for (const project of monorepoProjects) {
124
+ allProjectMap.set(project.name, project);
125
+ }
126
+ if (!recursive) {
127
+ return this.getDirectDependentProjects(allProjectMap);
128
+ }
129
+ const computedSet = /* @__PURE__ */ new Set();
130
+ computedSet.add(this.name);
131
+ const queue = this.getDirectDependentProjects(allProjectMap).filter(
132
+ (p) => !computedSet.has(p.name)
133
+ );
134
+ const result = [];
135
+ while (queue.length > 0) {
136
+ const item = queue.shift();
137
+ if (computedSet.has(item.name)) {
138
+ continue;
139
+ }
140
+ result.push(item);
141
+ computedSet.add(item.name);
142
+ const newDeps = item.getDirectDependentProjects(allProjectMap);
143
+ if (newDeps.length > 0) {
144
+ queue.push(...newDeps);
145
+ }
146
+ }
147
+ return result;
148
+ }
149
+ getDirectDependentProjects(allProjectMap) {
150
+ const pkgJson = this.getMetaData();
151
+ const { dependencies = {}, devDependencies = {} } = pkgJson;
152
+ const projects = [];
153
+ for (const d of Object.keys(dependencies)) {
154
+ if (allProjectMap.has(d)) {
155
+ projects.push(allProjectMap.get(d));
156
+ }
157
+ }
158
+ for (const d of Object.keys(devDependencies)) {
159
+ if (allProjectMap.has(d)) {
160
+ projects.push(allProjectMap.get(d));
161
+ }
162
+ }
163
+ return projects;
164
+ }
165
+ getSourceEntryPaths(options) {
166
+ const { exports: checkExports = false, field: sourceField = "source" } = options ?? {};
167
+ const pkgJson = this.getMetaData();
168
+ const sourceDirs = pkgJson[sourceField] ? [path5.normalize(pkgJson[sourceField])] : [];
169
+ if (checkExports) {
170
+ const exportsSourceDirs = __privateMethod(this, _getExportsSourceDirs, getExportsSourceDirs_fn).call(this, pkgJson.exports ?? {}, sourceField);
171
+ sourceDirs.push(...exportsSourceDirs);
172
+ }
173
+ if (!sourceDirs.length) {
174
+ throw new Error(
175
+ `"${sourceField}" field is not found in ${this.name} package.json`
176
+ );
177
+ }
178
+ return __privateMethod(this, _getCommonRootPaths, getCommonRootPaths_fn).call(this, sourceDirs);
179
+ }
180
+ };
181
+ _getExportsSourceDirs = new WeakSet();
182
+ getExportsSourceDirs_fn = function(exportsConfig, sourceField) {
183
+ const exportsSourceDirs = [];
184
+ for (const moduleRules of Object.values(exportsConfig)) {
185
+ if (typeof moduleRules === "object" && typeof moduleRules[sourceField] === "string") {
186
+ exportsSourceDirs.push(
187
+ path5.normalize(moduleRules[sourceField])
188
+ );
189
+ }
190
+ }
191
+ return exportsSourceDirs;
192
+ };
193
+ _getCommonRootPaths = new WeakSet();
194
+ getCommonRootPaths_fn = function(paths) {
195
+ const commonRootPathsSet = /* @__PURE__ */ new Set();
196
+ for (const p of paths) {
197
+ let dir;
198
+ try {
199
+ dir = fs.statSync(p).isDirectory() ? p : path5.dirname(p);
200
+ } catch {
201
+ dir = path5.dirname(p);
202
+ }
203
+ const rootPath = __privateMethod(this, _getRootPath, getRootPath_fn).call(this, dir);
204
+ if (!commonRootPathsSet.has(rootPath)) {
205
+ commonRootPathsSet.add(rootPath);
206
+ }
207
+ }
208
+ return Array.from(commonRootPathsSet).map((p) => path5.join(this.dir, p));
209
+ };
210
+ _getRootPath = new WeakSet();
211
+ getRootPath_fn = function(p) {
212
+ return p.split(path5.sep)[0];
213
+ };
214
+ }
215
+ });
216
+
217
+ // src/common/pnpm.ts
218
+ var pnpm_exports = {};
219
+ __export(pnpm_exports, {
220
+ getPatternsFromYaml: () => getPatternsFromYaml,
221
+ getProjects: () => getProjects,
222
+ makeFileFinder: () => makeFileFinder,
223
+ normalize: () => normalize,
224
+ readPnpmProjects: () => readPnpmProjects
225
+ });
226
+ import path6 from "path";
227
+ import { fse as fse3 } from "@rsbuild/shared";
228
+ import { parse } from "@rsbuild/shared/yaml";
229
+ import glob from "fast-glob";
230
+ var getPatternsFromYaml, normalize, getGlobOpts, makeFileFinder, readPnpmProjects, getProjects;
231
+ var init_pnpm = __esm({
232
+ "src/common/pnpm.ts"() {
233
+ "use strict";
234
+ init_esm();
235
+ init_constants();
236
+ init_project();
237
+ init_utils();
238
+ getPatternsFromYaml = async (monorepoRoot) => {
239
+ const workspaceYamlFilePath = path6.join(monorepoRoot, PNPM_WORKSPACE_FILE);
240
+ const yamlContent = await fse3.readFile(workspaceYamlFilePath, "utf8");
241
+ const pnpmWorkspace = parse(yamlContent);
242
+ return pnpmWorkspace.packages || [];
243
+ };
244
+ normalize = (results) => results.map((fp) => path6.normalize(fp));
245
+ getGlobOpts = (rootPath, patterns) => {
246
+ const globOpts = {
247
+ cwd: rootPath,
248
+ absolute: true,
249
+ followSymbolicLinks: false
250
+ };
251
+ if (patterns.some((cfg) => cfg.includes("**") || cfg.includes("*"))) {
252
+ globOpts.ignore = [
253
+ // allow globs like "packages/**" or "packages/*",
254
+ // but avoid picking up node_modules/**/package.json and dist/**/package.json
255
+ "**/dist/**",
256
+ "**/node_modules/**"
257
+ ];
258
+ }
259
+ return globOpts;
260
+ };
261
+ makeFileFinder = (rootPath, patterns) => {
262
+ const globOpts = getGlobOpts(rootPath, patterns);
263
+ return async (fileName, fileMapper) => {
264
+ let result = await glob(
265
+ patterns.map((globPath) => path6.posix.join(globPath, fileName)),
266
+ globOpts
267
+ );
268
+ result = result.sort();
269
+ result = normalize(result);
270
+ return fileMapper(result);
271
+ };
272
+ };
273
+ readPnpmProjects = async (monorepoRoot, patterns) => {
274
+ const finder = makeFileFinder(monorepoRoot, patterns);
275
+ const mapper = async (pkgJsonFilePath) => {
276
+ const pkgJson = await readPackageJson(pkgJsonFilePath);
277
+ return {
278
+ dir: path6.dirname(pkgJsonFilePath),
279
+ manifest: pkgJson
280
+ };
281
+ };
282
+ const projects = await finder(
283
+ PACKAGE_JSON,
284
+ (filePaths) => Promise.all(filePaths.map(mapper))
285
+ );
286
+ return projects;
287
+ };
288
+ getProjects = async (monorepoRoot) => {
289
+ const patterns = await getPatternsFromYaml(monorepoRoot);
290
+ const pnpmProjects = await readPnpmProjects(monorepoRoot, patterns);
291
+ return Promise.all(
292
+ pnpmProjects.filter((p) => p.manifest.name).map(async (p) => {
293
+ const project = new Project(p.manifest.name, p.dir);
294
+ await project.init();
295
+ return project;
296
+ })
297
+ );
298
+ };
299
+ }
300
+ });
301
+
302
+ // src/common/rush.ts
303
+ var rush_exports = {};
304
+ __export(rush_exports, {
305
+ getProjects: () => getProjects2
306
+ });
307
+ import path7 from "path";
308
+ var getProjects2;
309
+ var init_rush = __esm({
310
+ "src/common/rush.ts"() {
311
+ "use strict";
312
+ init_esm();
313
+ init_project();
314
+ init_utils();
315
+ getProjects2 = async (monorepoRoot) => {
316
+ const rushConfiguration = await readRushJson(monorepoRoot);
317
+ const { projects = [] } = rushConfiguration;
318
+ return Promise.all(
319
+ projects.map(async (p) => {
320
+ const project = new Project(
321
+ p.packageName,
322
+ path7.join(monorepoRoot, p.projectFolder)
323
+ );
324
+ await project.init();
325
+ return project;
326
+ })
327
+ );
328
+ };
329
+ }
330
+ });
331
+
332
+ // src/index.ts
333
+ init_esm();
334
+
335
+ // src/plugin.ts
336
+ init_esm();
337
+ import fs2 from "fs";
338
+ import path9 from "path";
339
+ import { TS_CONFIG_FILE, fse as fse5 } from "@rsbuild/shared";
340
+
341
+ // src/project-utils/index.ts
342
+ init_esm();
343
+
344
+ // src/project-utils/getDependentProjects.ts
345
+ init_esm();
346
+ import path8 from "path";
347
+ import { fse as fse4 } from "@rsbuild/shared";
348
+
349
+ // src/common/index.ts
350
+ init_esm();
351
+
352
+ // src/common/getBaseData.ts
353
+ init_esm();
354
+ import path3 from "path";
355
+
356
+ // src/common/isMonorepo.ts
357
+ init_esm();
358
+ init_constants();
11
359
  import path2 from "path";
12
- import {
13
- filterByField,
14
- getDependentProjects
15
- } from "@rsbuild/monorepo-utils";
16
- import { TS_CONFIG_FILE, fse } from "@rsbuild/shared";
360
+ import { fse } from "@rsbuild/shared";
361
+ var isPnpmMonorepo = async (monorepoRootPath) => {
362
+ const existPnpmWorkspaceFile = await fse.pathExists(
363
+ path2.join(monorepoRootPath, PNPM_WORKSPACE_FILE)
364
+ );
365
+ return existPnpmWorkspaceFile;
366
+ };
367
+ var isRushMonorepo = async (monorepoRootPath) => {
368
+ const existRushJsonFile = await fse.pathExists(
369
+ path2.join(monorepoRootPath, RUSH_JSON_FILE)
370
+ );
371
+ return existRushJsonFile;
372
+ };
373
+ var isMonorepo = async (monorepoRootPath, otherMonorepoChecks) => {
374
+ if (typeof otherMonorepoChecks === "object") {
375
+ for (const [monorepoType, monorepoCheck] of Object.entries(
376
+ otherMonorepoChecks
377
+ )) {
378
+ if (typeof monorepoCheck === "function" && await monorepoCheck(monorepoRootPath)) {
379
+ return {
380
+ isMonorepo: true,
381
+ type: monorepoType
382
+ };
383
+ }
384
+ }
385
+ }
386
+ if (await isPnpmMonorepo(monorepoRootPath)) {
387
+ return {
388
+ isMonorepo: true,
389
+ type: "pnpm"
390
+ };
391
+ }
392
+ if (await isRushMonorepo(monorepoRootPath)) {
393
+ return {
394
+ isMonorepo: true,
395
+ type: "rush"
396
+ };
397
+ }
398
+ return {
399
+ isMonorepo: false,
400
+ type: ""
401
+ };
402
+ };
403
+
404
+ // src/common/getBaseData.ts
405
+ var getMonorepoBaseData = async (starFindPath, otherMonorepoAnalyzer) => {
406
+ let repoIsMonorepo = false;
407
+ let findPath = starFindPath;
408
+ let type = "";
409
+ let otherMonorepoChecks;
410
+ if (otherMonorepoAnalyzer) {
411
+ otherMonorepoChecks = otherMonorepoChecks ?? {};
412
+ for (const [monoType, analyzer] of Object.entries(otherMonorepoAnalyzer)) {
413
+ otherMonorepoChecks[monoType] = analyzer.check;
414
+ }
415
+ }
416
+ while (true) {
417
+ const result = await isMonorepo(findPath, otherMonorepoChecks);
418
+ if (result.isMonorepo) {
419
+ repoIsMonorepo = true;
420
+ ({ type } = result);
421
+ break;
422
+ }
423
+ if (findPath === path3.dirname(findPath)) {
424
+ break;
425
+ }
426
+ findPath = path3.dirname(findPath);
427
+ }
428
+ return {
429
+ isMonorepo: repoIsMonorepo,
430
+ rootPath: repoIsMonorepo ? findPath : "",
431
+ type,
432
+ getProjects: otherMonorepoAnalyzer?.[type]?.getProjects
433
+ };
434
+ };
435
+
436
+ // src/common/getProjects.ts
437
+ init_esm();
438
+ var getMonorepoSubProjects = async (monorepoBaseData) => {
439
+ const { type, rootPath, getProjects: getProjects3 } = monorepoBaseData;
440
+ if (type === "pnpm") {
441
+ const { getProjects: getPnpmMonorepoSubProjects } = await Promise.resolve().then(() => (init_pnpm(), pnpm_exports));
442
+ return getPnpmMonorepoSubProjects(rootPath);
443
+ }
444
+ if (type === "rush") {
445
+ const { getProjects: getRushMonorepoSubProjects } = await Promise.resolve().then(() => (init_rush(), rush_exports));
446
+ return getRushMonorepoSubProjects(rootPath);
447
+ }
448
+ if (getProjects3) {
449
+ return getProjects3(rootPath);
450
+ }
451
+ return [];
452
+ };
453
+
454
+ // src/common/index.ts
455
+ init_pnpm();
456
+ init_rush();
457
+
458
+ // src/project-utils/getDependentProjects.ts
459
+ init_utils();
460
+
461
+ // src/project-utils/filter.ts
462
+ init_esm();
463
+ function hasExportsSourceField(exportsConfig, sourceField) {
464
+ return Object.values(exportsConfig).some(
465
+ (moduleRules) => typeof moduleRules === "object" && typeof moduleRules[sourceField] === "string"
466
+ );
467
+ }
468
+ var defaultFilter = (projects) => projects;
469
+ var filterByField = (fieldName, checkExports) => (projects) => {
470
+ return projects.filter((p) => {
471
+ return fieldName in p.metaData || checkExports && hasExportsSourceField(p.metaData.exports || {}, fieldName);
472
+ });
473
+ };
474
+
475
+ // src/project-utils/getDependentProjects.ts
476
+ var filterProjects = async (projects, filter) => {
477
+ if (!filter) {
478
+ return defaultFilter(projects);
479
+ }
480
+ return filter(projects);
481
+ };
482
+ var getDependentProjects = async (projectNameOrRootPath, options) => {
483
+ const {
484
+ cwd = process.cwd(),
485
+ recursive,
486
+ filter,
487
+ extraMonorepoStrategies
488
+ } = options;
489
+ const currentProjectPkgJsonPath = path8.join(
490
+ projectNameOrRootPath,
491
+ "package.json"
492
+ );
493
+ let projectName;
494
+ if (await fse4.pathExists(currentProjectPkgJsonPath)) {
495
+ ({ name: projectName } = await readPackageJson(currentProjectPkgJsonPath));
496
+ } else {
497
+ projectName = projectNameOrRootPath;
498
+ }
499
+ const monoBaseData = await getMonorepoBaseData(cwd, extraMonorepoStrategies);
500
+ if (!monoBaseData.isMonorepo) {
501
+ return [];
502
+ }
503
+ const projects = await getMonorepoSubProjects(monoBaseData);
504
+ const currentProject = projects.find(
505
+ (project) => project.name === projectName
506
+ );
507
+ if (!currentProject) {
508
+ return [];
509
+ }
510
+ let dependentProjects = currentProject.getDependentProjects(projects, {
511
+ recursive
512
+ });
513
+ dependentProjects = await filterProjects(dependentProjects, filter);
514
+ return dependentProjects;
515
+ };
516
+
517
+ // src/plugin.ts
17
518
  var PLUGIN_SOURCE_BUILD_NAME = "rsbuild:source-build";
18
519
  var getSourceInclude = async (options) => {
19
520
  const { projects, sourceField } = options;
@@ -67,11 +568,11 @@ function pluginSourceBuild(options) {
67
568
  }
68
569
  });
69
570
  const getReferences = async () => {
70
- const refers = projects.map((project) => path2.join(project.dir, TS_CONFIG_FILE)).filter((filePath) => fs.existsSync(filePath));
571
+ const refers = projects.map((project) => path9.join(project.dir, TS_CONFIG_FILE)).filter((filePath) => fs2.existsSync(filePath));
71
572
  if (api.context.tsconfigPath) {
72
- const { default: json5 } = await import("@rsbuild/shared/json5");
73
- const { references } = json5.parse(
74
- fse.readFileSync(api.context.tsconfigPath, "utf-8")
573
+ const { default: json52 } = await import("@rsbuild/shared/json5");
574
+ const { references } = json52.parse(
575
+ fse5.readFileSync(api.context.tsconfigPath, "utf-8")
75
576
  );
76
577
  return Array.isArray(references) ? references.map((r) => r.path).filter(Boolean).concat(refers) : refers;
77
578
  }
@@ -107,8 +608,14 @@ function pluginSourceBuild(options) {
107
608
  }
108
609
  };
109
610
  }
611
+
612
+ // src/project/index.ts
613
+ init_esm();
614
+ init_project();
110
615
  export {
111
616
  PLUGIN_SOURCE_BUILD_NAME,
112
- getSourceInclude,
617
+ Project,
618
+ getMonorepoBaseData,
619
+ getMonorepoSubProjects,
113
620
  pluginSourceBuild
114
621
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-source-build",
3
- "version": "0.7.0-beta.3",
3
+ "version": "0.7.0-beta.5",
4
4
  "description": "Source build plugin of Rsbuild",
5
5
  "homepage": "https://rsbuild.dev",
6
6
  "repository": {
@@ -23,18 +23,18 @@
23
23
  "dist"
24
24
  ],
25
25
  "dependencies": {
26
- "@rsbuild/monorepo-utils": "0.7.0-beta.3",
27
- "@rsbuild/shared": "0.7.0-beta.3"
26
+ "fast-glob": "^3.3.2",
27
+ "@rsbuild/shared": "0.7.0-beta.5"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@babel/core": "^7.24.5",
31
31
  "typescript": "^5.4.2",
32
- "@rsbuild/core": "0.7.0-beta.3",
33
- "@rsbuild/plugin-babel": "0.7.0-beta.3",
34
- "@scripts/test-helper": "0.7.0-beta.3"
32
+ "@rsbuild/core": "0.7.0-beta.5",
33
+ "@rsbuild/plugin-babel": "0.7.0-beta.5",
34
+ "@scripts/test-helper": "0.7.0-beta.5"
35
35
  },
36
36
  "peerDependencies": {
37
- "@rsbuild/core": "^0.7.0-beta.3"
37
+ "@rsbuild/core": "^0.7.0-beta.5"
38
38
  },
39
39
  "publishConfig": {
40
40
  "access": "public",