@kezh/my-custom-js-converter 0.1.62 → 0.2.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/core/context/map_context.js +35 -21
- package/core/util/ast.d.ts +19 -4
- package/core/util/ast.js +66 -9
- package/package.json +1 -1
|
@@ -83,7 +83,8 @@ class MapContext {
|
|
|
83
83
|
}
|
|
84
84
|
const requireModPathByName = (0, ast_1.getRequireModPaths)(path_1.default.dirname(inPath), srcAst);
|
|
85
85
|
const requireModNames = [...requireModPathByName.keys()].map((name) => name);
|
|
86
|
-
|
|
86
|
+
!root && requireModNames.unshift(entry);
|
|
87
|
+
const requireMethodNamesByMod = (0, ast_1.getRequireMethodNames)(srcAst, entry, requireModNames);
|
|
87
88
|
const dependencies = new map_config_1.Dependencies();
|
|
88
89
|
requireModNames.forEach((name) => {
|
|
89
90
|
const dep = new map_config_1.Dependency(requireMethodNamesByMod.get(name));
|
|
@@ -99,37 +100,50 @@ class MapContext {
|
|
|
99
100
|
* 过滤self的dependencies中无效方法
|
|
100
101
|
*/
|
|
101
102
|
filtering() {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const dependencies = selfMod.dependencies;
|
|
103
|
+
this.getModNames().forEach((modName) => {
|
|
104
|
+
const mod = this.getMod(modName);
|
|
105
|
+
const dependencies = mod.dependencies;
|
|
106
106
|
Object.keys(dependencies).forEach((depName) => {
|
|
107
107
|
const srcAst = (0, ast_1.parseSrcAst)(this.getSrcPathByMod(depName));
|
|
108
108
|
const methodNames = (0, ast_1.getObjectMethodNames)((0, ast_1.getObjectMethodsByEntryAndMethodNames)(srcAst, depName, dependencies[depName].methods));
|
|
109
109
|
dependencies[depName].methods = methodNames;
|
|
110
110
|
});
|
|
111
|
-
}
|
|
111
|
+
});
|
|
112
112
|
}
|
|
113
113
|
/**
|
|
114
114
|
* 去除dependencies中未使用方法
|
|
115
115
|
*/
|
|
116
116
|
shaking() {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
117
|
+
const astMap = new Map();
|
|
118
|
+
this.getModNames().forEach((mod) => {
|
|
119
|
+
astMap.set(mod, (0, ast_1.parseSrcAst)(this.getMod(mod).src));
|
|
120
|
+
});
|
|
121
|
+
const useMethodNameMap = new Map();
|
|
122
|
+
this.getModNames().forEach((mod) => {
|
|
123
|
+
useMethodNameMap.set(mod, this.getMethodNamesByMod(mod));
|
|
124
|
+
});
|
|
125
|
+
const dependencies = this.getMod("self").dependencies;
|
|
126
|
+
const filterMethodMap = new Map();
|
|
127
|
+
const readyStack = Object.keys(dependencies).flatMap((name) => dependencies[name].methods.map((method) => [name, method]));
|
|
128
|
+
while (readyStack.length) {
|
|
129
|
+
const method = readyStack.shift();
|
|
130
|
+
let filterMethods = new Set();
|
|
131
|
+
if (filterMethodMap.has(method[0])) {
|
|
132
|
+
filterMethods = filterMethodMap.get(method[0]);
|
|
125
133
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
134
|
+
filterMethods.add(method[1]);
|
|
135
|
+
filterMethodMap.set(method[0], filterMethods);
|
|
136
|
+
const inlineMethods = (0, ast_1.getInlineMethodsByMethodName)(astMap.get(method[0]), method[0], method[1], useMethodNameMap);
|
|
137
|
+
readyStack.push(...inlineMethods.map((m) => m.split("#")));
|
|
138
|
+
}
|
|
139
|
+
[...filterMethodMap.keys()].forEach((fMod) => {
|
|
140
|
+
const filterMethods = filterMethodMap.get(fMod);
|
|
141
|
+
this.getModNames().forEach((modName) => {
|
|
142
|
+
var _a;
|
|
143
|
+
const dep = (_a = this.getMod(modName)) === null || _a === void 0 ? void 0 : _a.dependencies[fMod];
|
|
144
|
+
if (dep) {
|
|
145
|
+
dep.methods = dep.methods.filter((m) => filterMethods.has(m));
|
|
146
|
+
}
|
|
133
147
|
});
|
|
134
148
|
});
|
|
135
149
|
}
|
package/core/util/ast.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Node } from "@babel/core";
|
|
|
2
2
|
import { ObjectMethod } from "@babel/types";
|
|
3
3
|
import { Mod } from "../config/map_config";
|
|
4
4
|
import { MapContext } from "../context/map_context";
|
|
5
|
-
declare type AstType = Node | Node[] | null | undefined;
|
|
5
|
+
export declare type AstType = Node | Node[] | null | undefined;
|
|
6
6
|
export declare function parseSrcAst(srcPath?: string): AstType;
|
|
7
7
|
export declare function outputObjectMethods(outPath: string, methods: ObjectMethod[]): void;
|
|
8
8
|
/**
|
|
@@ -15,12 +15,13 @@ export declare function outputObjectMethods(outPath: string, methods: ObjectMeth
|
|
|
15
15
|
*/
|
|
16
16
|
export declare function getRequireModPaths(baseDir: string, srcAst: AstType, ignoreMod?: string[]): Map<string, string>;
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
18
|
+
* 获取源码方法中引用的第三方模块方法名和自身模块方法名
|
|
19
19
|
* @param mod - 依赖模块
|
|
20
|
+
* @param entry - 入口对象
|
|
20
21
|
* @param srcAst - 源码AST
|
|
21
22
|
* @returns
|
|
22
23
|
*/
|
|
23
|
-
export declare function getRequireMethodNames(srcAst: AstType, mod: string[]): Map<string, string[]>;
|
|
24
|
+
export declare function getRequireMethodNames(srcAst: AstType, entry: string, mod: string[]): Map<string, string[]>;
|
|
24
25
|
/**
|
|
25
26
|
* 获取方法名
|
|
26
27
|
* @param methods
|
|
@@ -51,6 +52,13 @@ export declare function getObjectMehtodsByMehtodNamesAndInsideOwnMethods(srcAst:
|
|
|
51
52
|
* @param root
|
|
52
53
|
*/
|
|
53
54
|
export declare function modifyObjectMethods(srcAst: AstType, entry: string, mod: Mod, methods: ObjectMethod[], root?: boolean): void;
|
|
55
|
+
/**
|
|
56
|
+
* 获取调用方法中所依赖第三方模块方法名
|
|
57
|
+
* @param srcAst
|
|
58
|
+
* @param methodNames - 调用方法名
|
|
59
|
+
* @param depNames - 依赖模块名
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
54
62
|
export declare function getDependentMethodNames(srcAst: AstType, methodNames: string[], depNames: string[]): Map<string, string[]>;
|
|
55
63
|
/**
|
|
56
64
|
* 生成导入语句
|
|
@@ -65,4 +73,11 @@ export declare function importMods(outDir: string, srcAst: AstType, mapContext:
|
|
|
65
73
|
* @param srcAst - 源码AST
|
|
66
74
|
*/
|
|
67
75
|
export declare function deleteModMethods(srcAst: AstType, mapContext: MapContext): void;
|
|
68
|
-
|
|
76
|
+
/**
|
|
77
|
+
* 获取方法内部调用的方法名
|
|
78
|
+
* @param srcAst - 源码AST
|
|
79
|
+
* @param entry - 入口
|
|
80
|
+
* @param methodName - 方法名
|
|
81
|
+
* @param mapContext
|
|
82
|
+
*/
|
|
83
|
+
export declare function getInlineMethodsByMethodName(srcAst: AstType, entry: string, methodName: string, useMethodNameMap: Map<string, string[]>): string[];
|
package/core/util/ast.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.deleteModMethods = exports.importMods = exports.getDependentMethodNames = exports.modifyObjectMethods = exports.getObjectMehtodsByMehtodNamesAndInsideOwnMethods = exports.getObjectMethodsByEntryAndMethodNames = exports.getObjectMethodNames = exports.getRequireMethodNames = exports.getRequireModPaths = exports.outputObjectMethods = exports.parseSrcAst = void 0;
|
|
6
|
+
exports.getInlineMethodsByMethodName = exports.deleteModMethods = exports.importMods = exports.getDependentMethodNames = exports.modifyObjectMethods = exports.getObjectMehtodsByMehtodNamesAndInsideOwnMethods = exports.getObjectMethodsByEntryAndMethodNames = exports.getObjectMethodNames = exports.getRequireMethodNames = exports.getRequireModPaths = exports.outputObjectMethods = exports.parseSrcAst = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const parser_1 = require("@babel/parser");
|
|
9
9
|
const traverse_1 = __importDefault(require("@babel/traverse"));
|
|
@@ -81,25 +81,32 @@ function getRequireModPaths(baseDir, srcAst, ignoreMod) {
|
|
|
81
81
|
}
|
|
82
82
|
exports.getRequireModPaths = getRequireModPaths;
|
|
83
83
|
/**
|
|
84
|
-
*
|
|
84
|
+
* 获取源码方法中引用的第三方模块方法名和自身模块方法名
|
|
85
85
|
* @param mod - 依赖模块
|
|
86
|
+
* @param entry - 入口对象
|
|
86
87
|
* @param srcAst - 源码AST
|
|
87
88
|
* @returns
|
|
88
89
|
*/
|
|
89
|
-
function getRequireMethodNames(srcAst, mod) {
|
|
90
|
+
function getRequireMethodNames(srcAst, entry, mod) {
|
|
90
91
|
const methodsByMod = new Map();
|
|
91
92
|
(0, traverse_1.default)(srcAst, {
|
|
92
93
|
MemberExpression(path) {
|
|
93
|
-
const invokeObj = path.node.object;
|
|
94
|
-
if (!core_1.types.isIdentifier(invokeObj)) {
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
94
|
const invokeMethod = path.node.property;
|
|
98
95
|
if (!core_1.types.isIdentifier(invokeMethod)) {
|
|
99
96
|
return;
|
|
100
97
|
}
|
|
101
|
-
const
|
|
102
|
-
|
|
98
|
+
const invokeObj = path.node.object;
|
|
99
|
+
let key = null;
|
|
100
|
+
if (core_1.types.isIdentifier(invokeObj)) {
|
|
101
|
+
key = invokeObj.name;
|
|
102
|
+
if (!mod.includes(key)) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (core_1.types.isThisExpression(invokeObj)) {
|
|
107
|
+
key = entry;
|
|
108
|
+
}
|
|
109
|
+
if (!key) {
|
|
103
110
|
return;
|
|
104
111
|
}
|
|
105
112
|
let methods = [];
|
|
@@ -276,6 +283,13 @@ function modifyObjectMethods(srcAst, entry, mod, methods, root = false) {
|
|
|
276
283
|
});
|
|
277
284
|
}
|
|
278
285
|
exports.modifyObjectMethods = modifyObjectMethods;
|
|
286
|
+
/**
|
|
287
|
+
* 获取调用方法中所依赖第三方模块方法名
|
|
288
|
+
* @param srcAst
|
|
289
|
+
* @param methodNames - 调用方法名
|
|
290
|
+
* @param depNames - 依赖模块名
|
|
291
|
+
* @returns
|
|
292
|
+
*/
|
|
279
293
|
function getDependentMethodNames(srcAst, methodNames, depNames) {
|
|
280
294
|
const methodNamesByDepName = new Map();
|
|
281
295
|
(0, traverse_1.default)(srcAst, {
|
|
@@ -373,3 +387,46 @@ function deleteModMethods(srcAst, mapContext) {
|
|
|
373
387
|
});
|
|
374
388
|
}
|
|
375
389
|
exports.deleteModMethods = deleteModMethods;
|
|
390
|
+
/**
|
|
391
|
+
* 获取方法内部调用的方法名
|
|
392
|
+
* @param srcAst - 源码AST
|
|
393
|
+
* @param entry - 入口
|
|
394
|
+
* @param methodName - 方法名
|
|
395
|
+
* @param mapContext
|
|
396
|
+
*/
|
|
397
|
+
function getInlineMethodsByMethodName(srcAst, entry, methodName, useMethodNameMap) {
|
|
398
|
+
const inlineMethods = new Set();
|
|
399
|
+
const selfMethods = useMethodNameMap.get(entry);
|
|
400
|
+
const mods = [...useMethodNameMap.keys()];
|
|
401
|
+
(0, traverse_1.default)(srcAst, {
|
|
402
|
+
MemberExpression(path) {
|
|
403
|
+
const objectMethodPath = path.findParent((path) => core_1.types.isObjectMethod(path));
|
|
404
|
+
if (!objectMethodPath) {
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (!core_1.types.isIdentifier(objectMethodPath.node.key)) {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
if (objectMethodPath.node.key.name !== methodName) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
if (core_1.types.isThisExpression(path.node.object) &&
|
|
414
|
+
core_1.types.isIdentifier(path.node.property)) {
|
|
415
|
+
if (selfMethods.includes(path.node.property.name)) {
|
|
416
|
+
inlineMethods.add(`${entry}#${path.node.property.name}`);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (core_1.types.isIdentifier(path.node.object) &&
|
|
420
|
+
core_1.types.isIdentifier(path.node.property)) {
|
|
421
|
+
const modName = path.node.object.name;
|
|
422
|
+
const methodName = path.node.property.name;
|
|
423
|
+
if (mods.includes(modName) &&
|
|
424
|
+
useMethodNameMap.get(modName).includes(methodName)) {
|
|
425
|
+
inlineMethods.add(`${modName}#${methodName}`);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
return [...inlineMethods];
|
|
431
|
+
}
|
|
432
|
+
exports.getInlineMethodsByMethodName = getInlineMethodsByMethodName;
|