@kezh/my-custom-js-converter 0.1.58 → 0.1.62

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.
@@ -16,5 +16,12 @@ export declare class MapContext {
16
16
  private init;
17
17
  private writeToLocal;
18
18
  private buildMapContext;
19
+ /**
20
+ * 过滤self的dependencies中无效方法
21
+ */
22
+ private filtering;
23
+ /**
24
+ * 去除dependencies中未使用方法
25
+ */
19
26
  private shaking;
20
27
  }
@@ -58,6 +58,7 @@ class MapContext {
58
58
  }
59
59
  init() {
60
60
  this.buildMapContext(this.srcPath, this.entry, true);
61
+ this.filtering();
61
62
  this.shaking();
62
63
  this.writeToLocal();
63
64
  }
@@ -94,6 +95,24 @@ class MapContext {
94
95
  this.buildMapContext(modPath, name, false);
95
96
  });
96
97
  }
98
+ /**
99
+ * 过滤self的dependencies中无效方法
100
+ */
101
+ filtering() {
102
+ const self = "self";
103
+ const selfMod = this.getMod(self);
104
+ if (selfMod) {
105
+ const dependencies = selfMod.dependencies;
106
+ Object.keys(dependencies).forEach((depName) => {
107
+ const srcAst = (0, ast_1.parseSrcAst)(this.getSrcPathByMod(depName));
108
+ const methodNames = (0, ast_1.getObjectMethodNames)((0, ast_1.getObjectMethodsByEntryAndMethodNames)(srcAst, depName, dependencies[depName].methods));
109
+ dependencies[depName].methods = methodNames;
110
+ });
111
+ }
112
+ }
113
+ /**
114
+ * 去除dependencies中未使用方法
115
+ */
97
116
  shaking() {
98
117
  this.getModNames().forEach((modName) => {
99
118
  const depNames = this.getDependencyNameByMod(modName);
@@ -42,6 +42,14 @@ export declare function getObjectMethodsByEntryAndMethodNames(srcAst: AstType, e
42
42
  * @param methods
43
43
  */
44
44
  export declare function getObjectMehtodsByMehtodNamesAndInsideOwnMethods(srcAst: AstType, entry: string, methodNames: string[]): ObjectMethod[];
45
+ /**
46
+ * 修改依赖方法调用者
47
+ * @param srcAst
48
+ * @param entry
49
+ * @param mod
50
+ * @param methods
51
+ * @param root
52
+ */
45
53
  export declare function modifyObjectMethods(srcAst: AstType, entry: string, mod: Mod, methods: ObjectMethod[], root?: boolean): void;
46
54
  export declare function getDependentMethodNames(srcAst: AstType, methodNames: string[], depNames: string[]): Map<string, string[]>;
47
55
  /**
package/core/util/ast.js CHANGED
@@ -48,9 +48,12 @@ function getRequireModPaths(baseDir, srcAst, ignoreMod) {
48
48
  const modPathByName = new Map();
49
49
  (0, traverse_1.default)(srcAst, {
50
50
  Program(path) {
51
- path.node.body.filter(item => core_1.types.isVariableDeclaration(item)).flatMap(item => item.declarations)
52
- .filter(item => {
53
- if (core_1.types.isCallExpression(item.init) && core_1.types.isIdentifier(item.init.callee)) {
51
+ path.node.body
52
+ .filter((item) => core_1.types.isVariableDeclaration(item))
53
+ .flatMap((item) => item.declarations)
54
+ .filter((item) => {
55
+ if (core_1.types.isCallExpression(item.init) &&
56
+ core_1.types.isIdentifier(item.init.callee)) {
54
57
  if (item.init.callee.name === "require") {
55
58
  if (core_1.types.isIdentifier(item.id)) {
56
59
  if (ignoreMod === null || ignoreMod === void 0 ? void 0 : ignoreMod.includes(item.id.name)) {
@@ -63,8 +66,9 @@ function getRequireModPaths(baseDir, srcAst, ignoreMod) {
63
66
  }
64
67
  }
65
68
  return false;
66
- }).forEach(item => {
67
- const args = (item.init.arguments);
69
+ })
70
+ .forEach((item) => {
71
+ const args = item.init.arguments;
68
72
  if (args.length) {
69
73
  const modName = item.id.name;
70
74
  const modPath = args[0].value;
@@ -224,6 +228,14 @@ function getInsideOwnMethodNames(srcAst, entry, methodNames, allMethodNameSet =
224
228
  insideMethodNames.push(...getInsideOwnMethodNames(srcAst, entry, insideMethodNames, allMethodNameSet));
225
229
  return insideMethodNames;
226
230
  }
231
+ /**
232
+ * 修改依赖方法调用者
233
+ * @param srcAst
234
+ * @param entry
235
+ * @param mod
236
+ * @param methods
237
+ * @param root
238
+ */
227
239
  function modifyObjectMethods(srcAst, entry, mod, methods, root = false) {
228
240
  const deps = Object.keys(mod.dependencies);
229
241
  const methodNames = getObjectMethodNames(methods);
@@ -244,8 +256,9 @@ function modifyObjectMethods(srcAst, entry, mod, methods, root = false) {
244
256
  path.node.object = core_1.types.thisExpression();
245
257
  }
246
258
  else if (deps.includes(depName)) {
247
- path.node.object = core_1.types.thisExpression();
248
- if (core_1.types.isIdentifier(path.node.property)) {
259
+ if (core_1.types.isIdentifier(path.node.property) &&
260
+ mod.dependencies[depName].methods.includes(path.node.property.name)) {
261
+ path.node.object = core_1.types.thisExpression();
249
262
  path.node.property.name = `${path.node.property.name}__${depName}`;
250
263
  }
251
264
  }
@@ -300,7 +313,7 @@ exports.getDependentMethodNames = getDependentMethodNames;
300
313
  */
301
314
  function importMods(outDir, srcAst, mapContext) {
302
315
  const depNames = mapContext.getDependencyNameByMod("self");
303
- const importMods = depNames.map(name => {
316
+ const importMods = depNames.map((name) => {
304
317
  const srcPath = mapContext.getSrcPathByMod(name);
305
318
  const requireFrom = (0, path_1.relative)(outDir, srcPath.replace(/(index)?\.js$/, "")).replace(/[\\]+/g, "/");
306
319
  const variableDeclarator = core_1.types.variableDeclarator(core_1.types.identifier(name), core_1.types.callExpression(core_1.types.identifier("require"), [
@@ -332,6 +345,9 @@ function deleteModMethods(srcAst, mapContext) {
332
345
  }
333
346
  const methodName = path.node.key.name;
334
347
  const split = methodName.split("__");
348
+ if (split.length < 2) {
349
+ return;
350
+ }
335
351
  const depName = split.length ? split[split.length - 1] : "";
336
352
  deps.has(depName) && path.remove();
337
353
  }
@@ -343,7 +359,7 @@ function deleteModMethods(srcAst, mapContext) {
343
359
  }
344
360
  const methodName = path.node.property.name;
345
361
  const split = methodName.split("__");
346
- if (split.length > 1) {
362
+ if (split.length < 2) {
347
363
  return;
348
364
  }
349
365
  const depName = split[split.length - 1];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kezh/my-custom-js-converter",
3
- "version": "0.1.58",
3
+ "version": "0.1.62",
4
4
  "description": "自定义代码转换器",
5
5
  "main": "index.js",
6
6
  "bin": {