@rsbuild/plugin-source-build 1.0.1 → 1.0.3

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
@@ -1,526 +1,377 @@
1
- // src/plugin.ts
2
- import fs6 from "node:fs";
3
- import path8 from "node:path";
4
- import json52 from "json5";
5
-
6
- // src/project-utils/getDependentProjects.ts
7
- import fs5 from "node:fs";
8
- import path7 from "node:path";
9
-
10
- // src/common/getBaseData.ts
11
- import path2 from "node:path";
12
-
13
- // src/common/isMonorepo.ts
14
- import fs from "node:fs";
15
- import path from "node:path";
16
-
17
- // src/constants.ts
18
- var PNPM_WORKSPACE_FILE = "pnpm-workspace.yaml";
19
- var RUSH_JSON_FILE = "rush.json";
20
- var PACKAGE_JSON = "package.json";
21
-
22
- // src/common/isMonorepo.ts
23
- async function pathExists(path9) {
24
- return fs.promises.access(path9).then(() => true).catch(() => false);
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
+ const PNPM_WORKSPACE_FILE = 'pnpm-workspace.yaml';
6
+ const RUSH_JSON_FILE = 'rush.json';
7
+ const PACKAGE_JSON = 'package.json';
8
+ async function pathExists(path) {
9
+ return node_fs.promises.access(path).then(()=>true).catch(()=>false);
25
10
  }
26
- var isPnpmMonorepo = async (monorepoRootPath) => {
27
- const existPnpmWorkspaceFile = await pathExists(
28
- path.join(monorepoRootPath, PNPM_WORKSPACE_FILE)
29
- );
30
- return existPnpmWorkspaceFile;
11
+ const isPnpmMonorepo = async (monorepoRootPath)=>{
12
+ const existPnpmWorkspaceFile = await pathExists(node_path.join(monorepoRootPath, PNPM_WORKSPACE_FILE));
13
+ return existPnpmWorkspaceFile;
31
14
  };
32
- var isRushMonorepo = async (monorepoRootPath) => {
33
- const existRushJsonFile = await pathExists(
34
- path.join(monorepoRootPath, RUSH_JSON_FILE)
35
- );
36
- return existRushJsonFile;
15
+ const isRushMonorepo = async (monorepoRootPath)=>{
16
+ const existRushJsonFile = await pathExists(node_path.join(monorepoRootPath, RUSH_JSON_FILE));
17
+ return existRushJsonFile;
37
18
  };
38
- var isMonorepo = async (monorepoRootPath, otherMonorepoChecks) => {
39
- if (typeof otherMonorepoChecks === "object") {
40
- for (const [monorepoType, monorepoCheck] of Object.entries(
41
- otherMonorepoChecks
42
- )) {
43
- if (typeof monorepoCheck === "function" && await monorepoCheck(monorepoRootPath)) {
44
- return {
45
- isMonorepo: true,
46
- type: monorepoType
19
+ const isMonorepo = async (monorepoRootPath, otherMonorepoChecks)=>{
20
+ if ('object' == typeof otherMonorepoChecks) {
21
+ for (const [monorepoType, monorepoCheck] of Object.entries(otherMonorepoChecks))if ('function' == typeof monorepoCheck && await monorepoCheck(monorepoRootPath)) return {
22
+ isMonorepo: true,
23
+ type: monorepoType
47
24
  };
48
- }
49
25
  }
50
- }
51
- if (await isPnpmMonorepo(monorepoRootPath)) {
52
- return {
53
- isMonorepo: true,
54
- type: "pnpm"
26
+ if (await isPnpmMonorepo(monorepoRootPath)) return {
27
+ isMonorepo: true,
28
+ type: 'pnpm'
29
+ };
30
+ if (await isRushMonorepo(monorepoRootPath)) return {
31
+ isMonorepo: true,
32
+ type: 'rush'
55
33
  };
56
- }
57
- if (await isRushMonorepo(monorepoRootPath)) {
58
34
  return {
59
- isMonorepo: true,
60
- type: "rush"
35
+ isMonorepo: false,
36
+ type: ''
61
37
  };
62
- }
63
- return {
64
- isMonorepo: false,
65
- type: ""
66
- };
67
38
  };
68
-
69
- // src/common/getBaseData.ts
70
- var getMonorepoBaseData = async (starFindPath, otherMonorepoAnalyzer) => {
71
- let repoIsMonorepo = false;
72
- let findPath = starFindPath;
73
- let type = "";
74
- let otherMonorepoChecks;
75
- if (otherMonorepoAnalyzer) {
76
- otherMonorepoChecks = otherMonorepoChecks ?? {};
77
- for (const [monoType, analyzer] of Object.entries(otherMonorepoAnalyzer)) {
78
- otherMonorepoChecks[monoType] = analyzer.check;
79
- }
80
- }
81
- while (true) {
82
- const result = await isMonorepo(findPath, otherMonorepoChecks);
83
- if (result.isMonorepo) {
84
- repoIsMonorepo = true;
85
- ({ type } = result);
86
- break;
39
+ const getMonorepoBaseData = async (starFindPath, otherMonorepoAnalyzer)=>{
40
+ var _otherMonorepoAnalyzer_type;
41
+ let repoIsMonorepo = false;
42
+ let findPath = starFindPath;
43
+ let type = '';
44
+ let otherMonorepoChecks;
45
+ if (otherMonorepoAnalyzer) {
46
+ otherMonorepoChecks = otherMonorepoChecks ?? {};
47
+ for (const [monoType, analyzer] of Object.entries(otherMonorepoAnalyzer))otherMonorepoChecks[monoType] = analyzer.check;
87
48
  }
88
- if (findPath === path2.dirname(findPath)) {
89
- break;
49
+ while(true){
50
+ const result = await isMonorepo(findPath, otherMonorepoChecks);
51
+ if (result.isMonorepo) {
52
+ repoIsMonorepo = true;
53
+ ({ type } = result);
54
+ break;
55
+ }
56
+ if (findPath === node_path.dirname(findPath)) break;
57
+ findPath = node_path.dirname(findPath);
90
58
  }
91
- findPath = path2.dirname(findPath);
92
- }
93
- return {
94
- isMonorepo: repoIsMonorepo,
95
- rootPath: repoIsMonorepo ? findPath : "",
96
- type,
97
- getProjects: otherMonorepoAnalyzer?.[type]?.getProjects
98
- };
99
- };
100
-
101
- // src/common/pnpm.ts
102
- import fs4 from "node:fs";
103
- import path5 from "node:path";
104
- import glob from "fast-glob";
105
-
106
- // src/project.ts
107
- import fs3 from "node:fs";
108
- import path4 from "node:path";
109
-
110
- // src/utils.ts
111
- import fs2 from "node:fs";
112
- import path3 from "node:path";
113
- import json5 from "json5";
114
- var readPackageJson = async (pkgJsonFilePath) => {
115
- return readJson(pkgJsonFilePath);
59
+ return {
60
+ isMonorepo: repoIsMonorepo,
61
+ rootPath: repoIsMonorepo ? findPath : '',
62
+ type,
63
+ getProjects: null == otherMonorepoAnalyzer ? void 0 : null == (_otherMonorepoAnalyzer_type = otherMonorepoAnalyzer[type]) ? void 0 : _otherMonorepoAnalyzer_type.getProjects
64
+ };
116
65
  };
117
- var readRushJson = async (rushJsonFilePath) => {
118
- const rushJson = readJson(
119
- rushJsonFilePath.includes(RUSH_JSON_FILE) ? rushJsonFilePath : path3.join(rushJsonFilePath, RUSH_JSON_FILE)
120
- );
121
- return rushJson;
66
+ const readPackageJson = async (pkgJsonFilePath)=>readJson(pkgJsonFilePath);
67
+ const readRushJson = async (rushJsonFilePath)=>{
68
+ const rushJson = readJson(rushJsonFilePath.includes(RUSH_JSON_FILE) ? rushJsonFilePath : node_path.join(rushJsonFilePath, RUSH_JSON_FILE));
69
+ return rushJson;
122
70
  };
123
- async function pathExists2(path9) {
124
- return fs2.promises.access(path9).then(() => true).catch(() => false);
71
+ async function utils_pathExists(path) {
72
+ return node_fs.promises.access(path).then(()=>true).catch(()=>false);
125
73
  }
126
- var readJson = async (jsonFileAbsPath) => {
127
- if (!await pathExists2(jsonFileAbsPath)) {
128
- return {};
129
- }
130
- const content = await fs2.promises.readFile(jsonFileAbsPath, "utf-8");
131
- const json = json5.parse(content);
132
- return json;
74
+ const readJson = async (jsonFileAbsPath)=>{
75
+ if (!await utils_pathExists(jsonFileAbsPath)) return {};
76
+ const content = await node_fs.promises.readFile(jsonFileAbsPath, 'utf-8');
77
+ const json = json5.parse(content);
78
+ return json;
133
79
  };
134
-
135
- // src/project.ts
136
- var Project = class {
137
- constructor(name, dir) {
138
- this.name = name;
139
- this.dir = dir;
140
- }
141
- async init() {
142
- this.metaData = await readPackageJson(path4.join(this.dir, PACKAGE_JSON));
143
- }
144
- getMetaData() {
145
- if (this.metaData === null) {
146
- throw new Error(
147
- "The Project object needs to be initialized by executing the `init` function"
148
- );
149
- }
150
- return this.metaData;
151
- }
152
- getDependentProjects(monorepoProjects, options) {
153
- const { recursive } = options ?? { recursive: false };
154
- const allProjectMap = /* @__PURE__ */ new Map();
155
- for (const project of monorepoProjects) {
156
- allProjectMap.set(project.name, project);
157
- }
158
- if (!recursive) {
159
- return this.getDirectDependentProjects(allProjectMap);
80
+ function _check_private_redeclaration(obj, privateCollection) {
81
+ if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object");
82
+ }
83
+ function _class_private_method_get(receiver, privateSet, fn) {
84
+ if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance");
85
+ return fn;
86
+ }
87
+ function _class_private_method_init(obj, privateSet) {
88
+ _check_private_redeclaration(obj, privateSet);
89
+ privateSet.add(obj);
90
+ }
91
+ function _define_property(obj, key, value) {
92
+ if (key in obj) Object.defineProperty(obj, key, {
93
+ value: value,
94
+ enumerable: true,
95
+ configurable: true,
96
+ writable: true
97
+ });
98
+ else obj[key] = value;
99
+ return obj;
100
+ }
101
+ var _getExportsSourceDirs = /*#__PURE__*/ new WeakSet(), _getCommonRootPaths = /*#__PURE__*/ new WeakSet(), _getRootPath = /*#__PURE__*/ new WeakSet();
102
+ class Project {
103
+ async init() {
104
+ this.metaData = await readPackageJson(node_path.join(this.dir, PACKAGE_JSON));
160
105
  }
161
- const computedSet = /* @__PURE__ */ new Set();
162
- computedSet.add(this.name);
163
- const queue = this.getDirectDependentProjects(allProjectMap).filter(
164
- (p) => !computedSet.has(p.name)
165
- );
166
- const result = [];
167
- while (queue.length > 0) {
168
- const item = queue.shift();
169
- if (computedSet.has(item.name)) {
170
- continue;
171
- }
172
- result.push(item);
173
- computedSet.add(item.name);
174
- const newDeps = item.getDirectDependentProjects(allProjectMap);
175
- if (newDeps.length > 0) {
176
- queue.push(...newDeps);
177
- }
106
+ getMetaData() {
107
+ if (null === this.metaData) throw new Error('The Project object needs to be initialized by executing the `init` function');
108
+ return this.metaData;
178
109
  }
179
- return result;
180
- }
181
- getDirectDependentProjects(allProjectMap) {
182
- const pkgJson = this.getMetaData();
183
- const { dependencies = {}, devDependencies = {} } = pkgJson;
184
- const projects = [];
185
- for (const d of Object.keys(dependencies)) {
186
- if (allProjectMap.has(d)) {
187
- projects.push(allProjectMap.get(d));
188
- }
110
+ getDependentProjects(monorepoProjects, options) {
111
+ const { recursive } = options ?? {
112
+ recursive: false
113
+ };
114
+ const allProjectMap = new Map();
115
+ for (const project of monorepoProjects)allProjectMap.set(project.name, project);
116
+ if (!recursive) return this.getDirectDependentProjects(allProjectMap);
117
+ const computedSet = new Set();
118
+ computedSet.add(this.name);
119
+ const queue = this.getDirectDependentProjects(allProjectMap).filter((p)=>!computedSet.has(p.name));
120
+ const result = [];
121
+ while(queue.length > 0){
122
+ const item = queue.shift();
123
+ if (computedSet.has(item.name)) continue;
124
+ result.push(item);
125
+ computedSet.add(item.name);
126
+ const newDeps = item.getDirectDependentProjects(allProjectMap);
127
+ if (newDeps.length > 0) queue.push(...newDeps);
128
+ }
129
+ return result;
189
130
  }
190
- for (const d of Object.keys(devDependencies)) {
191
- if (allProjectMap.has(d)) {
192
- projects.push(allProjectMap.get(d));
193
- }
131
+ getDirectDependentProjects(allProjectMap) {
132
+ const pkgJson = this.getMetaData();
133
+ const { dependencies = {}, devDependencies = {} } = pkgJson;
134
+ const projects = [];
135
+ for (const d of Object.keys(dependencies))if (allProjectMap.has(d)) projects.push(allProjectMap.get(d));
136
+ for (const d of Object.keys(devDependencies))if (allProjectMap.has(d)) projects.push(allProjectMap.get(d));
137
+ return projects;
194
138
  }
195
- return projects;
196
- }
197
- getSourceEntryPaths(options) {
198
- const { exports: checkExports = false, field: sourceField = "source" } = options ?? {};
199
- const pkgJson = this.getMetaData();
200
- const sourceDirs = pkgJson[sourceField] ? [path4.normalize(pkgJson[sourceField])] : [];
201
- if (checkExports) {
202
- const exportsSourceDirs = this.#getExportsSourceDirs(
203
- pkgJson.exports ?? {},
204
- sourceField
205
- );
206
- sourceDirs.push(...exportsSourceDirs);
139
+ getSourceEntryPaths(options) {
140
+ const { exports: checkExports = false, field: sourceField = 'source' } = options ?? {};
141
+ const pkgJson = this.getMetaData();
142
+ const sourceDirs = pkgJson[sourceField] ? [
143
+ node_path.normalize(pkgJson[sourceField])
144
+ ] : [];
145
+ if (checkExports) {
146
+ const exportsSourceDirs = _class_private_method_get(this, _getExportsSourceDirs, getExportsSourceDirs).call(this, pkgJson.exports ?? {}, sourceField);
147
+ sourceDirs.push(...exportsSourceDirs);
148
+ }
149
+ if (!sourceDirs.length) throw new Error(`"${sourceField}" field is not found in ${this.name} package.json`);
150
+ return _class_private_method_get(this, _getCommonRootPaths, getCommonRootPaths).call(this, sourceDirs);
207
151
  }
208
- if (!sourceDirs.length) {
209
- throw new Error(
210
- `"${sourceField}" field is not found in ${this.name} package.json`
211
- );
152
+ constructor(name, dir){
153
+ _class_private_method_init(this, _getExportsSourceDirs);
154
+ _class_private_method_init(this, _getCommonRootPaths);
155
+ _class_private_method_init(this, _getRootPath);
156
+ _define_property(this, "name", void 0);
157
+ _define_property(this, "dir", void 0);
158
+ _define_property(this, "metaData", void 0);
159
+ this.name = name;
160
+ this.dir = dir;
212
161
  }
213
- return this.#getCommonRootPaths(sourceDirs);
214
- }
215
- #getExportsSourceDirs(exportsConfig, sourceField) {
162
+ }
163
+ function getExportsSourceDirs(exportsConfig, sourceField) {
216
164
  const exportsSourceDirs = [];
217
- if (typeof exportsConfig[sourceField] === "string") {
218
- exportsSourceDirs.push(
219
- path4.normalize(exportsConfig[sourceField])
220
- );
221
- }
222
- for (const moduleRules of Object.values(exportsConfig)) {
223
- if (typeof moduleRules === "object" && typeof moduleRules[sourceField] === "string") {
224
- exportsSourceDirs.push(
225
- path4.normalize(moduleRules[sourceField])
226
- );
227
- }
228
- }
165
+ if ('string' == typeof exportsConfig[sourceField]) exportsSourceDirs.push(node_path.normalize(exportsConfig[sourceField]));
166
+ for (const moduleRules of Object.values(exportsConfig))if ('object' == typeof moduleRules && 'string' == typeof moduleRules[sourceField]) exportsSourceDirs.push(node_path.normalize(moduleRules[sourceField]));
229
167
  return exportsSourceDirs;
230
- }
231
- /**
232
- *
233
- * @param paths normalize paths
234
- * @returns common root paths
235
- */
236
- #getCommonRootPaths(paths) {
237
- const commonRootPathsSet = /* @__PURE__ */ new Set();
238
- for (const p of paths) {
239
- let dir;
240
- try {
241
- dir = fs3.statSync(p).isDirectory() ? p : path4.dirname(p);
242
- } catch {
243
- dir = path4.dirname(p);
244
- }
245
- const rootPath = this.#getRootPath(dir);
246
- if (!commonRootPathsSet.has(rootPath)) {
247
- commonRootPathsSet.add(rootPath);
248
- }
168
+ }
169
+ function getCommonRootPaths(paths) {
170
+ const commonRootPathsSet = new Set();
171
+ for (const p of paths){
172
+ let dir;
173
+ try {
174
+ dir = node_fs.statSync(p).isDirectory() ? p : node_path.dirname(p);
175
+ } catch {
176
+ dir = node_path.dirname(p);
177
+ }
178
+ const rootPath = _class_private_method_get(this, _getRootPath, getRootPath).call(this, dir);
179
+ if (!commonRootPathsSet.has(rootPath)) commonRootPathsSet.add(rootPath);
249
180
  }
250
- return Array.from(commonRootPathsSet).map((p) => path4.join(this.dir, p));
251
- }
252
- #getRootPath(p) {
253
- return p.split(path4.sep)[0];
254
- }
255
- };
256
-
257
- // src/common/pnpm.ts
258
- var getPatternsFromYaml = async (monorepoRoot) => {
259
- const { parse } = await import("yaml");
260
- const workspaceYamlFilePath = path5.join(monorepoRoot, PNPM_WORKSPACE_FILE);
261
- const yamlContent = await fs4.promises.readFile(workspaceYamlFilePath, "utf8");
262
- const pnpmWorkspace = parse(yamlContent);
263
- return pnpmWorkspace.packages || [];
181
+ return Array.from(commonRootPathsSet).map((p)=>node_path.join(this.dir, p));
182
+ }
183
+ function getRootPath(p) {
184
+ return p.split(node_path.sep)[0];
185
+ }
186
+ const getPatternsFromYaml = async (monorepoRoot)=>{
187
+ const { parse } = await import("yaml");
188
+ const workspaceYamlFilePath = node_path.join(monorepoRoot, PNPM_WORKSPACE_FILE);
189
+ const yamlContent = await node_fs.promises.readFile(workspaceYamlFilePath, 'utf8');
190
+ const pnpmWorkspace = parse(yamlContent);
191
+ return pnpmWorkspace.packages || [];
264
192
  };
265
- var normalize = (results) => results.map((fp) => path5.normalize(fp));
266
- var getGlobOpts = (rootPath, patterns) => {
267
- const globOpts = {
268
- cwd: rootPath,
269
- absolute: true,
270
- followSymbolicLinks: false
271
- };
272
- if (patterns.some((cfg) => cfg.includes("**") || cfg.includes("*"))) {
273
- globOpts.ignore = [
274
- // allow globs like "packages/**" or "packages/*",
275
- // but avoid picking up node_modules/**/package.json and dist/**/package.json
276
- "**/dist/**",
277
- "**/node_modules/**"
193
+ const normalize = (results)=>results.map((fp)=>node_path.normalize(fp));
194
+ const getGlobOpts = (rootPath, patterns)=>{
195
+ const globOpts = {
196
+ cwd: rootPath,
197
+ absolute: true,
198
+ followSymbolicLinks: false
199
+ };
200
+ if (patterns.some((cfg)=>cfg.includes('**') || cfg.includes('*'))) globOpts.ignore = [
201
+ '**/dist/**',
202
+ '**/node_modules/**'
278
203
  ];
279
- }
280
- return globOpts;
204
+ return globOpts;
281
205
  };
282
- var makeFileFinder = (rootPath, patterns) => {
283
- const globOpts = getGlobOpts(rootPath, patterns);
284
- return async (fileName, fileMapper) => {
285
- let result = await glob(
286
- patterns.map((globPath) => path5.posix.join(globPath, fileName)),
287
- globOpts
288
- );
289
- result = result.sort();
290
- result = normalize(result);
291
- return fileMapper(result);
292
- };
206
+ const makeFileFinder = (rootPath, patterns)=>{
207
+ const globOpts = getGlobOpts(rootPath, patterns);
208
+ return async (fileName, fileMapper)=>{
209
+ let result = await fast_glob(patterns.map((globPath)=>node_path.posix.join(globPath, fileName)), globOpts);
210
+ result = result.sort();
211
+ result = normalize(result);
212
+ return fileMapper(result);
213
+ };
293
214
  };
294
- var readPnpmProjects = async (monorepoRoot, patterns) => {
295
- const finder = makeFileFinder(monorepoRoot, patterns);
296
- const mapper = async (pkgJsonFilePath) => {
297
- const pkgJson = await readPackageJson(pkgJsonFilePath);
298
- return {
299
- dir: path5.dirname(pkgJsonFilePath),
300
- manifest: pkgJson
215
+ const readPnpmProjects = async (monorepoRoot, patterns)=>{
216
+ const finder = makeFileFinder(monorepoRoot, patterns);
217
+ const mapper = async (pkgJsonFilePath)=>{
218
+ const pkgJson = await readPackageJson(pkgJsonFilePath);
219
+ return {
220
+ dir: node_path.dirname(pkgJsonFilePath),
221
+ manifest: pkgJson
222
+ };
301
223
  };
302
- };
303
- const projects = await finder(
304
- PACKAGE_JSON,
305
- (filePaths) => Promise.all(filePaths.map(mapper))
306
- );
307
- return projects;
224
+ const projects = await finder(PACKAGE_JSON, (filePaths)=>Promise.all(filePaths.map(mapper)));
225
+ return projects;
308
226
  };
309
- var getProjects = async (monorepoRoot) => {
310
- const patterns = await getPatternsFromYaml(monorepoRoot);
311
- const pnpmProjects = await readPnpmProjects(monorepoRoot, patterns);
312
- return Promise.all(
313
- pnpmProjects.filter((p) => p.manifest.name).map(async (p) => {
314
- const project = new Project(p.manifest.name, p.dir);
315
- await project.init();
316
- return project;
317
- })
318
- );
227
+ const pnpm_getProjects = async (monorepoRoot)=>{
228
+ const patterns = await getPatternsFromYaml(monorepoRoot);
229
+ const pnpmProjects = await readPnpmProjects(monorepoRoot, patterns);
230
+ return Promise.all(pnpmProjects.filter((p)=>p.manifest.name).map(async (p)=>{
231
+ const project = new Project(p.manifest.name, p.dir);
232
+ await project.init();
233
+ return project;
234
+ }));
319
235
  };
320
-
321
- // src/common/rush.ts
322
- import path6 from "node:path";
323
- var getProjects2 = async (monorepoRoot) => {
324
- const rushConfiguration = await readRushJson(monorepoRoot);
325
- const { projects = [] } = rushConfiguration;
326
- return Promise.all(
327
- projects.map(async (p) => {
328
- const project = new Project(
329
- p.packageName,
330
- path6.join(monorepoRoot, p.projectFolder)
331
- );
332
- await project.init();
333
- return project;
334
- })
335
- );
236
+ const rush_getProjects = async (monorepoRoot)=>{
237
+ const rushConfiguration = await readRushJson(monorepoRoot);
238
+ const { projects = [] } = rushConfiguration;
239
+ return Promise.all(projects.map(async (p)=>{
240
+ const project = new Project(p.packageName, node_path.join(monorepoRoot, p.projectFolder));
241
+ await project.init();
242
+ return project;
243
+ }));
336
244
  };
337
-
338
- // src/common/getProjects.ts
339
- var getMonorepoSubProjects = async (monorepoBaseData) => {
340
- const { type, rootPath, getProjects: getProjects3 } = monorepoBaseData;
341
- if (type === "pnpm") {
342
- return getProjects(rootPath);
343
- }
344
- if (type === "rush") {
345
- return getProjects2(rootPath);
346
- }
347
- if (getProjects3) {
348
- return getProjects3(rootPath);
349
- }
350
- return [];
245
+ const getMonorepoSubProjects = async (monorepoBaseData)=>{
246
+ const { type, rootPath, getProjects } = monorepoBaseData;
247
+ if ('pnpm' === type) return pnpm_getProjects(rootPath);
248
+ if ('rush' === type) return rush_getProjects(rootPath);
249
+ if (getProjects) return getProjects(rootPath);
250
+ return [];
351
251
  };
352
-
353
- // src/project-utils/getDependentProjects.ts
354
- async function pathExists3(path9) {
355
- return fs5.promises.access(path9).then(() => true).catch(() => false);
252
+ async function getDependentProjects_pathExists(path) {
253
+ return node_fs.promises.access(path).then(()=>true).catch(()=>false);
356
254
  }
357
- var getDependentProjects = async (projectNameOrRootPath, options) => {
358
- const {
359
- cwd = process.cwd(),
360
- recursive,
361
- filter,
362
- extraMonorepoStrategies
363
- } = options;
364
- const currentProjectPkgJsonPath = path7.join(
365
- projectNameOrRootPath,
366
- "package.json"
367
- );
368
- let projectName;
369
- if (await pathExists3(currentProjectPkgJsonPath)) {
370
- ({ name: projectName } = await readPackageJson(currentProjectPkgJsonPath));
371
- } else {
372
- projectName = projectNameOrRootPath;
373
- }
374
- const monoBaseData = await getMonorepoBaseData(cwd, extraMonorepoStrategies);
375
- if (!monoBaseData.isMonorepo) {
376
- return [];
377
- }
378
- const projects = await getMonorepoSubProjects(monoBaseData);
379
- const currentProject = projects.find(
380
- (project) => project.name === projectName
381
- );
382
- if (!currentProject) {
383
- return [];
384
- }
385
- let dependentProjects = currentProject.getDependentProjects(projects, {
386
- recursive
387
- });
388
- if (filter) {
389
- dependentProjects = await filter(dependentProjects);
390
- }
391
- return dependentProjects;
255
+ const getDependentProjects = async (projectNameOrRootPath, options)=>{
256
+ const { cwd = process.cwd(), recursive, filter, extraMonorepoStrategies } = options;
257
+ const currentProjectPkgJsonPath = node_path.join(projectNameOrRootPath, 'package.json');
258
+ let projectName;
259
+ if (await getDependentProjects_pathExists(currentProjectPkgJsonPath)) ({ name: projectName } = await readPackageJson(currentProjectPkgJsonPath));
260
+ else projectName = projectNameOrRootPath;
261
+ const monoBaseData = await getMonorepoBaseData(cwd, extraMonorepoStrategies);
262
+ if (!monoBaseData.isMonorepo) return [];
263
+ const projects = await getMonorepoSubProjects(monoBaseData);
264
+ const currentProject = projects.find((project)=>project.name === projectName);
265
+ if (!currentProject) return [];
266
+ let dependentProjects = currentProject.getDependentProjects(projects, {
267
+ recursive
268
+ });
269
+ if (filter) dependentProjects = await filter(dependentProjects);
270
+ return dependentProjects;
392
271
  };
393
-
394
- // src/project-utils/filter.ts
395
272
  function hasExportsSourceField(exportsConfig, sourceField) {
396
- return typeof exportsConfig[sourceField] === "string" || Object.values(exportsConfig).some(
397
- (moduleRules) => typeof moduleRules === "object" && typeof moduleRules[sourceField] === "string"
398
- );
273
+ return 'string' == typeof exportsConfig[sourceField] || Object.values(exportsConfig).some((moduleRules)=>'object' == typeof moduleRules && 'string' == typeof moduleRules[sourceField]);
399
274
  }
400
- var filterByField = (fieldName, checkExports) => (projects) => {
401
- return projects.filter((p) => {
402
- return fieldName in p.metaData || checkExports && hasExportsSourceField(p.metaData.exports || {}, fieldName);
403
- });
404
- };
405
-
406
- // src/plugin.ts
407
- var PLUGIN_SOURCE_BUILD_NAME = "rsbuild:source-build";
408
- var getSourceInclude = async (options) => {
409
- const { projects, sourceField } = options;
410
- const includes = [];
411
- for (const project of projects) {
412
- includes.push(
413
- ...project.getSourceEntryPaths({ field: sourceField, exports: true })
414
- );
415
- }
416
- return includes;
275
+ const filterByField = (fieldName, checkExports)=>(projects)=>projects.filter((p)=>fieldName in p.metaData || checkExports && hasExportsSourceField(p.metaData.exports || {}, fieldName));
276
+ const PLUGIN_SOURCE_BUILD_NAME = 'rsbuild:source-build';
277
+ const getSourceInclude = async (options)=>{
278
+ const { projects, sourceField } = options;
279
+ const includes = [];
280
+ for (const project of projects)includes.push(...project.getSourceEntryPaths({
281
+ field: sourceField,
282
+ exports: true
283
+ }));
284
+ return includes;
417
285
  };
418
286
  function pluginSourceBuild(options) {
419
- const {
420
- projectName,
421
- sourceField = "source",
422
- resolvePriority = "source",
423
- extraMonorepoStrategies
424
- } = options ?? {};
425
- return {
426
- name: PLUGIN_SOURCE_BUILD_NAME,
427
- setup(api) {
428
- const projectRootPath = api.context.rootPath;
429
- let projects;
430
- api.modifyEnvironmentConfig(async (config) => {
431
- projects = projects || await getDependentProjects(projectName || projectRootPath, {
432
- cwd: projectRootPath,
433
- recursive: true,
434
- filter: filterByField(sourceField, true),
435
- extraMonorepoStrategies
436
- });
437
- const includes = await getSourceInclude({
438
- projects,
439
- sourceField
440
- });
441
- config.source = config.source ?? {};
442
- config.source.include = [...config.source.include ?? [], ...includes];
443
- });
444
- api.modifyBundlerChain((chain, { CHAIN_ID }) => {
445
- for (const ruleId of [CHAIN_ID.RULE.TS, CHAIN_ID.RULE.JS]) {
446
- if (chain.module.rules.get(ruleId)) {
447
- const rule = chain.module.rule(ruleId);
448
- rule.resolve.mainFields.merge(
449
- resolvePriority === "source" ? [sourceField, "..."] : ["...", sourceField]
450
- );
451
- rule.resolve.conditionNames.add("...").add(sourceField);
452
- }
453
- }
454
- });
455
- const getReferences = async (tsconfigPath, rspackReferences) => {
456
- const references = /* @__PURE__ */ new Set();
457
- for (const project of projects || []) {
458
- const filePath = path8.join(project.dir, "tsconfig.json");
459
- if (fs6.existsSync(filePath)) {
460
- references.add(filePath);
461
- }
462
- }
463
- const tsconfig = json52.parse(
464
- fs6.readFileSync(tsconfigPath, "utf-8")
465
- );
466
- const userReferences = [
467
- ...Array.isArray(rspackReferences) ? rspackReferences : [],
468
- ...tsconfig.references ? tsconfig.references.map((item) => item.path).filter(Boolean) : []
469
- ];
470
- if (userReferences.length) {
471
- const baseDir = path8.dirname(tsconfigPath);
472
- for (const item of userReferences) {
473
- if (!item) {
474
- continue;
475
- }
476
- const absolutePath = path8.isAbsolute(item) ? item : path8.join(baseDir, item);
477
- references.add(absolutePath);
478
- }
287
+ const { projectName, sourceField = 'source', resolvePriority = 'source', extraMonorepoStrategies } = options ?? {};
288
+ return {
289
+ name: PLUGIN_SOURCE_BUILD_NAME,
290
+ setup (api) {
291
+ const projectRootPath = api.context.rootPath;
292
+ let projects;
293
+ api.modifyEnvironmentConfig(async (config)=>{
294
+ projects = projects || await getDependentProjects(projectName || projectRootPath, {
295
+ cwd: projectRootPath,
296
+ recursive: true,
297
+ filter: filterByField(sourceField, true),
298
+ extraMonorepoStrategies
299
+ });
300
+ const includes = await getSourceInclude({
301
+ projects,
302
+ sourceField
303
+ });
304
+ config.source = config.source ?? {};
305
+ config.source.include = [
306
+ ...config.source.include ?? [],
307
+ ...includes
308
+ ];
309
+ });
310
+ api.modifyBundlerChain((chain, { CHAIN_ID })=>{
311
+ for (const ruleId of [
312
+ CHAIN_ID.RULE.TS,
313
+ CHAIN_ID.RULE.JS
314
+ ])if (chain.module.rules.get(ruleId)) {
315
+ const rule = chain.module.rule(ruleId);
316
+ rule.resolve.mainFields.merge('source' === resolvePriority ? [
317
+ sourceField,
318
+ '...'
319
+ ] : [
320
+ '...',
321
+ sourceField
322
+ ]);
323
+ rule.resolve.conditionNames.add('...').add(sourceField);
324
+ }
325
+ });
326
+ const getReferences = async (tsconfigPath, rspackReferences)=>{
327
+ const references = new Set();
328
+ for (const project of projects || []){
329
+ const filePath = node_path.join(project.dir, 'tsconfig.json');
330
+ if (node_fs.existsSync(filePath)) references.add(filePath);
331
+ }
332
+ const tsconfig = json5.parse(node_fs.readFileSync(tsconfigPath, 'utf-8'));
333
+ const userReferences = [
334
+ ...Array.isArray(rspackReferences) ? rspackReferences : [],
335
+ ...tsconfig.references ? tsconfig.references.map((item)=>item.path).filter(Boolean) : []
336
+ ];
337
+ if (userReferences.length) {
338
+ const baseDir = node_path.dirname(tsconfigPath);
339
+ for (const item of userReferences){
340
+ if (!item) continue;
341
+ const absolutePath = node_path.isAbsolute(item) ? item : node_path.join(baseDir, item);
342
+ references.add(absolutePath);
343
+ }
344
+ }
345
+ references.delete(tsconfigPath);
346
+ return Array.from(references);
347
+ };
348
+ if ('rspack' === api.context.bundlerType) api.modifyRspackConfig(async (config, { environment })=>{
349
+ const { tsconfigPath } = environment;
350
+ if (!tsconfigPath) return;
351
+ config.resolve ||= {};
352
+ const { tsConfig = {
353
+ configFile: tsconfigPath
354
+ } } = config.resolve;
355
+ const configObject = 'string' == typeof tsConfig ? {
356
+ configFile: tsConfig
357
+ } : tsConfig;
358
+ const references = await getReferences(tsconfigPath, configObject.references);
359
+ config.resolve.tsConfig = {
360
+ configFile: (null == configObject ? void 0 : configObject.configFile) || tsconfigPath,
361
+ references: references
362
+ };
363
+ });
364
+ else api.modifyBundlerChain(async (chain, { CHAIN_ID, environment })=>{
365
+ const { TS_CONFIG_PATHS } = CHAIN_ID.RESOLVE_PLUGIN;
366
+ const { tsconfigPath } = environment;
367
+ if (!chain.resolve.plugins.has(TS_CONFIG_PATHS) || !tsconfigPath) return;
368
+ const references = await getReferences(tsconfigPath);
369
+ chain.resolve.plugin(TS_CONFIG_PATHS).tap((options)=>options.map((option)=>({
370
+ ...option,
371
+ references
372
+ })));
373
+ });
479
374
  }
480
- references.delete(tsconfigPath);
481
- return Array.from(references);
482
- };
483
- if (api.context.bundlerType === "rspack") {
484
- api.modifyRspackConfig(async (config, { environment }) => {
485
- const { tsconfigPath } = environment;
486
- if (!tsconfigPath) {
487
- return;
488
- }
489
- config.resolve ||= {};
490
- const { tsConfig = { configFile: tsconfigPath } } = config.resolve;
491
- const configObject = typeof tsConfig === "string" ? { configFile: tsConfig } : tsConfig;
492
- const references = await getReferences(
493
- tsconfigPath,
494
- configObject.references
495
- );
496
- config.resolve.tsConfig = {
497
- configFile: configObject?.configFile || tsconfigPath,
498
- references
499
- };
500
- });
501
- } else {
502
- api.modifyBundlerChain(async (chain, { CHAIN_ID, environment }) => {
503
- const { TS_CONFIG_PATHS } = CHAIN_ID.RESOLVE_PLUGIN;
504
- const { tsconfigPath } = environment;
505
- if (!chain.resolve.plugins.has(TS_CONFIG_PATHS) || !tsconfigPath) {
506
- return;
507
- }
508
- const references = await getReferences(tsconfigPath);
509
- chain.resolve.plugin(TS_CONFIG_PATHS).tap(
510
- (options2) => options2.map((option) => ({
511
- ...option,
512
- references
513
- }))
514
- );
515
- });
516
- }
517
- }
518
- };
375
+ };
519
376
  }
520
- export {
521
- PLUGIN_SOURCE_BUILD_NAME,
522
- Project,
523
- getMonorepoBaseData,
524
- getMonorepoSubProjects,
525
- pluginSourceBuild
526
- };
377
+ export { PLUGIN_SOURCE_BUILD_NAME, Project, getMonorepoBaseData, getMonorepoSubProjects, pluginSourceBuild };