@rsdoctor/graph 0.0.2-beta.0
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/LICENSE +21 -0
- package/README.md +15 -0
- package/dist/cjs/graph/chunk-graph/asset.js +44 -0
- package/dist/cjs/graph/chunk-graph/chunk.js +105 -0
- package/dist/cjs/graph/chunk-graph/entrypoint.js +52 -0
- package/dist/cjs/graph/chunk-graph/graph.js +88 -0
- package/dist/cjs/graph/chunk-graph/index.js +28 -0
- package/dist/cjs/graph/index.js +26 -0
- package/dist/cjs/graph/module-graph/dependency.js +91 -0
- package/dist/cjs/graph/module-graph/graph.js +354 -0
- package/dist/cjs/graph/module-graph/index.js +32 -0
- package/dist/cjs/graph/module-graph/module.js +284 -0
- package/dist/cjs/graph/module-graph/statement.js +82 -0
- package/dist/cjs/graph/module-graph/tree-shaking/export.js +107 -0
- package/dist/cjs/graph/module-graph/tree-shaking/index.js +30 -0
- package/dist/cjs/graph/module-graph/tree-shaking/module.js +98 -0
- package/dist/cjs/graph/module-graph/tree-shaking/sideEffect.js +94 -0
- package/dist/cjs/graph/module-graph/tree-shaking/types.js +16 -0
- package/dist/cjs/graph/module-graph/tree-shaking/variable.js +63 -0
- package/dist/cjs/graph/module-graph/types.js +16 -0
- package/dist/cjs/graph/module-graph/utils.js +65 -0
- package/dist/cjs/graph/package-graph/dependency.js +56 -0
- package/dist/cjs/graph/package-graph/graph.js +173 -0
- package/dist/cjs/graph/package-graph/index.js +28 -0
- package/dist/cjs/graph/package-graph/package.js +121 -0
- package/dist/cjs/graph/package-graph/types.js +16 -0
- package/dist/cjs/graph/package-graph/utils.js +30 -0
- package/dist/cjs/index.js +22 -0
- package/dist/esm/graph/chunk-graph/asset.js +20 -0
- package/dist/esm/graph/chunk-graph/chunk.js +81 -0
- package/dist/esm/graph/chunk-graph/entrypoint.js +28 -0
- package/dist/esm/graph/chunk-graph/graph.js +64 -0
- package/dist/esm/graph/chunk-graph/index.js +4 -0
- package/dist/esm/graph/index.js +3 -0
- package/dist/esm/graph/module-graph/dependency.js +67 -0
- package/dist/esm/graph/module-graph/graph.js +335 -0
- package/dist/esm/graph/module-graph/index.js +6 -0
- package/dist/esm/graph/module-graph/module.js +250 -0
- package/dist/esm/graph/module-graph/statement.js +58 -0
- package/dist/esm/graph/module-graph/tree-shaking/export.js +83 -0
- package/dist/esm/graph/module-graph/tree-shaking/index.js +5 -0
- package/dist/esm/graph/module-graph/tree-shaking/module.js +74 -0
- package/dist/esm/graph/module-graph/tree-shaking/sideEffect.js +70 -0
- package/dist/esm/graph/module-graph/tree-shaking/types.js +0 -0
- package/dist/esm/graph/module-graph/tree-shaking/variable.js +39 -0
- package/dist/esm/graph/module-graph/types.js +0 -0
- package/dist/esm/graph/module-graph/utils.js +39 -0
- package/dist/esm/graph/package-graph/dependency.js +32 -0
- package/dist/esm/graph/package-graph/graph.js +149 -0
- package/dist/esm/graph/package-graph/index.js +4 -0
- package/dist/esm/graph/package-graph/package.js +97 -0
- package/dist/esm/graph/package-graph/types.js +0 -0
- package/dist/esm/graph/package-graph/utils.js +6 -0
- package/dist/esm/index.js +1 -0
- package/dist/type/graph/chunk-graph/asset.d.ts +10 -0
- package/dist/type/graph/chunk-graph/chunk.d.ts +30 -0
- package/dist/type/graph/chunk-graph/entrypoint.d.ts +12 -0
- package/dist/type/graph/chunk-graph/graph.d.ts +22 -0
- package/dist/type/graph/chunk-graph/index.d.ts +4 -0
- package/dist/type/graph/index.d.ts +3 -0
- package/dist/type/graph/module-graph/dependency.d.ts +25 -0
- package/dist/type/graph/module-graph/graph.d.ts +38 -0
- package/dist/type/graph/module-graph/index.d.ts +6 -0
- package/dist/type/graph/module-graph/module.d.ts +64 -0
- package/dist/type/graph/module-graph/statement.d.ts +12 -0
- package/dist/type/graph/module-graph/tree-shaking/export.d.ts +22 -0
- package/dist/type/graph/module-graph/tree-shaking/index.d.ts +5 -0
- package/dist/type/graph/module-graph/tree-shaking/module.d.ts +28 -0
- package/dist/type/graph/module-graph/tree-shaking/sideEffect.d.ts +27 -0
- package/dist/type/graph/module-graph/tree-shaking/types.d.ts +5 -0
- package/dist/type/graph/module-graph/tree-shaking/variable.d.ts +18 -0
- package/dist/type/graph/module-graph/types.d.ts +12 -0
- package/dist/type/graph/module-graph/utils.d.ts +4 -0
- package/dist/type/graph/package-graph/dependency.d.ts +15 -0
- package/dist/type/graph/package-graph/graph.d.ts +25 -0
- package/dist/type/graph/package-graph/index.d.ts +4 -0
- package/dist/type/graph/package-graph/package.d.ts +25 -0
- package/dist/type/graph/package-graph/types.d.ts +6 -0
- package/dist/type/graph/package-graph/utils.d.ts +1 -0
- package/dist/type/index.d.ts +1 -0
- package/package.json +42 -0
@@ -0,0 +1,335 @@
|
|
1
|
+
import { SDK } from "@rsdoctor/types";
|
2
|
+
import { Dependency } from "./dependency";
|
3
|
+
import { Module } from "./module";
|
4
|
+
import { Statement } from "./statement";
|
5
|
+
import {
|
6
|
+
ModuleGraphModule,
|
7
|
+
ExportInfo,
|
8
|
+
SideEffect,
|
9
|
+
Variable
|
10
|
+
} from "./tree-shaking";
|
11
|
+
class ModuleGraph {
|
12
|
+
constructor() {
|
13
|
+
this._dependenciesIdMap = /* @__PURE__ */ new Map();
|
14
|
+
this._moduleWebpackIdMap = /* @__PURE__ */ new Map();
|
15
|
+
this._moduleIdMap = /* @__PURE__ */ new Map();
|
16
|
+
this._moduleGraphModules = /* @__PURE__ */ new Map();
|
17
|
+
this._exportIdMap = /* @__PURE__ */ new Map();
|
18
|
+
this._sideEffectIdMap = /* @__PURE__ */ new Map();
|
19
|
+
this._varIdMap = /* @__PURE__ */ new Map();
|
20
|
+
}
|
21
|
+
static init() {
|
22
|
+
Module.init();
|
23
|
+
Dependency.init();
|
24
|
+
ModuleGraphModule.init();
|
25
|
+
}
|
26
|
+
static fromData(data) {
|
27
|
+
const moduleGraph = new ModuleGraph();
|
28
|
+
const getStatement = (data2) => new Statement(moduleGraph.getModuleById(data2.module), data2.position);
|
29
|
+
for (const item of data.modules ?? []) {
|
30
|
+
const module = new Module(
|
31
|
+
String(item.id),
|
32
|
+
item.path,
|
33
|
+
item.isEntry,
|
34
|
+
item.kind
|
35
|
+
);
|
36
|
+
module.id = item.id;
|
37
|
+
module.setSize(item.size);
|
38
|
+
module.meta = {
|
39
|
+
strictHarmonyModule: item.meta?.strictHarmonyModule ?? false,
|
40
|
+
hasSetEsModuleStatement: item.meta?.hasSetEsModuleStatement ?? false
|
41
|
+
};
|
42
|
+
module._isPreferSource = item.isPreferSource;
|
43
|
+
module.chunks = item.chunks;
|
44
|
+
moduleGraph.addModule(module);
|
45
|
+
}
|
46
|
+
for (const moduleData of data.modules ?? []) {
|
47
|
+
const module = moduleGraph.getModuleById(moduleData.id);
|
48
|
+
for (const depId of moduleData.dependencies) {
|
49
|
+
const depData = (data.dependencies ?? []).find(
|
50
|
+
(item) => item.id === depId
|
51
|
+
);
|
52
|
+
const depModule = depData && moduleGraph.getModuleById(depData.originDependency);
|
53
|
+
if (!depData || !depModule) {
|
54
|
+
console.warn(`The connection data ID is empty: ${depId}`);
|
55
|
+
continue;
|
56
|
+
}
|
57
|
+
const dep = module.addDependency(
|
58
|
+
depData.request,
|
59
|
+
depModule,
|
60
|
+
depData.kind,
|
61
|
+
depData.statements.map(getStatement)
|
62
|
+
);
|
63
|
+
if (!dep) {
|
64
|
+
continue;
|
65
|
+
}
|
66
|
+
dep.id = depData.id;
|
67
|
+
moduleGraph.addDependency(dep);
|
68
|
+
}
|
69
|
+
for (const normalModuleId of moduleData.modules ?? []) {
|
70
|
+
const normalModule = moduleGraph.getModuleById(normalModuleId);
|
71
|
+
if (!normalModule) {
|
72
|
+
console.warn(
|
73
|
+
`Add the ordinary module ID as empty: ${normalModuleId}`
|
74
|
+
);
|
75
|
+
continue;
|
76
|
+
}
|
77
|
+
module.addNormalModule(normalModule);
|
78
|
+
}
|
79
|
+
for (const concatenationModuleId of moduleData.concatenationModules ?? []) {
|
80
|
+
const concatenationModule = moduleGraph.getModuleById(
|
81
|
+
concatenationModuleId
|
82
|
+
);
|
83
|
+
if (!concatenationModule) {
|
84
|
+
console.warn(
|
85
|
+
`The aggregation module ID is empty: ${concatenationModule}`
|
86
|
+
);
|
87
|
+
continue;
|
88
|
+
}
|
89
|
+
module.addConcatenationModule(concatenationModule);
|
90
|
+
}
|
91
|
+
}
|
92
|
+
for (const exportData of data.exports ?? []) {
|
93
|
+
const info = new ExportInfo(
|
94
|
+
exportData.name,
|
95
|
+
exportData.identifier ? getStatement(exportData.identifier) : void 0
|
96
|
+
);
|
97
|
+
info.id = exportData.id;
|
98
|
+
moduleGraph.addExportInfo(info);
|
99
|
+
}
|
100
|
+
for (const exportData of data.exports ?? []) {
|
101
|
+
if (exportData.from) {
|
102
|
+
const current = moduleGraph._exportIdMap.get(exportData.id);
|
103
|
+
const from = moduleGraph._exportIdMap.get(exportData.from);
|
104
|
+
if (current && from) {
|
105
|
+
current.setFromExport(from);
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}
|
109
|
+
for (const varData of data.variables ?? []) {
|
110
|
+
const module = moduleGraph.getModuleById(varData.module);
|
111
|
+
if (!module) {
|
112
|
+
console.warn(
|
113
|
+
`The module ID in the variable is empty:${varData.module}`
|
114
|
+
);
|
115
|
+
continue;
|
116
|
+
}
|
117
|
+
const info = new Variable(
|
118
|
+
varData.name,
|
119
|
+
module,
|
120
|
+
varData.usedInfo,
|
121
|
+
getStatement(varData.identifier)
|
122
|
+
);
|
123
|
+
const exportInfo = moduleGraph._exportIdMap.get(varData.exported ?? -1);
|
124
|
+
if (exportInfo) {
|
125
|
+
info.setExportInfo(exportInfo);
|
126
|
+
}
|
127
|
+
info.id = varData.id;
|
128
|
+
moduleGraph.addVariable(info);
|
129
|
+
}
|
130
|
+
for (const sideData of data.sideEffects ?? []) {
|
131
|
+
const module = moduleGraph.getModuleById(sideData.module);
|
132
|
+
if (!module) {
|
133
|
+
console.warn(
|
134
|
+
`The module ID in the side effects is empty:${sideData.module}`
|
135
|
+
);
|
136
|
+
continue;
|
137
|
+
}
|
138
|
+
const info = new SideEffect(
|
139
|
+
sideData.name,
|
140
|
+
module,
|
141
|
+
getStatement(sideData.identifier),
|
142
|
+
sideData.originName
|
143
|
+
);
|
144
|
+
info.isNameSpace = sideData.isNameSpace;
|
145
|
+
info.id = sideData.id;
|
146
|
+
for (const exportId of sideData.exports ?? []) {
|
147
|
+
const exportInfo = moduleGraph._exportIdMap.get(exportId);
|
148
|
+
if (exportInfo) {
|
149
|
+
exportInfo.addSideEffect(info);
|
150
|
+
}
|
151
|
+
}
|
152
|
+
if (sideData.variable) {
|
153
|
+
const varInfo = moduleGraph._varIdMap.get(sideData.variable);
|
154
|
+
if (varInfo) {
|
155
|
+
info._variable = varInfo;
|
156
|
+
}
|
157
|
+
}
|
158
|
+
moduleGraph.addSideEffect(info);
|
159
|
+
}
|
160
|
+
for (const mgmData of data.moduleGraphModules ?? []) {
|
161
|
+
const module = moduleGraph.getModuleById(mgmData.module);
|
162
|
+
if (!module) {
|
163
|
+
console.warn(
|
164
|
+
`The module ID in ModuleGraphModule is empty: ${mgmData.module}`
|
165
|
+
);
|
166
|
+
continue;
|
167
|
+
}
|
168
|
+
const mgm = new ModuleGraphModule(module, moduleGraph, mgmData.dynamic);
|
169
|
+
moduleGraph.addModuleGraphModule(mgm);
|
170
|
+
mgmData.exports.forEach((id) => {
|
171
|
+
const info = moduleGraph._exportIdMap.get(id);
|
172
|
+
if (info) {
|
173
|
+
mgm.addExportInfo(info);
|
174
|
+
}
|
175
|
+
});
|
176
|
+
mgmData.sideEffects.forEach((id) => {
|
177
|
+
const info = moduleGraph._sideEffectIdMap.get(id);
|
178
|
+
if (info) {
|
179
|
+
mgm.addSideEffect(info);
|
180
|
+
}
|
181
|
+
});
|
182
|
+
mgmData.variables.forEach((id) => {
|
183
|
+
const info = moduleGraph._varIdMap.get(id);
|
184
|
+
if (info) {
|
185
|
+
mgm.addVariable(info);
|
186
|
+
}
|
187
|
+
});
|
188
|
+
}
|
189
|
+
ModuleGraph.init();
|
190
|
+
return moduleGraph;
|
191
|
+
}
|
192
|
+
clear() {
|
193
|
+
this._dependenciesIdMap = /* @__PURE__ */ new Map();
|
194
|
+
this._moduleWebpackIdMap = /* @__PURE__ */ new Map();
|
195
|
+
this._moduleIdMap = /* @__PURE__ */ new Map();
|
196
|
+
this._moduleGraphModules = /* @__PURE__ */ new Map();
|
197
|
+
this._exportIdMap = /* @__PURE__ */ new Map();
|
198
|
+
this._sideEffectIdMap = /* @__PURE__ */ new Map();
|
199
|
+
this._varIdMap = /* @__PURE__ */ new Map();
|
200
|
+
}
|
201
|
+
size() {
|
202
|
+
return this._moduleIdMap.size;
|
203
|
+
}
|
204
|
+
fromInstance(data) {
|
205
|
+
this._dependenciesIdMap = new Map(data._dependenciesIdMap);
|
206
|
+
this._moduleWebpackIdMap = new Map(data._moduleWebpackIdMap);
|
207
|
+
this._moduleIdMap = new Map(data._moduleIdMap);
|
208
|
+
this._moduleGraphModules = new Map(data._moduleGraphModules);
|
209
|
+
this._exportIdMap = new Map(data._exportIdMap);
|
210
|
+
this._sideEffectIdMap = new Map(data._sideEffectIdMap);
|
211
|
+
this._varIdMap = new Map(data._varIdMap);
|
212
|
+
}
|
213
|
+
getSubGraphByModule(module) {
|
214
|
+
const map = /* @__PURE__ */ new Set();
|
215
|
+
const result = [module];
|
216
|
+
map.add(module);
|
217
|
+
for (let i = 0; i < result.length; i++) {
|
218
|
+
const current = result[i];
|
219
|
+
for (const { dependency: depModule } of current.getDependencies()) {
|
220
|
+
if (map.has(depModule)) {
|
221
|
+
continue;
|
222
|
+
}
|
223
|
+
map.add(depModule);
|
224
|
+
result.push(depModule);
|
225
|
+
}
|
226
|
+
}
|
227
|
+
return result;
|
228
|
+
}
|
229
|
+
getModules() {
|
230
|
+
return Array.from(this._moduleWebpackIdMap.values());
|
231
|
+
}
|
232
|
+
getDependencies() {
|
233
|
+
return Array.from(this._dependenciesIdMap.values());
|
234
|
+
}
|
235
|
+
getEntryModules() {
|
236
|
+
return this.getModules().filter(
|
237
|
+
(item) => item.isEntry && item.kind !== SDK.ModuleKind.Concatenation
|
238
|
+
);
|
239
|
+
}
|
240
|
+
getModuleById(id) {
|
241
|
+
return this._moduleIdMap.get(id);
|
242
|
+
}
|
243
|
+
getDependencyById(id) {
|
244
|
+
return this._dependenciesIdMap.get(id);
|
245
|
+
}
|
246
|
+
getModuleByWebpackId(id) {
|
247
|
+
return this._moduleWebpackIdMap.get(id);
|
248
|
+
}
|
249
|
+
getModuleByFile(file) {
|
250
|
+
return this.getModules().find((item) => item.path === file);
|
251
|
+
}
|
252
|
+
addModule(...modules) {
|
253
|
+
for (const module of modules) {
|
254
|
+
if (!this._moduleIdMap.has(module.id)) {
|
255
|
+
this._moduleWebpackIdMap.set(module.webpackId, module);
|
256
|
+
this._moduleIdMap.set(module.id, module);
|
257
|
+
}
|
258
|
+
}
|
259
|
+
}
|
260
|
+
addDependency(...deps) {
|
261
|
+
for (const dep of deps) {
|
262
|
+
if (!this._dependenciesIdMap.has(dep.id)) {
|
263
|
+
this._dependenciesIdMap.set(dep.id, dep);
|
264
|
+
this.addModule(dep.module);
|
265
|
+
this.addModule(dep.dependency);
|
266
|
+
}
|
267
|
+
}
|
268
|
+
}
|
269
|
+
removeModule(module) {
|
270
|
+
this._moduleIdMap.delete(module.id);
|
271
|
+
this._moduleWebpackIdMap.delete(module.webpackId);
|
272
|
+
for (const dep of module.getDependencies()) {
|
273
|
+
this.removeDependency(dep);
|
274
|
+
this._dependenciesIdMap.delete(dep.id);
|
275
|
+
}
|
276
|
+
for (const imported of module.getImported()) {
|
277
|
+
imported.removeDependencyByModule(imported);
|
278
|
+
}
|
279
|
+
}
|
280
|
+
removeDependency(dep) {
|
281
|
+
dep.module.removeDependency(dep);
|
282
|
+
dep.dependency.removeImported(dep.module);
|
283
|
+
this._dependenciesIdMap.delete(dep.id);
|
284
|
+
}
|
285
|
+
addModuleGraphModule(mgm) {
|
286
|
+
if (!this._moduleGraphModules.has(mgm.module)) {
|
287
|
+
this._moduleGraphModules.set(mgm.module, mgm);
|
288
|
+
}
|
289
|
+
}
|
290
|
+
getModuleGraphModule(module) {
|
291
|
+
return this._moduleGraphModules.get(module);
|
292
|
+
}
|
293
|
+
getModuleGraphModules() {
|
294
|
+
return Array.from(this._moduleGraphModules.values());
|
295
|
+
}
|
296
|
+
addExportInfo(data) {
|
297
|
+
this._exportIdMap.set(data.id, data);
|
298
|
+
}
|
299
|
+
addSideEffect(data) {
|
300
|
+
this._sideEffectIdMap.set(data.id, data);
|
301
|
+
}
|
302
|
+
addVariable(data) {
|
303
|
+
this._varIdMap.set(data.id, data);
|
304
|
+
}
|
305
|
+
toData(configs) {
|
306
|
+
return {
|
307
|
+
dependencies: this.getDependencies().map((item) => item.toData()),
|
308
|
+
modules: this.getModules().map(
|
309
|
+
(item) => item.toData(configs?.contextPath)
|
310
|
+
),
|
311
|
+
moduleGraphModules: Array.from(this._moduleGraphModules.values()).map(
|
312
|
+
(item) => item.toData()
|
313
|
+
),
|
314
|
+
exports: Array.from(this._exportIdMap.values()).map(
|
315
|
+
(item) => item.toData()
|
316
|
+
),
|
317
|
+
sideEffects: Array.from(this._sideEffectIdMap.values()).map(
|
318
|
+
(item) => item.toData()
|
319
|
+
),
|
320
|
+
variables: Array.from(this._varIdMap.values()).map(
|
321
|
+
(item) => item.toData()
|
322
|
+
)
|
323
|
+
};
|
324
|
+
}
|
325
|
+
toCodeData(type = SDK.ToDataType.Normal) {
|
326
|
+
const codeMap = {};
|
327
|
+
this.getModules().forEach((item) => {
|
328
|
+
codeMap[item.id] = item.getSource(type);
|
329
|
+
});
|
330
|
+
return codeMap;
|
331
|
+
}
|
332
|
+
}
|
333
|
+
export {
|
334
|
+
ModuleGraph
|
335
|
+
};
|
@@ -0,0 +1,250 @@
|
|
1
|
+
import { SDK } from "@rsdoctor/types";
|
2
|
+
import path from "path";
|
3
|
+
import { isNumber } from "lodash";
|
4
|
+
import { Dependency } from "./dependency";
|
5
|
+
import { Statement } from "./statement";
|
6
|
+
import { getModuleName } from "./utils";
|
7
|
+
let id = 1;
|
8
|
+
class Module {
|
9
|
+
constructor(webpackId, path2, isEntry = false, kind = SDK.ModuleKind.Normal) {
|
10
|
+
this.source = {
|
11
|
+
source: "",
|
12
|
+
transformed: "",
|
13
|
+
parsedSource: ""
|
14
|
+
};
|
15
|
+
this.size = {
|
16
|
+
sourceSize: 0,
|
17
|
+
transformedSize: 0,
|
18
|
+
parsedSize: 0
|
19
|
+
};
|
20
|
+
this.chunks = [];
|
21
|
+
this.dependencies = [];
|
22
|
+
this.imported = [];
|
23
|
+
this.modules = [];
|
24
|
+
this.concatenationModules = [];
|
25
|
+
this.meta = {
|
26
|
+
hasSetEsModuleStatement: false,
|
27
|
+
strictHarmonyModule: false
|
28
|
+
};
|
29
|
+
this.id = id++;
|
30
|
+
this.webpackId = webpackId;
|
31
|
+
this.path = path2;
|
32
|
+
this.isEntry = isEntry;
|
33
|
+
this.kind = kind;
|
34
|
+
}
|
35
|
+
static init() {
|
36
|
+
id = 1;
|
37
|
+
}
|
38
|
+
get rootModule() {
|
39
|
+
return this.modules.find((item) => item.path === this.path);
|
40
|
+
}
|
41
|
+
get isPreferSource() {
|
42
|
+
if (typeof this._isPreferSource === "boolean") {
|
43
|
+
return this._isPreferSource;
|
44
|
+
}
|
45
|
+
const result = this.source.source.length > 0 && this.source.source !== "test code" && Boolean(this.sourceMap);
|
46
|
+
this._isPreferSource = result;
|
47
|
+
return result;
|
48
|
+
}
|
49
|
+
getChunks() {
|
50
|
+
return this.chunks.slice();
|
51
|
+
}
|
52
|
+
addChunk(chunk) {
|
53
|
+
if (!this.chunks.includes(chunk)) {
|
54
|
+
this.chunks.push(chunk);
|
55
|
+
chunk.addModule(this);
|
56
|
+
}
|
57
|
+
}
|
58
|
+
removeChunk(chunk) {
|
59
|
+
this.chunks = this.chunks.filter((item) => item !== chunk);
|
60
|
+
}
|
61
|
+
getDependencies() {
|
62
|
+
return this.dependencies.slice();
|
63
|
+
}
|
64
|
+
getDependencyByRequest(request) {
|
65
|
+
return this.dependencies.find((item) => item.request === request);
|
66
|
+
}
|
67
|
+
getDependencyByModule(module) {
|
68
|
+
return this.dependencies.find(
|
69
|
+
(item) => item.originDependency === module || item.dependency === module
|
70
|
+
);
|
71
|
+
}
|
72
|
+
addDependency(request, module, kind, statements) {
|
73
|
+
const dep = new Dependency(request, this, module, kind, statements);
|
74
|
+
if (this.dependencies.every((item) => !item.isSameWithoutStatements(dep))) {
|
75
|
+
this.dependencies.push(dep);
|
76
|
+
module.addImported(this);
|
77
|
+
if (module.rootModule) {
|
78
|
+
module.rootModule.addImported(this);
|
79
|
+
}
|
80
|
+
return dep;
|
81
|
+
}
|
82
|
+
}
|
83
|
+
removeDependency(dep) {
|
84
|
+
this.dependencies = this.dependencies.filter((item) => item === dep);
|
85
|
+
}
|
86
|
+
removeDependencyByModule(module) {
|
87
|
+
const dep = this.getDependencyByModule(module);
|
88
|
+
if (dep) {
|
89
|
+
this.removeDependency(dep);
|
90
|
+
}
|
91
|
+
}
|
92
|
+
getImported() {
|
93
|
+
return this.imported.slice();
|
94
|
+
}
|
95
|
+
addImported(module) {
|
96
|
+
if (!this.imported.includes(module)) {
|
97
|
+
this.imported.push(module);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
removeImported(module) {
|
101
|
+
this.imported = this.imported.filter((item) => item === module);
|
102
|
+
}
|
103
|
+
setProgram(program) {
|
104
|
+
this.program = program;
|
105
|
+
}
|
106
|
+
getProgram() {
|
107
|
+
return this.program;
|
108
|
+
}
|
109
|
+
setSource(input) {
|
110
|
+
const { source } = this;
|
111
|
+
source.source = input.source ?? source.source;
|
112
|
+
source.transformed = input.transformed ?? source.transformed;
|
113
|
+
source.parsedSource = input.parsedSource ?? source.parsedSource;
|
114
|
+
}
|
115
|
+
getSource(type = SDK.ToDataType.Normal) {
|
116
|
+
return type === SDK.ToDataType.Lite ? {
|
117
|
+
source: "",
|
118
|
+
transformed: "",
|
119
|
+
parsedSource: this.source.parsedSource
|
120
|
+
} : this.isPreferSource ? {
|
121
|
+
source: this.source.source,
|
122
|
+
transformed: "",
|
123
|
+
parsedSource: ""
|
124
|
+
} : {
|
125
|
+
source: this.source.source,
|
126
|
+
transformed: this.source.transformed,
|
127
|
+
parsedSource: this.source.parsedSource
|
128
|
+
};
|
129
|
+
}
|
130
|
+
setSourceMap(sourceMap) {
|
131
|
+
this.sourceMap = sourceMap;
|
132
|
+
}
|
133
|
+
getSourceMap() {
|
134
|
+
return this.sourceMap;
|
135
|
+
}
|
136
|
+
setSize(input) {
|
137
|
+
const { size } = this;
|
138
|
+
size.sourceSize = input.sourceSize ?? size.sourceSize;
|
139
|
+
size.transformedSize = input.transformedSize ?? size.transformedSize;
|
140
|
+
size.parsedSize = input.parsedSize ?? size.parsedSize;
|
141
|
+
}
|
142
|
+
getSize() {
|
143
|
+
return { ...this.size };
|
144
|
+
}
|
145
|
+
getStatement(transformed) {
|
146
|
+
return new Statement(this, {
|
147
|
+
source: this.getSourceRange(transformed),
|
148
|
+
transformed: {
|
149
|
+
start: { ...transformed.start },
|
150
|
+
end: transformed.end ? { ...transformed.end } : void 0
|
151
|
+
}
|
152
|
+
});
|
153
|
+
}
|
154
|
+
getSourceRange(transformed) {
|
155
|
+
const { sourceMap } = this;
|
156
|
+
if (!sourceMap) {
|
157
|
+
return;
|
158
|
+
}
|
159
|
+
const source = {
|
160
|
+
start: {}
|
161
|
+
};
|
162
|
+
const startInSource = sourceMap.originalPositionFor({
|
163
|
+
line: transformed.start.line ?? 0,
|
164
|
+
column: transformed.start.column ?? 0,
|
165
|
+
// The largest lower bound.
|
166
|
+
bias: 1
|
167
|
+
});
|
168
|
+
if (isNumber(startInSource.line)) {
|
169
|
+
source.start = {
|
170
|
+
line: startInSource.line,
|
171
|
+
column: startInSource.column ?? void 0
|
172
|
+
};
|
173
|
+
}
|
174
|
+
if (transformed.end) {
|
175
|
+
const endInSource = sourceMap.originalPositionFor({
|
176
|
+
line: transformed.end.line ?? 0,
|
177
|
+
column: transformed.end.column ?? 0
|
178
|
+
// The smallest lower bound
|
179
|
+
// bias: 2,
|
180
|
+
});
|
181
|
+
if (isNumber(endInSource.line)) {
|
182
|
+
source.end = {
|
183
|
+
line: endInSource.line,
|
184
|
+
column: endInSource.column ?? void 0
|
185
|
+
};
|
186
|
+
}
|
187
|
+
}
|
188
|
+
return source;
|
189
|
+
}
|
190
|
+
addNormalModule(module) {
|
191
|
+
if (!this.modules.includes(module)) {
|
192
|
+
this.modules.push(module);
|
193
|
+
module.addConcatenationModule(this);
|
194
|
+
}
|
195
|
+
}
|
196
|
+
getNormalModules() {
|
197
|
+
return this.modules.slice();
|
198
|
+
}
|
199
|
+
addConcatenationModule(module) {
|
200
|
+
if (!this.concatenationModules.includes(module)) {
|
201
|
+
this.concatenationModules.push(module);
|
202
|
+
}
|
203
|
+
}
|
204
|
+
getConcatenationModules() {
|
205
|
+
return this.concatenationModules.slice();
|
206
|
+
}
|
207
|
+
toData(contextPath) {
|
208
|
+
const { isPreferSource } = this;
|
209
|
+
const moduleName = getModuleName(this.webpackId);
|
210
|
+
const data = {
|
211
|
+
id: this.id,
|
212
|
+
webpackId: contextPath && moduleName.indexOf(".") > 0 ? path.relative(contextPath, moduleName) : this.webpackId,
|
213
|
+
path: this.path,
|
214
|
+
isPreferSource,
|
215
|
+
dependencies: this.dependencies.map((item) => item.id),
|
216
|
+
imported: this.imported.map((item) => item.id),
|
217
|
+
chunks: this.chunks.map((item) => item.id),
|
218
|
+
size: this.getSize(),
|
219
|
+
kind: this.kind
|
220
|
+
};
|
221
|
+
if (this.meta.hasSetEsModuleStatement || this.meta.strictHarmonyModule) {
|
222
|
+
data.meta = {};
|
223
|
+
if (this.meta.hasSetEsModuleStatement) {
|
224
|
+
data.meta.hasSetEsModuleStatement = true;
|
225
|
+
}
|
226
|
+
if (this.meta.strictHarmonyModule) {
|
227
|
+
data.meta.strictHarmonyModule = true;
|
228
|
+
}
|
229
|
+
}
|
230
|
+
if (this.isEntry) {
|
231
|
+
data.isEntry = this.isEntry;
|
232
|
+
}
|
233
|
+
if (this.modules.length > 0) {
|
234
|
+
data.modules = this.modules.map((item) => item.id);
|
235
|
+
}
|
236
|
+
if (this.rootModule) {
|
237
|
+
data.rootModule = this.rootModule.id;
|
238
|
+
}
|
239
|
+
if (this.concatenationModules.length > 0) {
|
240
|
+
data.concatenationModules = this.concatenationModules.map(
|
241
|
+
(data2) => data2.id
|
242
|
+
);
|
243
|
+
}
|
244
|
+
return data;
|
245
|
+
}
|
246
|
+
}
|
247
|
+
Module.kind = SDK.ModuleKind;
|
248
|
+
export {
|
249
|
+
Module
|
250
|
+
};
|
@@ -0,0 +1,58 @@
|
|
1
|
+
import { isSameRange } from "./utils";
|
2
|
+
class Statement {
|
3
|
+
static getDefaultStatement(module) {
|
4
|
+
const defaultPosition = {
|
5
|
+
line: 1,
|
6
|
+
column: 0
|
7
|
+
};
|
8
|
+
const defaultRange = {
|
9
|
+
start: { ...defaultPosition },
|
10
|
+
end: { ...defaultPosition }
|
11
|
+
};
|
12
|
+
return new Statement(module, {
|
13
|
+
source: { ...defaultRange },
|
14
|
+
transformed: { ...defaultRange }
|
15
|
+
});
|
16
|
+
}
|
17
|
+
constructor(module, position) {
|
18
|
+
this.module = module;
|
19
|
+
this.position = position;
|
20
|
+
}
|
21
|
+
isSame(statement) {
|
22
|
+
return this.module.id === statement.module.id && isSameRange(this.position.transformed, statement.position.transformed);
|
23
|
+
}
|
24
|
+
getSourcePosition() {
|
25
|
+
const { module, position } = this;
|
26
|
+
if (position.source) {
|
27
|
+
return position.source;
|
28
|
+
}
|
29
|
+
if (module.getSourceMap()) {
|
30
|
+
position.source = module.getSourceRange(position.transformed);
|
31
|
+
return position.source;
|
32
|
+
}
|
33
|
+
}
|
34
|
+
getLineCode() {
|
35
|
+
const useSource = this.module.isPreferSource && this.position.source;
|
36
|
+
const sourceCode = this.module.getSource();
|
37
|
+
const source = useSource ? sourceCode.source : sourceCode.transformed;
|
38
|
+
const line = useSource ? this.position.source.start.line : this.position.transformed.start.line;
|
39
|
+
if (typeof line === "number") {
|
40
|
+
return source.split("\n")[line - 1];
|
41
|
+
}
|
42
|
+
}
|
43
|
+
toData() {
|
44
|
+
const position = {
|
45
|
+
transformed: this.position.transformed
|
46
|
+
};
|
47
|
+
if (this.position.source) {
|
48
|
+
position.source = this.position.source;
|
49
|
+
}
|
50
|
+
return {
|
51
|
+
position,
|
52
|
+
module: this.module.id
|
53
|
+
};
|
54
|
+
}
|
55
|
+
}
|
56
|
+
export {
|
57
|
+
Statement
|
58
|
+
};
|