@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/README.md +11 -4
- package/README.zh-CN.md +11 -4
- package/dist/common/getBaseData.d.ts +9 -0
- package/dist/common/getProjects.d.ts +4 -0
- package/dist/common/index.d.ts +6 -0
- package/dist/common/isMonorepo.d.ts +8 -0
- package/dist/common/pnpm.d.ts +2 -0
- package/dist/common/rush.d.ts +2 -0
- package/dist/constants.d.ts +3 -0
- package/dist/index.cjs +432 -545
- package/dist/index.d.ts +4 -207
- package/dist/index.js +336 -485
- package/dist/plugin.d.ts +23 -0
- package/dist/project-utils/filter.d.ts +4 -0
- package/dist/project-utils/getDependentProjects.d.ts +12 -0
- package/dist/project-utils/index.d.ts +2 -0
- package/dist/project.d.ts +18 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/{index.d.cts → types/packageJson.d.ts} +10 -65
- package/dist/types/rushJson.d.ts +7 -0
- package/dist/utils.d.ts +4 -0
- package/package.json +31 -29
package/dist/index.js
CHANGED
|
@@ -1,526 +1,377 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
60
|
-
|
|
35
|
+
isMonorepo: false,
|
|
36
|
+
type: ''
|
|
61
37
|
};
|
|
62
|
-
}
|
|
63
|
-
return {
|
|
64
|
-
isMonorepo: false,
|
|
65
|
-
type: ""
|
|
66
|
-
};
|
|
67
38
|
};
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
89
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
rushJsonFilePath.includes(RUSH_JSON_FILE) ? rushJsonFilePath :
|
|
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
|
|
124
|
-
|
|
71
|
+
async function utils_pathExists(path) {
|
|
72
|
+
return node_fs.promises.access(path).then(()=>true).catch(()=>false);
|
|
125
73
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
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
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
-
|
|
214
|
-
|
|
215
|
-
#getExportsSourceDirs(exportsConfig, sourceField) {
|
|
162
|
+
}
|
|
163
|
+
function getExportsSourceDirs(exportsConfig, sourceField) {
|
|
216
164
|
const exportsSourceDirs = [];
|
|
217
|
-
if (typeof exportsConfig[sourceField]
|
|
218
|
-
|
|
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
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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)
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
return p.split(
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
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
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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
|
-
|
|
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
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
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
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
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
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
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
|
-
|
|
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
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
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
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
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
|
-
|
|
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 };
|