@kezh/my-custom-js-converter 0.1.44 → 0.1.52

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.
@@ -10,8 +10,11 @@ export declare class MapContext {
10
10
  getModNames(): string[];
11
11
  getMod(name: string): Mod | null | undefined;
12
12
  getMethodNamesByMod(name: string): string[];
13
+ getDependencyNameByMod(name: string): string[];
14
+ getSrcPathByMod(name: string): string | undefined;
13
15
  static readFromLocal(inDir: string): MapContext;
14
16
  private init;
15
17
  private writeToLocal;
16
18
  private buildMapContext;
19
+ private shaking;
17
20
  }
@@ -35,6 +35,18 @@ class MapContext {
35
35
  .forEach((methods) => methodNames.add(methods));
36
36
  return [...methodNames];
37
37
  }
38
+ getDependencyNameByMod(name) {
39
+ const mod = this.getMod(name);
40
+ const deps = mod === null || mod === void 0 ? void 0 : mod.dependencies;
41
+ if (deps) {
42
+ return Object.keys(deps);
43
+ }
44
+ return [];
45
+ }
46
+ getSrcPathByMod(name) {
47
+ var _a;
48
+ return (_a = this.getMod(name)) === null || _a === void 0 ? void 0 : _a.src;
49
+ }
38
50
  static readFromLocal(inDir) {
39
51
  const inPath = path_1.default.join(inDir, MapContext.SAVE_FILE);
40
52
  console.log(`input map => ${inPath}`);
@@ -46,6 +58,7 @@ class MapContext {
46
58
  }
47
59
  init() {
48
60
  this.buildMapContext(this.srcPath, this.entry, true);
61
+ this.shaking();
49
62
  this.writeToLocal();
50
63
  }
51
64
  writeToLocal() {
@@ -81,6 +94,26 @@ class MapContext {
81
94
  this.buildMapContext(modPath, name, false);
82
95
  });
83
96
  }
97
+ shaking() {
98
+ this.getModNames().forEach((modName) => {
99
+ const depNames = this.getDependencyNameByMod(modName);
100
+ if (!depNames.length) {
101
+ return;
102
+ }
103
+ const methodNames = this.getMethodNamesByMod(modName);
104
+ if (!methodNames.length) {
105
+ return;
106
+ }
107
+ const srcAst = (0, ast_1.parseSrcAst)(this.getSrcPathByMod(modName));
108
+ const methodNamesByDepName = (0, ast_1.getDependentMethodNames)(srcAst, methodNames, depNames);
109
+ const mod = this.getMod(modName);
110
+ depNames.forEach((depName) => {
111
+ mod.dependencies[depName] = new map_config_1.Dependency(methodNamesByDepName.has(depName)
112
+ ? methodNamesByDepName.get(depName)
113
+ : []);
114
+ });
115
+ });
116
+ }
84
117
  }
85
118
  exports.MapContext = MapContext;
86
119
  MapContext.SAVE_FILE = "mod.map";
@@ -43,6 +43,14 @@ export declare function getObjectMethodsByEntryAndMethodNames(srcAst: AstType, e
43
43
  */
44
44
  export declare function getObjectMehtodsByMehtodNamesAndInsideOwnMethods(srcAst: AstType, entry: string, methodNames: string[]): ObjectMethod[];
45
45
  export declare function modifyObjectMethods(srcAst: AstType, entry: string, mod: Mod, methods: ObjectMethod[], root?: boolean): void;
46
+ export declare function getDependentMethodNames(srcAst: AstType, methodNames: string[], depNames: string[]): Map<string, string[]>;
47
+ /**
48
+ * 生成导入语句
49
+ * const mod = require($modPath);
50
+ * @param outDir
51
+ * @param srcAst
52
+ * @param mapContext
53
+ */
46
54
  export declare function importMods(outDir: string, srcAst: AstType, mapContext: MapContext): void;
47
55
  /**
48
56
  * 去除第三方模块方法
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.modifyObjectMethods = exports.getObjectMehtodsByMehtodNamesAndInsideOwnMethods = exports.getObjectMethodsByEntryAndMethodNames = exports.getObjectMethodNames = exports.getRequireMethodNames = exports.getRequireModPaths = exports.outputObjectMethods = exports.parseSrcAst = void 0;
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;
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"));
@@ -261,13 +261,46 @@ function modifyObjectMethods(srcAst, entry, mod, methods, root = false) {
261
261
  });
262
262
  }
263
263
  exports.modifyObjectMethods = modifyObjectMethods;
264
+ function getDependentMethodNames(srcAst, methodNames, depNames) {
265
+ const methodNamesByDepName = new Map();
266
+ (0, traverse_1.default)(srcAst, {
267
+ MemberExpression(path) {
268
+ var _a;
269
+ const objectMethodNode = path.findParent((path) => core_1.types.isObjectMethod(path));
270
+ if (!core_1.types.isIdentifier(objectMethodNode === null || objectMethodNode === void 0 ? void 0 : objectMethodNode.node.key)) {
271
+ return;
272
+ }
273
+ if (!methodNames.includes(objectMethodNode.node.key.name)) {
274
+ return;
275
+ }
276
+ if (!(core_1.types.isIdentifier(path.node.object) &&
277
+ core_1.types.isIdentifier(path.node.property))) {
278
+ return;
279
+ }
280
+ if (depNames.includes(path.node.object.name)) {
281
+ const depName = path.node.object.name;
282
+ if (!methodNamesByDepName.has(depName)) {
283
+ methodNamesByDepName.set(depName, []);
284
+ }
285
+ (_a = methodNamesByDepName.get(depName)) === null || _a === void 0 ? void 0 : _a.push(path.node.property.name);
286
+ }
287
+ }
288
+ });
289
+ return methodNamesByDepName;
290
+ }
291
+ exports.getDependentMethodNames = getDependentMethodNames;
292
+ /**
293
+ * 生成导入语句
294
+ * const mod = require($modPath);
295
+ * @param outDir
296
+ * @param srcAst
297
+ * @param mapContext
298
+ */
264
299
  function importMods(outDir, srcAst, mapContext) {
265
- const importMods = mapContext
266
- .getModNames()
267
- .filter((name) => name !== "self")
268
- .map((name) => {
269
- const mod = mapContext.getMod(name);
270
- const requireFrom = (0, path_1.relative)(outDir, mod.src.replace(/index\.js$/, "")).replace(/[\\]+/g, "/");
300
+ const depNames = mapContext.getDependencyNameByMod("self");
301
+ const importMods = depNames.map(name => {
302
+ const srcPath = mapContext.getSrcPathByMod(name);
303
+ const requireFrom = (0, path_1.relative)(outDir, srcPath.replace(/index\.js$/, "")).replace(/[\\]+/g, "/");
271
304
  const variableDeclarator = core_1.types.variableDeclarator(core_1.types.identifier(name), core_1.types.callExpression(core_1.types.identifier("require"), [
272
305
  core_1.types.stringLiteral(requireFrom)
273
306
  ]));
@@ -296,9 +329,9 @@ function deleteModMethods(srcAst, mapContext) {
296
329
  return;
297
330
  }
298
331
  const methodName = path.node.key.name;
299
- const isDepMethod = [...deps].filter((name) => methodName.lastIndexOf(`__${name}`) !== -1)
300
- .length > 0;
301
- isDepMethod && path.remove();
332
+ const split = methodName.split("__");
333
+ const depName = split.length ? split[split.length - 1] : "";
334
+ deps.has(depName) && path.remove();
302
335
  }
303
336
  });
304
337
  (0, traverse_1.default)(srcAst, {
@@ -307,11 +340,9 @@ function deleteModMethods(srcAst, mapContext) {
307
340
  return;
308
341
  }
309
342
  const methodName = path.node.property.name;
310
- const depNames = [...deps]
311
- .filter((name) => methodName.lastIndexOf(`__${name}`) !== -1)
312
- .map((name) => name);
313
- if (depNames.length) {
314
- const depName = depNames[0];
343
+ const split = methodName.split("__");
344
+ const depName = split.length ? split[split.length - 1] : "";
345
+ if (deps.has(depName)) {
315
346
  path.node.property.name = methodName.replace(`__${depName}`, "");
316
347
  if (core_1.types.isThisExpression(path.node.object)) {
317
348
  path.node.object = core_1.types.identifier(depName);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kezh/my-custom-js-converter",
3
- "version": "0.1.44",
3
+ "version": "0.1.52",
4
4
  "description": "自定义代码转换器",
5
5
  "main": "index.js",
6
6
  "bin": {