@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.
Files changed (81) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +15 -0
  3. package/dist/cjs/graph/chunk-graph/asset.js +44 -0
  4. package/dist/cjs/graph/chunk-graph/chunk.js +105 -0
  5. package/dist/cjs/graph/chunk-graph/entrypoint.js +52 -0
  6. package/dist/cjs/graph/chunk-graph/graph.js +88 -0
  7. package/dist/cjs/graph/chunk-graph/index.js +28 -0
  8. package/dist/cjs/graph/index.js +26 -0
  9. package/dist/cjs/graph/module-graph/dependency.js +91 -0
  10. package/dist/cjs/graph/module-graph/graph.js +354 -0
  11. package/dist/cjs/graph/module-graph/index.js +32 -0
  12. package/dist/cjs/graph/module-graph/module.js +284 -0
  13. package/dist/cjs/graph/module-graph/statement.js +82 -0
  14. package/dist/cjs/graph/module-graph/tree-shaking/export.js +107 -0
  15. package/dist/cjs/graph/module-graph/tree-shaking/index.js +30 -0
  16. package/dist/cjs/graph/module-graph/tree-shaking/module.js +98 -0
  17. package/dist/cjs/graph/module-graph/tree-shaking/sideEffect.js +94 -0
  18. package/dist/cjs/graph/module-graph/tree-shaking/types.js +16 -0
  19. package/dist/cjs/graph/module-graph/tree-shaking/variable.js +63 -0
  20. package/dist/cjs/graph/module-graph/types.js +16 -0
  21. package/dist/cjs/graph/module-graph/utils.js +65 -0
  22. package/dist/cjs/graph/package-graph/dependency.js +56 -0
  23. package/dist/cjs/graph/package-graph/graph.js +173 -0
  24. package/dist/cjs/graph/package-graph/index.js +28 -0
  25. package/dist/cjs/graph/package-graph/package.js +121 -0
  26. package/dist/cjs/graph/package-graph/types.js +16 -0
  27. package/dist/cjs/graph/package-graph/utils.js +30 -0
  28. package/dist/cjs/index.js +22 -0
  29. package/dist/esm/graph/chunk-graph/asset.js +20 -0
  30. package/dist/esm/graph/chunk-graph/chunk.js +81 -0
  31. package/dist/esm/graph/chunk-graph/entrypoint.js +28 -0
  32. package/dist/esm/graph/chunk-graph/graph.js +64 -0
  33. package/dist/esm/graph/chunk-graph/index.js +4 -0
  34. package/dist/esm/graph/index.js +3 -0
  35. package/dist/esm/graph/module-graph/dependency.js +67 -0
  36. package/dist/esm/graph/module-graph/graph.js +335 -0
  37. package/dist/esm/graph/module-graph/index.js +6 -0
  38. package/dist/esm/graph/module-graph/module.js +250 -0
  39. package/dist/esm/graph/module-graph/statement.js +58 -0
  40. package/dist/esm/graph/module-graph/tree-shaking/export.js +83 -0
  41. package/dist/esm/graph/module-graph/tree-shaking/index.js +5 -0
  42. package/dist/esm/graph/module-graph/tree-shaking/module.js +74 -0
  43. package/dist/esm/graph/module-graph/tree-shaking/sideEffect.js +70 -0
  44. package/dist/esm/graph/module-graph/tree-shaking/types.js +0 -0
  45. package/dist/esm/graph/module-graph/tree-shaking/variable.js +39 -0
  46. package/dist/esm/graph/module-graph/types.js +0 -0
  47. package/dist/esm/graph/module-graph/utils.js +39 -0
  48. package/dist/esm/graph/package-graph/dependency.js +32 -0
  49. package/dist/esm/graph/package-graph/graph.js +149 -0
  50. package/dist/esm/graph/package-graph/index.js +4 -0
  51. package/dist/esm/graph/package-graph/package.js +97 -0
  52. package/dist/esm/graph/package-graph/types.js +0 -0
  53. package/dist/esm/graph/package-graph/utils.js +6 -0
  54. package/dist/esm/index.js +1 -0
  55. package/dist/type/graph/chunk-graph/asset.d.ts +10 -0
  56. package/dist/type/graph/chunk-graph/chunk.d.ts +30 -0
  57. package/dist/type/graph/chunk-graph/entrypoint.d.ts +12 -0
  58. package/dist/type/graph/chunk-graph/graph.d.ts +22 -0
  59. package/dist/type/graph/chunk-graph/index.d.ts +4 -0
  60. package/dist/type/graph/index.d.ts +3 -0
  61. package/dist/type/graph/module-graph/dependency.d.ts +25 -0
  62. package/dist/type/graph/module-graph/graph.d.ts +38 -0
  63. package/dist/type/graph/module-graph/index.d.ts +6 -0
  64. package/dist/type/graph/module-graph/module.d.ts +64 -0
  65. package/dist/type/graph/module-graph/statement.d.ts +12 -0
  66. package/dist/type/graph/module-graph/tree-shaking/export.d.ts +22 -0
  67. package/dist/type/graph/module-graph/tree-shaking/index.d.ts +5 -0
  68. package/dist/type/graph/module-graph/tree-shaking/module.d.ts +28 -0
  69. package/dist/type/graph/module-graph/tree-shaking/sideEffect.d.ts +27 -0
  70. package/dist/type/graph/module-graph/tree-shaking/types.d.ts +5 -0
  71. package/dist/type/graph/module-graph/tree-shaking/variable.d.ts +18 -0
  72. package/dist/type/graph/module-graph/types.d.ts +12 -0
  73. package/dist/type/graph/module-graph/utils.d.ts +4 -0
  74. package/dist/type/graph/package-graph/dependency.d.ts +15 -0
  75. package/dist/type/graph/package-graph/graph.d.ts +25 -0
  76. package/dist/type/graph/package-graph/index.d.ts +4 -0
  77. package/dist/type/graph/package-graph/package.d.ts +25 -0
  78. package/dist/type/graph/package-graph/types.d.ts +6 -0
  79. package/dist/type/graph/package-graph/utils.d.ts +1 -0
  80. package/dist/type/index.d.ts +1 -0
  81. package/package.json +42 -0
@@ -0,0 +1,83 @@
1
+ let id = 1;
2
+ class ExportInfo {
3
+ constructor(name, identifier, variable) {
4
+ this.id = id++;
5
+ this._sideEffects = [];
6
+ this.name = name;
7
+ this.identifier = identifier;
8
+ this._variable = variable;
9
+ }
10
+ static init() {
11
+ id = 1;
12
+ }
13
+ get isReExport() {
14
+ return Boolean(this.from);
15
+ }
16
+ set variable(data) {
17
+ this._variable = data;
18
+ }
19
+ get variable() {
20
+ if (this._variable) {
21
+ return this._variable;
22
+ }
23
+ if (!this.from || this._variable === false) {
24
+ this._variable = false;
25
+ return;
26
+ }
27
+ const result = this.getRecursiveExport()?.variable;
28
+ if (!result) {
29
+ this._variable = false;
30
+ }
31
+ return result;
32
+ }
33
+ addSideEffect(info) {
34
+ if (this._sideEffects.every((item) => item.id !== info.id)) {
35
+ this._sideEffects.push(info);
36
+ if (this.from) {
37
+ this.from.addSideEffect(info);
38
+ }
39
+ }
40
+ }
41
+ getSideEffects() {
42
+ return this._sideEffects.slice();
43
+ }
44
+ setFromExport(from) {
45
+ this.from = from;
46
+ }
47
+ getRecursiveExport(depth = Infinity) {
48
+ if (depth === 0) {
49
+ throw new Error(
50
+ "`getRecursiveExport` method parameter depth must be greater than 1."
51
+ );
52
+ }
53
+ if (!this.from) {
54
+ return this;
55
+ }
56
+ let currentDepth = 0;
57
+ let current = this;
58
+ while (current.from && currentDepth < depth) {
59
+ currentDepth++;
60
+ current = current.from;
61
+ }
62
+ return current;
63
+ }
64
+ toData() {
65
+ const data = {
66
+ id: this.id,
67
+ name: this.name,
68
+ isReExport: this.isReExport,
69
+ used: this._sideEffects.map((item) => item.id)
70
+ };
71
+ if (this.from) {
72
+ data.from = this.from.id;
73
+ data.root = this.getRecursiveExport().id;
74
+ }
75
+ if (this.variable) {
76
+ data.variable = this.variable.id;
77
+ }
78
+ return data;
79
+ }
80
+ }
81
+ export {
82
+ ExportInfo
83
+ };
@@ -0,0 +1,5 @@
1
+ export * from "./module";
2
+ export * from "./export";
3
+ export * from "./variable";
4
+ export * from "./sideEffect";
5
+ export * from "./types";
@@ -0,0 +1,74 @@
1
+ import { ExportInfo } from "./export";
2
+ import { SideEffect } from "./sideEffect";
3
+ import { Variable } from "./variable";
4
+ class ModuleGraphModule {
5
+ constructor(module, graph, dynamic) {
6
+ this.exports = [];
7
+ this.sideEffects = [];
8
+ this.variables = [];
9
+ this.module = module;
10
+ this._graph = graph;
11
+ if (typeof this._dynamic === "boolean") {
12
+ this._dynamic = dynamic;
13
+ }
14
+ }
15
+ static init() {
16
+ ExportInfo.init();
17
+ SideEffect.init();
18
+ Variable.init();
19
+ }
20
+ get dynamic() {
21
+ if (typeof this._dynamic === "boolean") {
22
+ return this._dynamic;
23
+ }
24
+ return this.module.getImported().map((item) => item.getDependencyByModule(this.module)).some((item) => item && item.meta.exportsType === "dynamic");
25
+ }
26
+ addExportInfo(data) {
27
+ this.exports.push(data);
28
+ this._graph.addExportInfo(data);
29
+ }
30
+ addSideEffect(data) {
31
+ this.sideEffects.push(data);
32
+ this._graph.addSideEffect(data);
33
+ }
34
+ addVariable(data) {
35
+ this.variables.push(data);
36
+ this._graph.addVariable(data);
37
+ }
38
+ getExports() {
39
+ return this.exports.slice();
40
+ }
41
+ getSideEffects(name) {
42
+ if (name) {
43
+ return this.sideEffects.filter((item) => item.name === name);
44
+ }
45
+ return this.sideEffects.slice();
46
+ }
47
+ getOwnExports() {
48
+ return this.exports.filter((item) => !item.isReExport);
49
+ }
50
+ getExport(name) {
51
+ return this.exports.find((item) => item.name === name);
52
+ }
53
+ getReExports() {
54
+ return this.exports.filter((item) => item.isReExport);
55
+ }
56
+ getOwnExport(name) {
57
+ return this.getOwnExports().find((item) => item.name === name);
58
+ }
59
+ getReExport(name) {
60
+ return this.getReExports().find((item) => item.name === name);
61
+ }
62
+ toData() {
63
+ return {
64
+ module: this.module.id,
65
+ dynamic: this.dynamic,
66
+ exports: this.exports.map((item) => item.id),
67
+ sideEffects: this.sideEffects.map((item) => item.id),
68
+ variables: this.variables.map((item) => item.id)
69
+ };
70
+ }
71
+ }
72
+ export {
73
+ ModuleGraphModule
74
+ };
@@ -0,0 +1,70 @@
1
+ let id = 1;
2
+ const _SideEffect = class _SideEffect {
3
+ constructor(name, module, identifier, fromRequest, originName) {
4
+ this.id = id++;
5
+ this._exports = [];
6
+ this.name = name;
7
+ this.module = module;
8
+ this.identifier = identifier;
9
+ if (fromRequest) {
10
+ this.fromDependency = this.module.getDependencyByRequest(fromRequest);
11
+ }
12
+ if (originName === _SideEffect.NamespaceSymbol) {
13
+ this.isNameSpace = true;
14
+ } else {
15
+ this.isNameSpace = false;
16
+ this.originName = originName;
17
+ }
18
+ }
19
+ static init() {
20
+ id = 1;
21
+ }
22
+ get variable() {
23
+ if (typeof this._variable !== "undefined") {
24
+ if (this._variable) {
25
+ return this._variable;
26
+ }
27
+ return;
28
+ }
29
+ const result = this.exports[0]?.getRecursiveExport()?.variable;
30
+ this._variable = result ?? false;
31
+ return result;
32
+ }
33
+ get exports() {
34
+ return this._exports?.slice() ?? [];
35
+ }
36
+ setModuleExport(mgm) {
37
+ mgm.getExports().forEach((info) => this.setExportInfo(info));
38
+ }
39
+ setExportInfo(info) {
40
+ this._exports.push(info);
41
+ info.addSideEffect(this);
42
+ }
43
+ toData() {
44
+ const data = {
45
+ id: this.id,
46
+ name: this.name,
47
+ identifier: this.identifier.toData(),
48
+ module: this.module.id
49
+ };
50
+ if (this.fromDependency) {
51
+ data.fromDependency = this.fromDependency.id;
52
+ }
53
+ if (this.isNameSpace) {
54
+ data.isNameSpace = this.isNameSpace;
55
+ }
56
+ if (this.exports.length > 0) {
57
+ data.exports = this.exports.map((item) => item.id);
58
+ }
59
+ if (this.variable) {
60
+ data.variable = this.variable.id;
61
+ }
62
+ return data;
63
+ }
64
+ };
65
+ /** Name import tags */
66
+ _SideEffect.NamespaceSymbol = Symbol("namespace");
67
+ let SideEffect = _SideEffect;
68
+ export {
69
+ SideEffect
70
+ };
@@ -0,0 +1,39 @@
1
+ let id = 1;
2
+ class Variable {
3
+ constructor(name, module, usedInfo, identifier) {
4
+ this.id = id++;
5
+ this.name = name;
6
+ this.module = module;
7
+ this.usedInfo = usedInfo;
8
+ this.identifier = identifier;
9
+ }
10
+ static init() {
11
+ id = 1;
12
+ }
13
+ get isUsed() {
14
+ return this._exported ? this._exported.getSideEffects().length > 0 : false;
15
+ }
16
+ setExportInfo(info) {
17
+ this._exported = info;
18
+ info.variable = this;
19
+ }
20
+ getExportInfo() {
21
+ return this._exported;
22
+ }
23
+ toData() {
24
+ const data = {
25
+ id: this.id,
26
+ name: this.name,
27
+ module: this.module.id,
28
+ identifier: this.identifier.toData(),
29
+ usedInfo: this.usedInfo
30
+ };
31
+ if (this._exported) {
32
+ data.exported = this._exported.id;
33
+ }
34
+ return data;
35
+ }
36
+ }
37
+ export {
38
+ Variable
39
+ };
File without changes
@@ -0,0 +1,39 @@
1
+ import { last, isUndefined, isNil } from "lodash";
2
+ function isSamePosition(po1, po2) {
3
+ return po1.line === po2.line && po1.column === po2.column;
4
+ }
5
+ function isSameRange(po1, po2) {
6
+ if (!isSamePosition(po1.start, po2.start)) {
7
+ return false;
8
+ }
9
+ if (!isNil(po1.end) && !isNil(po2.end)) {
10
+ return isSamePosition(po1.end, po2.end);
11
+ }
12
+ return isUndefined(po1.end) && isUndefined(po2.end);
13
+ }
14
+ const NAME_WITH_LOADERS = /!/;
15
+ const NAME_WITH_MODULES = /\s\+\s\d*\smodules$/;
16
+ const INVALID_CSS_PREFIX = /^css\s.*node_modules(?!\/)/;
17
+ function getModuleName(name) {
18
+ if (!name) {
19
+ return "";
20
+ }
21
+ if (NAME_WITH_LOADERS.test(name)) {
22
+ const normalizedName = last(name.split(NAME_WITH_LOADERS));
23
+ if (normalizedName?.trim()) {
24
+ return normalizedName;
25
+ }
26
+ }
27
+ if (NAME_WITH_MODULES.test(name)) {
28
+ return name.replace(NAME_WITH_MODULES, "");
29
+ }
30
+ if (INVALID_CSS_PREFIX.test(name)) {
31
+ return name.replace(INVALID_CSS_PREFIX, "");
32
+ }
33
+ return name;
34
+ }
35
+ export {
36
+ getModuleName,
37
+ isSamePosition,
38
+ isSameRange
39
+ };
@@ -0,0 +1,32 @@
1
+ let id = 1;
2
+ class PackageDependency {
3
+ constructor(pack, dep, refDependency) {
4
+ this.id = id++;
5
+ this.package = pack;
6
+ this.dependency = dep;
7
+ this.refDependency = refDependency;
8
+ }
9
+ get name() {
10
+ return this.dependency.name;
11
+ }
12
+ get version() {
13
+ return this.dependency.version;
14
+ }
15
+ get root() {
16
+ return this.dependency.root;
17
+ }
18
+ isSame(dep) {
19
+ return this.refDependency === dep.refDependency && this.dependency.isSame(dep.dependency);
20
+ }
21
+ toData() {
22
+ return {
23
+ id: this.id,
24
+ dependency: this.dependency.id,
25
+ package: this.package.id,
26
+ refDependency: this.refDependency.id
27
+ };
28
+ }
29
+ }
30
+ export {
31
+ PackageDependency
32
+ };
@@ -0,0 +1,149 @@
1
+ import { unionBy } from "lodash";
2
+ import { dirname, join, resolve } from "path";
3
+ import { SDK } from "@rsdoctor/types";
4
+ import { Package as PackageUtil } from "@rsdoctor/utils/common";
5
+ import { Package } from "./package";
6
+ import { PackageDependency } from "./dependency";
7
+ class PackageGraph {
8
+ constructor(root) {
9
+ this._dependencies = [];
10
+ this._packages = [];
11
+ this._pkgNameMap = /* @__PURE__ */ new Map();
12
+ this._pkgFileMap = /* @__PURE__ */ new Map();
13
+ this._root = root;
14
+ }
15
+ static fromModuleGraph(graph, root, getPackageFile) {
16
+ const pkgGraph = new PackageGraph(root);
17
+ const modules = graph.getModules().filter((item) => item.kind === SDK.ModuleKind.Normal);
18
+ for (const item of modules) {
19
+ const pkg = pkgGraph.getPackageByModule(item, getPackageFile);
20
+ if (pkg) {
21
+ pkgGraph.addPackage(pkg);
22
+ pkg.addModule(item);
23
+ }
24
+ }
25
+ for (const dep of graph.getDependencies()) {
26
+ const modulePkg = pkgGraph.getPackageByFile(dep.module.path);
27
+ const dependencyPkg = pkgGraph.getPackageByFile(dep.dependency.path);
28
+ if (modulePkg && dependencyPkg && !modulePkg.isSame(dependencyPkg)) {
29
+ const pkgDep = new PackageDependency(modulePkg, dependencyPkg, dep);
30
+ pkgGraph.addDependency(pkgDep);
31
+ modulePkg.addDependency(pkgDep);
32
+ }
33
+ }
34
+ return pkgGraph;
35
+ }
36
+ getPackages() {
37
+ return this._packages.slice();
38
+ }
39
+ getPackageByModule(module, readFile) {
40
+ const { path: file, meta } = module;
41
+ const { _pkgFileMap: pkgsMap } = this;
42
+ const getPackageByData = (data2) => {
43
+ return this.getPackageByData(data2) ?? new Package(data2.name, data2.root, data2.version);
44
+ };
45
+ if (pkgsMap.has(file)) {
46
+ return pkgsMap.get(file);
47
+ }
48
+ if (meta.packageData) {
49
+ const pkg2 = getPackageByData(meta.packageData);
50
+ pkgsMap.set(file, pkg2);
51
+ return pkg2;
52
+ }
53
+ const readPackageJson = (file2, readFile2) => {
54
+ let result;
55
+ let current = file2;
56
+ while (current !== "/" && !result) {
57
+ if (dirname(current) === current) {
58
+ break;
59
+ }
60
+ current = dirname(current);
61
+ if (readFile2) {
62
+ result = readFile2(join(current, "package.json")) || PackageUtil.getPackageMetaFromModulePath(file2);
63
+ }
64
+ if (!readFile2) {
65
+ result = PackageUtil.getPackageMetaFromModulePath(file2);
66
+ } else if (!result?.name) {
67
+ result = void 0;
68
+ }
69
+ }
70
+ if (!result) {
71
+ return;
72
+ }
73
+ if (readFile2 && (!result.name || !result.version)) {
74
+ return readPackageJson(dirname(current), readFile2);
75
+ }
76
+ return {
77
+ ...result,
78
+ root: current
79
+ };
80
+ };
81
+ const cache = this.getPackageContainFile(file);
82
+ if (cache) {
83
+ pkgsMap.set(file, cache);
84
+ return cache;
85
+ }
86
+ const data = readPackageJson(file, readFile);
87
+ if (!data) {
88
+ return;
89
+ }
90
+ if (data.root.startsWith(".")) {
91
+ data.root = resolve(this._root, data.root);
92
+ }
93
+ const pkg = getPackageByData(data);
94
+ this.addPackage(pkg);
95
+ pkgsMap.set(file, pkg);
96
+ return pkg;
97
+ }
98
+ getPackageByFile(file) {
99
+ return this._pkgFileMap.get(file);
100
+ }
101
+ getPackageContainFile(file) {
102
+ return this._packages.find((pkg) => pkg.contain(file));
103
+ }
104
+ getPackagesByName(name) {
105
+ return this._pkgNameMap.get(name) ?? [];
106
+ }
107
+ getPackageByData(data) {
108
+ return this._pkgNameMap.get(data.name)?.find(
109
+ (item) => item.version === data.version && item.root === data.root
110
+ );
111
+ }
112
+ addPackage(pkg) {
113
+ if (this._packages.every((item) => !item.isSame(pkg))) {
114
+ this._packages.push(pkg);
115
+ const { _pkgNameMap: map } = this;
116
+ const arr = map.get(pkg.name) ?? [];
117
+ if (arr.every((item) => !item.isSame(pkg))) {
118
+ arr.push(pkg);
119
+ map.set(pkg.name, arr);
120
+ }
121
+ }
122
+ }
123
+ getDependenciesFromPackage(pkg) {
124
+ return this._dependencies.filter((dep) => dep.dependency === pkg);
125
+ }
126
+ addDependency(dep) {
127
+ if (this._dependencies.every((item) => !item.isSame(dep))) {
128
+ this._dependencies.push(dep);
129
+ }
130
+ }
131
+ getDependenciesFromOrigin() {
132
+ return this._dependencies.filter((item) => !item.package);
133
+ }
134
+ getDuplicatePackages() {
135
+ return unionBy(
136
+ Array.from(this._pkgNameMap.values()).filter((pkgs) => pkgs.length > 1),
137
+ (pkgs) => pkgs[0].name
138
+ );
139
+ }
140
+ toData() {
141
+ return {
142
+ packages: this._packages.map((e) => e.toData()),
143
+ dependencies: this._dependencies.map((d) => d.toData())
144
+ };
145
+ }
146
+ }
147
+ export {
148
+ PackageGraph
149
+ };
@@ -0,0 +1,4 @@
1
+ export * from "./dependency";
2
+ export * from "./graph";
3
+ export * from "./package";
4
+ export * from "./types";
@@ -0,0 +1,97 @@
1
+ import { relative } from "path";
2
+ import { isPackagePath } from "./utils";
3
+ let id = 1;
4
+ class Package {
5
+ constructor(name, root, version) {
6
+ this.id = id++;
7
+ this._modules = [];
8
+ this._dependencies = [];
9
+ this._imported = [];
10
+ this.name = name;
11
+ this.root = root;
12
+ this.version = version;
13
+ }
14
+ getModules() {
15
+ return this._modules.slice();
16
+ }
17
+ getDependencies() {
18
+ return this._dependencies.slice();
19
+ }
20
+ getImported() {
21
+ return this._imported.slice();
22
+ }
23
+ addModule(module) {
24
+ if (!this._modules.includes(module)) {
25
+ this._modules.push(module);
26
+ }
27
+ }
28
+ addDependency(dep) {
29
+ if (this._dependencies.every((item) => !item.isSame(dep))) {
30
+ this._dependencies.push(dep);
31
+ dep.dependency.addImported(this);
32
+ }
33
+ }
34
+ getDependenciesChain(graph) {
35
+ function getImported(pkg, ans) {
36
+ const dependencies = graph.getDependenciesFromPackage(pkg);
37
+ for (const dep of dependencies) {
38
+ if (!dep.refDependency) {
39
+ continue;
40
+ }
41
+ if (ans.some((dep2) => dep2.dependency === pkg)) {
42
+ continue;
43
+ }
44
+ if (!dep.package) {
45
+ return ans.concat(dep);
46
+ }
47
+ return getImported(dep.package, ans.concat(dep));
48
+ }
49
+ return ans;
50
+ }
51
+ return getImported(this, []);
52
+ }
53
+ addImported(pkg) {
54
+ if (!this._imported.includes(pkg)) {
55
+ this._imported.push(pkg);
56
+ }
57
+ }
58
+ contain(file) {
59
+ const subPath = relative(this.root, file);
60
+ if (subPath.startsWith("..")) {
61
+ return false;
62
+ }
63
+ return !isPackagePath(subPath);
64
+ }
65
+ isSame(pkg) {
66
+ return this.root === pkg.root && this.version === pkg.version && this.name === pkg.name;
67
+ }
68
+ getSize() {
69
+ return this._modules.reduce(
70
+ (ans, item) => {
71
+ const size = item.getSize();
72
+ ans.sourceSize += size.sourceSize;
73
+ ans.transformedSize += size.transformedSize;
74
+ ans.parsedSize += size.parsedSize;
75
+ return ans;
76
+ },
77
+ {
78
+ sourceSize: 0,
79
+ transformedSize: 0,
80
+ parsedSize: 0
81
+ }
82
+ );
83
+ }
84
+ toData() {
85
+ return {
86
+ id: this.id,
87
+ name: this.name,
88
+ root: this.root,
89
+ version: this.version,
90
+ modules: this.getModules().map((e) => e.id),
91
+ size: this.getSize()
92
+ };
93
+ }
94
+ }
95
+ export {
96
+ Package
97
+ };
File without changes
@@ -0,0 +1,6 @@
1
+ function isPackagePath(path) {
2
+ return /(^|[/\\])node_modules[/\\]/.test(path);
3
+ }
4
+ export {
5
+ isPackagePath
6
+ };
@@ -0,0 +1 @@
1
+ export * from "./graph";
@@ -0,0 +1,10 @@
1
+ import { SDK } from '@rsdoctor/types';
2
+ import { Chunk } from './chunk';
3
+ export declare class Asset implements SDK.AssetInstance {
4
+ path: string;
5
+ size: number;
6
+ content: string;
7
+ chunks: Chunk[];
8
+ constructor(path: string, size: number, chunks: Chunk[], content: string);
9
+ toData(types: SDK.ToDataType): SDK.AssetData;
10
+ }
@@ -0,0 +1,30 @@
1
+ import { SDK } from '@rsdoctor/types';
2
+ import { Module } from '../module-graph';
3
+ import { Asset } from './asset';
4
+ export declare class Chunk implements SDK.ChunkInstance {
5
+ readonly id: string;
6
+ readonly name: string;
7
+ readonly size: number;
8
+ readonly initial: boolean;
9
+ readonly entry: boolean;
10
+ private _assets;
11
+ private _modules;
12
+ private _dependencies;
13
+ private _imported;
14
+ private _parsedSize;
15
+ constructor(id: string, name: string, size: number, initial: boolean, entry: boolean);
16
+ isEntry(): boolean;
17
+ isChunkEntryModule(module: Module): boolean;
18
+ hasModule(module: Module): boolean;
19
+ addModule(module: Module): void;
20
+ addAsset(asset: Asset): void;
21
+ addModules(modules: Module[]): void;
22
+ addDependency(dep: Chunk): void;
23
+ addImported(imported: Chunk): void;
24
+ getAssets(): Asset[];
25
+ getModules(): Module[];
26
+ getDependencies(): Chunk[];
27
+ getImported(): Chunk[];
28
+ setParsedSize(parsedSize: number): void;
29
+ toData(): SDK.ChunkData;
30
+ }
@@ -0,0 +1,12 @@
1
+ import { SDK } from '@rsdoctor/types';
2
+ import type { Asset } from './asset';
3
+ import type { Chunk } from './chunk';
4
+ export declare class EntryPoint implements SDK.EntryPointInstance {
5
+ readonly name: string;
6
+ constructor(name: string);
7
+ private _chunks;
8
+ private _assets;
9
+ addChunk(chunk: Chunk): void;
10
+ addAsset(asset: Asset): void;
11
+ toData(): SDK.EntryPointData;
12
+ }
@@ -0,0 +1,22 @@
1
+ import { SDK } from '@rsdoctor/types';
2
+ import type { Asset } from './asset';
3
+ import type { Chunk } from './chunk';
4
+ import type { Module } from '../module-graph';
5
+ import type { EntryPoint } from './entrypoint';
6
+ export declare class ChunkGraph implements SDK.ChunkGraphInstance {
7
+ private _assets;
8
+ private _chunks;
9
+ private _entrypoints;
10
+ getAssets(): Asset[];
11
+ getChunks(): Chunk[];
12
+ addAsset(...assets: Asset[]): void;
13
+ addChunk(...chunks: Chunk[]): void;
14
+ getChunkById(id: string): Chunk | undefined;
15
+ getChunkByModule(module: Module): Chunk | undefined;
16
+ getAssetByPath(path: string): Asset | undefined;
17
+ getAssetsByChunk(chunk: Chunk): Asset[] | undefined;
18
+ getEntryPoints(): EntryPoint[];
19
+ addEntryPoint(...entrypoints: EntryPoint[]): void;
20
+ /** output the chunk graph data */
21
+ toData(type: SDK.ToDataType): SDK.ChunkGraphData;
22
+ }