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