@rsbuild/plugin-source-build 1.0.1 → 1.0.2
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 +442 -522
- package/dist/index.d.ts +4 -207
- package/dist/index.js +363 -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 +27 -25
package/dist/index.js
CHANGED
|
@@ -1,526 +1,404 @@
|
|
|
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 * as __WEBPACK_EXTERNAL_MODULE_node_fs__ from "node:fs";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
|
|
3
|
+
import * as __WEBPACK_EXTERNAL_MODULE_json5__ from "json5";
|
|
4
|
+
import * as __WEBPACK_EXTERNAL_MODULE_fast_glob__ from "fast-glob";
|
|
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 __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].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(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].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(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].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;
|
|
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;
|
|
79
48
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
+
// find system root path
|
|
57
|
+
if (findPath === __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(findPath)) break;
|
|
58
|
+
findPath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(findPath);
|
|
90
59
|
}
|
|
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);
|
|
60
|
+
return {
|
|
61
|
+
isMonorepo: repoIsMonorepo,
|
|
62
|
+
rootPath: repoIsMonorepo ? findPath : '',
|
|
63
|
+
type,
|
|
64
|
+
getProjects: null == otherMonorepoAnalyzer ? void 0 : null === (_otherMonorepoAnalyzer_type = otherMonorepoAnalyzer[type]) || void 0 === _otherMonorepoAnalyzer_type ? void 0 : _otherMonorepoAnalyzer_type.getProjects
|
|
65
|
+
};
|
|
116
66
|
};
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
rushJsonFilePath.includes(RUSH_JSON_FILE) ? rushJsonFilePath :
|
|
120
|
-
|
|
121
|
-
return rushJson;
|
|
67
|
+
const readPackageJson = async (pkgJsonFilePath)=>readJson(pkgJsonFilePath);
|
|
68
|
+
const readRushJson = async (rushJsonFilePath)=>{
|
|
69
|
+
const rushJson = readJson(rushJsonFilePath.includes(RUSH_JSON_FILE) ? rushJsonFilePath : __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(rushJsonFilePath, RUSH_JSON_FILE));
|
|
70
|
+
return rushJson;
|
|
122
71
|
};
|
|
123
|
-
async function
|
|
124
|
-
|
|
72
|
+
async function utils_pathExists(path) {
|
|
73
|
+
return __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.access(path).then(()=>true).catch(()=>false);
|
|
125
74
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const json = json5.parse(content);
|
|
132
|
-
return json;
|
|
75
|
+
const readJson = async (jsonFileAbsPath)=>{
|
|
76
|
+
if (!await utils_pathExists(jsonFileAbsPath)) return {};
|
|
77
|
+
const content = await __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.readFile(jsonFileAbsPath, 'utf-8');
|
|
78
|
+
const json = __WEBPACK_EXTERNAL_MODULE_json5__["default"].parse(content);
|
|
79
|
+
return json;
|
|
133
80
|
};
|
|
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
|
-
|
|
81
|
+
function _check_private_redeclaration(obj, privateCollection) {
|
|
82
|
+
if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
83
|
+
}
|
|
84
|
+
function _class_private_method_get(receiver, privateSet, fn) {
|
|
85
|
+
if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance");
|
|
86
|
+
return fn;
|
|
87
|
+
}
|
|
88
|
+
function _class_private_method_init(obj, privateSet) {
|
|
89
|
+
_check_private_redeclaration(obj, privateSet);
|
|
90
|
+
privateSet.add(obj);
|
|
91
|
+
}
|
|
92
|
+
function _define_property(obj, key, value) {
|
|
93
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
94
|
+
value: value,
|
|
95
|
+
enumerable: true,
|
|
96
|
+
configurable: true,
|
|
97
|
+
writable: true
|
|
98
|
+
});
|
|
99
|
+
else obj[key] = value;
|
|
100
|
+
return obj;
|
|
101
|
+
}
|
|
102
|
+
var _getExportsSourceDirs = /*#__PURE__*/ new WeakSet(), /**
|
|
103
|
+
*
|
|
104
|
+
* @param paths normalize paths
|
|
105
|
+
* @returns common root paths
|
|
106
|
+
*/ _getCommonRootPaths = /*#__PURE__*/ new WeakSet(), _getRootPath = /*#__PURE__*/ new WeakSet();
|
|
107
|
+
class Project {
|
|
108
|
+
async init() {
|
|
109
|
+
this.metaData = await readPackageJson(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(this.dir, PACKAGE_JSON));
|
|
160
110
|
}
|
|
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
|
-
}
|
|
111
|
+
getMetaData() {
|
|
112
|
+
if (null === this.metaData) throw new Error('The Project object needs to be initialized by executing the `init` function');
|
|
113
|
+
return this.metaData;
|
|
178
114
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
115
|
+
getDependentProjects(monorepoProjects, options) {
|
|
116
|
+
const { recursive } = options ?? {
|
|
117
|
+
recursive: false
|
|
118
|
+
};
|
|
119
|
+
const allProjectMap = new Map();
|
|
120
|
+
for (const project of monorepoProjects)allProjectMap.set(project.name, project);
|
|
121
|
+
if (!recursive) return this.getDirectDependentProjects(allProjectMap);
|
|
122
|
+
const computedSet = new Set();
|
|
123
|
+
computedSet.add(this.name);
|
|
124
|
+
const queue = this.getDirectDependentProjects(allProjectMap).filter((p)=>!computedSet.has(p.name));
|
|
125
|
+
const result = [];
|
|
126
|
+
while(queue.length > 0){
|
|
127
|
+
const item = queue.shift();
|
|
128
|
+
if (computedSet.has(item.name)) continue;
|
|
129
|
+
result.push(item);
|
|
130
|
+
computedSet.add(item.name);
|
|
131
|
+
const newDeps = item.getDirectDependentProjects(allProjectMap);
|
|
132
|
+
if (newDeps.length > 0) queue.push(...newDeps);
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
189
135
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
136
|
+
getDirectDependentProjects(allProjectMap) {
|
|
137
|
+
const pkgJson = this.getMetaData();
|
|
138
|
+
const { dependencies = {}, devDependencies = {} } = pkgJson;
|
|
139
|
+
const projects = [];
|
|
140
|
+
for (const d of Object.keys(dependencies))if (allProjectMap.has(d)) projects.push(allProjectMap.get(d));
|
|
141
|
+
for (const d of Object.keys(devDependencies))if (allProjectMap.has(d)) projects.push(allProjectMap.get(d));
|
|
142
|
+
return projects;
|
|
194
143
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
144
|
+
getSourceEntryPaths(options) {
|
|
145
|
+
const { exports: checkExports = false, field: sourceField = 'source' } = options ?? {};
|
|
146
|
+
const pkgJson = this.getMetaData();
|
|
147
|
+
// normalize strings
|
|
148
|
+
const sourceDirs = pkgJson[sourceField] ? [
|
|
149
|
+
__WEBPACK_EXTERNAL_MODULE_node_path__["default"].normalize(pkgJson[sourceField])
|
|
150
|
+
] : [];
|
|
151
|
+
if (checkExports) {
|
|
152
|
+
/**
|
|
153
|
+
* analyze exports:
|
|
154
|
+
* "exports": {
|
|
155
|
+
* ".": {
|
|
156
|
+
* "source": "./src/index.ts"
|
|
157
|
+
* }
|
|
158
|
+
* },
|
|
159
|
+
*/ const exportsSourceDirs = _class_private_method_get(this, _getExportsSourceDirs, getExportsSourceDirs).call(this, pkgJson.exports ?? {}, sourceField);
|
|
160
|
+
sourceDirs.push(...exportsSourceDirs);
|
|
161
|
+
}
|
|
162
|
+
if (!sourceDirs.length) throw new Error(`"${sourceField}" field is not found in ${this.name} package.json`);
|
|
163
|
+
return _class_private_method_get(this, _getCommonRootPaths, getCommonRootPaths).call(this, sourceDirs);
|
|
207
164
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
165
|
+
constructor(name, dir){
|
|
166
|
+
_class_private_method_init(this, _getExportsSourceDirs);
|
|
167
|
+
_class_private_method_init(this, _getCommonRootPaths);
|
|
168
|
+
_class_private_method_init(this, _getRootPath);
|
|
169
|
+
_define_property(this, "name", void 0);
|
|
170
|
+
_define_property(this, "dir", void 0);
|
|
171
|
+
_define_property(this, "metaData", void 0);
|
|
172
|
+
this.name = name;
|
|
173
|
+
this.dir = dir;
|
|
212
174
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
#getExportsSourceDirs(exportsConfig, sourceField) {
|
|
175
|
+
}
|
|
176
|
+
function getExportsSourceDirs(exportsConfig, sourceField) {
|
|
216
177
|
const exportsSourceDirs = [];
|
|
217
|
-
if (typeof exportsConfig[sourceField]
|
|
218
|
-
|
|
219
|
-
|
|
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
|
-
}
|
|
178
|
+
if ('string' == typeof exportsConfig[sourceField]) exportsSourceDirs.push(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].normalize(exportsConfig[sourceField]));
|
|
179
|
+
for (const moduleRules of Object.values(exportsConfig))if ('object' == typeof moduleRules && 'string' == typeof moduleRules[sourceField]) exportsSourceDirs.push(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].normalize(moduleRules[sourceField]));
|
|
180
|
+
// normalize strings
|
|
229
181
|
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
|
-
}
|
|
182
|
+
}
|
|
183
|
+
function getCommonRootPaths(paths) {
|
|
184
|
+
const commonRootPathsSet = new Set();
|
|
185
|
+
for (const p of paths){
|
|
186
|
+
let dir;
|
|
187
|
+
try {
|
|
188
|
+
dir = __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].statSync(p).isDirectory() ? p : __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(p);
|
|
189
|
+
} catch {
|
|
190
|
+
dir = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(p);
|
|
191
|
+
}
|
|
192
|
+
const rootPath = _class_private_method_get(this, _getRootPath, getRootPath).call(this, dir);
|
|
193
|
+
if (!commonRootPathsSet.has(rootPath)) commonRootPathsSet.add(rootPath);
|
|
249
194
|
}
|
|
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 || [];
|
|
195
|
+
return Array.from(commonRootPathsSet).map((p)=>__WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(this.dir, p));
|
|
196
|
+
}
|
|
197
|
+
function getRootPath(p) {
|
|
198
|
+
return p.split(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].sep)[0];
|
|
199
|
+
}
|
|
200
|
+
const getPatternsFromYaml = async (monorepoRoot)=>{
|
|
201
|
+
const { parse } = await import("yaml");
|
|
202
|
+
const workspaceYamlFilePath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(monorepoRoot, PNPM_WORKSPACE_FILE);
|
|
203
|
+
const yamlContent = await __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.readFile(workspaceYamlFilePath, 'utf8');
|
|
204
|
+
const pnpmWorkspace = parse(yamlContent);
|
|
205
|
+
return pnpmWorkspace.packages || [];
|
|
264
206
|
};
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
"**/node_modules/**"
|
|
207
|
+
const normalize = (results)=>results.map((fp)=>__WEBPACK_EXTERNAL_MODULE_node_path__["default"].normalize(fp));
|
|
208
|
+
const getGlobOpts = (rootPath, patterns)=>{
|
|
209
|
+
const globOpts = {
|
|
210
|
+
cwd: rootPath,
|
|
211
|
+
absolute: true,
|
|
212
|
+
followSymbolicLinks: false
|
|
213
|
+
};
|
|
214
|
+
if (patterns.some((cfg)=>cfg.includes('**') || cfg.includes('*'))) globOpts.ignore = [
|
|
215
|
+
// allow globs like "packages/**" or "packages/*",
|
|
216
|
+
// but avoid picking up node_modules/**/package.json and dist/**/package.json
|
|
217
|
+
'**/dist/**',
|
|
218
|
+
'**/node_modules/**'
|
|
278
219
|
];
|
|
279
|
-
|
|
280
|
-
return globOpts;
|
|
220
|
+
return globOpts;
|
|
281
221
|
};
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
};
|
|
222
|
+
const makeFileFinder = (rootPath, patterns)=>{
|
|
223
|
+
const globOpts = getGlobOpts(rootPath, patterns);
|
|
224
|
+
return async (fileName, fileMapper)=>{
|
|
225
|
+
let result = await (0, __WEBPACK_EXTERNAL_MODULE_fast_glob__["default"])(patterns.map((globPath)=>__WEBPACK_EXTERNAL_MODULE_node_path__["default"].posix.join(globPath, fileName)), globOpts);
|
|
226
|
+
// fast-glob does not respect pattern order, so we re-sort by absolute path
|
|
227
|
+
result = result.sort();
|
|
228
|
+
// POSIX results always need to be normalized
|
|
229
|
+
result = normalize(result);
|
|
230
|
+
return fileMapper(result);
|
|
231
|
+
};
|
|
293
232
|
};
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
233
|
+
const readPnpmProjects = async (monorepoRoot, patterns)=>{
|
|
234
|
+
const finder = makeFileFinder(monorepoRoot, patterns);
|
|
235
|
+
const mapper = async (pkgJsonFilePath)=>{
|
|
236
|
+
const pkgJson = await readPackageJson(pkgJsonFilePath);
|
|
237
|
+
return {
|
|
238
|
+
dir: __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(pkgJsonFilePath),
|
|
239
|
+
manifest: pkgJson
|
|
240
|
+
};
|
|
301
241
|
};
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
PACKAGE_JSON,
|
|
305
|
-
(filePaths) => Promise.all(filePaths.map(mapper))
|
|
306
|
-
);
|
|
307
|
-
return projects;
|
|
242
|
+
const projects = await finder(PACKAGE_JSON, (filePaths)=>Promise.all(filePaths.map(mapper)));
|
|
243
|
+
return projects;
|
|
308
244
|
};
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
})
|
|
318
|
-
);
|
|
245
|
+
const pnpm_getProjects = async (monorepoRoot)=>{
|
|
246
|
+
const patterns = await getPatternsFromYaml(monorepoRoot);
|
|
247
|
+
const pnpmProjects = await readPnpmProjects(monorepoRoot, patterns);
|
|
248
|
+
return Promise.all(pnpmProjects.filter((p)=>p.manifest.name).map(async (p)=>{
|
|
249
|
+
const project = new Project(p.manifest.name, p.dir);
|
|
250
|
+
await project.init();
|
|
251
|
+
return project;
|
|
252
|
+
}));
|
|
319
253
|
};
|
|
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
|
-
);
|
|
254
|
+
const rush_getProjects = async (monorepoRoot)=>{
|
|
255
|
+
const rushConfiguration = await readRushJson(monorepoRoot);
|
|
256
|
+
const { projects = [] } = rushConfiguration;
|
|
257
|
+
return Promise.all(projects.map(async (p)=>{
|
|
258
|
+
const project = new Project(p.packageName, __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(monorepoRoot, p.projectFolder));
|
|
259
|
+
await project.init();
|
|
260
|
+
return project;
|
|
261
|
+
}));
|
|
336
262
|
};
|
|
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 [];
|
|
263
|
+
const getMonorepoSubProjects = async (monorepoBaseData)=>{
|
|
264
|
+
const { type, rootPath, getProjects } = monorepoBaseData;
|
|
265
|
+
if ('pnpm' === type) return pnpm_getProjects(rootPath);
|
|
266
|
+
if ('rush' === type) return rush_getProjects(rootPath);
|
|
267
|
+
if (getProjects) return getProjects(rootPath);
|
|
268
|
+
return [];
|
|
351
269
|
};
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
async function pathExists3(path9) {
|
|
355
|
-
return fs5.promises.access(path9).then(() => true).catch(() => false);
|
|
270
|
+
async function getDependentProjects_pathExists(path) {
|
|
271
|
+
return __WEBPACK_EXTERNAL_MODULE_node_fs__["default"].promises.access(path).then(()=>true).catch(()=>false);
|
|
356
272
|
}
|
|
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;
|
|
273
|
+
const getDependentProjects = async (projectNameOrRootPath, options)=>{
|
|
274
|
+
const { cwd = process.cwd(), recursive, filter, extraMonorepoStrategies } = options;
|
|
275
|
+
// check if first argument is projectRootPath.
|
|
276
|
+
const currentProjectPkgJsonPath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(projectNameOrRootPath, 'package.json');
|
|
277
|
+
let projectName;
|
|
278
|
+
if (await getDependentProjects_pathExists(currentProjectPkgJsonPath)) ({ name: projectName } = await readPackageJson(currentProjectPkgJsonPath));
|
|
279
|
+
else projectName = projectNameOrRootPath;
|
|
280
|
+
const monoBaseData = await getMonorepoBaseData(cwd, extraMonorepoStrategies);
|
|
281
|
+
if (!monoBaseData.isMonorepo) return [];
|
|
282
|
+
const projects = await getMonorepoSubProjects(monoBaseData);
|
|
283
|
+
const currentProject = projects.find((project)=>project.name === projectName);
|
|
284
|
+
if (!currentProject) return [];
|
|
285
|
+
let dependentProjects = currentProject.getDependentProjects(projects, {
|
|
286
|
+
recursive
|
|
287
|
+
});
|
|
288
|
+
if (filter) dependentProjects = await filter(dependentProjects);
|
|
289
|
+
return dependentProjects;
|
|
392
290
|
};
|
|
393
|
-
|
|
394
|
-
// src/project-utils/filter.ts
|
|
395
291
|
function hasExportsSourceField(exportsConfig, sourceField) {
|
|
396
|
-
|
|
397
|
-
(moduleRules) => typeof moduleRules === "object" && typeof moduleRules[sourceField] === "string"
|
|
398
|
-
);
|
|
292
|
+
return 'string' == typeof exportsConfig[sourceField] || Object.values(exportsConfig).some((moduleRules)=>'object' == typeof moduleRules && 'string' == typeof moduleRules[sourceField]);
|
|
399
293
|
}
|
|
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;
|
|
294
|
+
const filterByField = (fieldName, checkExports)=>(projects)=>projects.filter((p)=>fieldName in p.metaData || checkExports && hasExportsSourceField(p.metaData.exports || {}, fieldName));
|
|
295
|
+
const PLUGIN_SOURCE_BUILD_NAME = 'rsbuild:source-build';
|
|
296
|
+
const getSourceInclude = async (options)=>{
|
|
297
|
+
const { projects, sourceField } = options;
|
|
298
|
+
const includes = [];
|
|
299
|
+
for (const project of projects)includes.push(...project.getSourceEntryPaths({
|
|
300
|
+
field: sourceField,
|
|
301
|
+
exports: true
|
|
302
|
+
}));
|
|
303
|
+
return includes;
|
|
417
304
|
};
|
|
418
305
|
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
|
-
|
|
306
|
+
const { projectName, sourceField = 'source', resolvePriority = 'source', extraMonorepoStrategies } = options ?? {};
|
|
307
|
+
return {
|
|
308
|
+
name: PLUGIN_SOURCE_BUILD_NAME,
|
|
309
|
+
setup (api) {
|
|
310
|
+
const projectRootPath = api.context.rootPath;
|
|
311
|
+
let projects;
|
|
312
|
+
api.modifyEnvironmentConfig(async (config)=>{
|
|
313
|
+
projects = projects || await getDependentProjects(projectName || projectRootPath, {
|
|
314
|
+
cwd: projectRootPath,
|
|
315
|
+
recursive: true,
|
|
316
|
+
filter: filterByField(sourceField, true),
|
|
317
|
+
extraMonorepoStrategies
|
|
318
|
+
});
|
|
319
|
+
const includes = await getSourceInclude({
|
|
320
|
+
projects,
|
|
321
|
+
sourceField
|
|
322
|
+
});
|
|
323
|
+
config.source = config.source ?? {};
|
|
324
|
+
config.source.include = [
|
|
325
|
+
...config.source.include ?? [],
|
|
326
|
+
...includes
|
|
327
|
+
];
|
|
328
|
+
});
|
|
329
|
+
api.modifyBundlerChain((chain, { CHAIN_ID })=>{
|
|
330
|
+
for (const ruleId of [
|
|
331
|
+
CHAIN_ID.RULE.TS,
|
|
332
|
+
CHAIN_ID.RULE.JS
|
|
333
|
+
])if (chain.module.rules.get(ruleId)) {
|
|
334
|
+
const rule = chain.module.rule(ruleId);
|
|
335
|
+
// https://rspack.dev/config/resolve
|
|
336
|
+
// when source is not exist, other mainFields will effect. // source > Rspack default mainFields.
|
|
337
|
+
rule.resolve.mainFields.merge('source' === resolvePriority ? [
|
|
338
|
+
sourceField,
|
|
339
|
+
'...'
|
|
340
|
+
] : [
|
|
341
|
+
'...',
|
|
342
|
+
sourceField
|
|
343
|
+
]);
|
|
344
|
+
// `conditionNames` is not affected by `resolvePriority`.
|
|
345
|
+
// The priority is controlled by the order of fields declared in `exports`.
|
|
346
|
+
rule.resolve.conditionNames.add('...').add(sourceField);
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
const getReferences = async (tsconfigPath, rspackReferences)=>{
|
|
350
|
+
const references = new Set();
|
|
351
|
+
for (const project of projects || []){
|
|
352
|
+
const filePath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(project.dir, 'tsconfig.json');
|
|
353
|
+
if (__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].existsSync(filePath)) references.add(filePath);
|
|
354
|
+
}
|
|
355
|
+
// Add references in the current project's tsconfig.json
|
|
356
|
+
const tsconfig = __WEBPACK_EXTERNAL_MODULE_json5__["default"].parse(__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].readFileSync(tsconfigPath, 'utf-8'));
|
|
357
|
+
const userReferences = [
|
|
358
|
+
...Array.isArray(rspackReferences) ? rspackReferences : [],
|
|
359
|
+
...tsconfig.references ? tsconfig.references.map((item)=>item.path).filter(Boolean) : []
|
|
360
|
+
];
|
|
361
|
+
if (userReferences.length) {
|
|
362
|
+
const baseDir = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].dirname(tsconfigPath);
|
|
363
|
+
for (const item of userReferences){
|
|
364
|
+
if (!item) continue;
|
|
365
|
+
const absolutePath = __WEBPACK_EXTERNAL_MODULE_node_path__["default"].isAbsolute(item) ? item : __WEBPACK_EXTERNAL_MODULE_node_path__["default"].join(baseDir, item);
|
|
366
|
+
references.add(absolutePath);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
// avoid self reference, it will break the resolver
|
|
370
|
+
references.delete(tsconfigPath);
|
|
371
|
+
return Array.from(references);
|
|
372
|
+
};
|
|
373
|
+
if ('rspack' === api.context.bundlerType) api.modifyRspackConfig(async (config, { environment })=>{
|
|
374
|
+
const { tsconfigPath } = environment;
|
|
375
|
+
if (!tsconfigPath) return;
|
|
376
|
+
config.resolve ||= {};
|
|
377
|
+
const { tsConfig = {
|
|
378
|
+
configFile: tsconfigPath
|
|
379
|
+
} } = config.resolve;
|
|
380
|
+
const configObject = 'string' == typeof tsConfig ? {
|
|
381
|
+
configFile: tsConfig
|
|
382
|
+
} : tsConfig;
|
|
383
|
+
const references = await getReferences(tsconfigPath, configObject.references);
|
|
384
|
+
config.resolve.tsConfig = {
|
|
385
|
+
configFile: (null == configObject ? void 0 : configObject.configFile) || tsconfigPath,
|
|
386
|
+
references: references
|
|
387
|
+
};
|
|
388
|
+
});
|
|
389
|
+
else api.modifyBundlerChain(async (chain, { CHAIN_ID, environment })=>{
|
|
390
|
+
const { TS_CONFIG_PATHS } = CHAIN_ID.RESOLVE_PLUGIN;
|
|
391
|
+
const { tsconfigPath } = environment;
|
|
392
|
+
if (!chain.resolve.plugins.has(TS_CONFIG_PATHS) || !tsconfigPath) return;
|
|
393
|
+
const references = await getReferences(tsconfigPath);
|
|
394
|
+
// set references config
|
|
395
|
+
// https://github.com/dividab/tsconfig-paths-webpack-plugin#options
|
|
396
|
+
chain.resolve.plugin(TS_CONFIG_PATHS).tap((options)=>options.map((option)=>({
|
|
397
|
+
...option,
|
|
398
|
+
references
|
|
399
|
+
})));
|
|
400
|
+
});
|
|
479
401
|
}
|
|
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
|
-
};
|
|
402
|
+
};
|
|
519
403
|
}
|
|
520
|
-
export {
|
|
521
|
-
PLUGIN_SOURCE_BUILD_NAME,
|
|
522
|
-
Project,
|
|
523
|
-
getMonorepoBaseData,
|
|
524
|
-
getMonorepoSubProjects,
|
|
525
|
-
pluginSourceBuild
|
|
526
|
-
};
|
|
404
|
+
export { PLUGIN_SOURCE_BUILD_NAME, Project, getMonorepoBaseData, getMonorepoSubProjects, pluginSourceBuild };
|