@kezh/my-custom-js-converter 1.0.1 → 1.1.1

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.
@@ -12,6 +12,7 @@ export declare class MapContext {
12
12
  getMethodNamesByMod(name: string): string[];
13
13
  getDependencyNameByMod(name: string): string[];
14
14
  getSrcPathByMod(name: string): string | undefined;
15
+ getAbsoluteSrcPathByMod(name: string): string | undefined;
15
16
  static readFromLocal(inDir: string): MapContext;
16
17
  private init;
17
18
  private writeToLocal;
@@ -8,6 +8,7 @@ const map_config_1 = require("../config/map_config");
8
8
  const fs_1 = __importDefault(require("fs"));
9
9
  const path_1 = __importDefault(require("path"));
10
10
  const ast_1 = require("../util/ast");
11
+ const parent_path_1 = require("../util/parent_path");
11
12
  class MapContext {
12
13
  constructor(srcPath = "", entry = "", outDir = "", initMap = false) {
13
14
  this.map = new map_config_1.MapConfig();
@@ -47,6 +48,13 @@ class MapContext {
47
48
  var _a;
48
49
  return (_a = this.getMod(name)) === null || _a === void 0 ? void 0 : _a.src;
49
50
  }
51
+ getAbsoluteSrcPathByMod(name) {
52
+ const srcPath = this.getSrcPathByMod(name);
53
+ const parentRootDir = (0, parent_path_1.getParentRootDir)();
54
+ return parentRootDir && srcPath
55
+ ? path_1.default.resolve(parentRootDir, srcPath)
56
+ : srcPath;
57
+ }
50
58
  static readFromLocal(inDir) {
51
59
  const inPath = path_1.default.join(inDir, MapContext.SAVE_FILE);
52
60
  console.log(`input map => ${inPath}`);
@@ -59,7 +67,7 @@ class MapContext {
59
67
  }
60
68
  catch (err) {
61
69
  console.log("the map is not found!");
62
- console.log(err);
70
+ // console.log(err);
63
71
  }
64
72
  return new MapContext();
65
73
  }
@@ -98,7 +106,8 @@ class MapContext {
98
106
  const dep = new map_config_1.Dependency(requireMethodNamesByMod.get(name));
99
107
  dependencies[name] = dep;
100
108
  });
101
- const mod = new map_config_1.Mod(inPath, dependencies);
109
+ const parentRootDir = (0, parent_path_1.getParentRootDir)();
110
+ const mod = new map_config_1.Mod(parentRootDir ? path_1.default.relative(parentRootDir, inPath) : inPath, dependencies);
102
111
  this.appendModMap(mod, root, entry);
103
112
  [...requireModPathByName.entries()].forEach(([name, modPath]) => {
104
113
  this.buildMapContext(modPath, name, false);
@@ -112,7 +121,7 @@ class MapContext {
112
121
  const mod = this.getMod(modName);
113
122
  const dependencies = mod.dependencies;
114
123
  Object.keys(dependencies).forEach((depName) => {
115
- const srcAst = (0, ast_1.parseSrcAst)(this.getSrcPathByMod(depName));
124
+ const srcAst = (0, ast_1.parseSrcAst)(this.getAbsoluteSrcPathByMod(depName));
116
125
  const methodNames = (0, ast_1.getObjectMethodNames)((0, ast_1.getObjectMethodsByEntryAndMethodNames)(srcAst, depName, dependencies[depName].methods));
117
126
  dependencies[depName].methods = methodNames;
118
127
  });
@@ -124,7 +133,7 @@ class MapContext {
124
133
  shaking() {
125
134
  const astMap = new Map();
126
135
  this.getModNames().forEach((mod) => {
127
- astMap.set(mod, (0, ast_1.parseSrcAst)(this.getMod(mod).src));
136
+ astMap.set(mod, (0, ast_1.parseSrcAst)(this.getAbsoluteSrcPathByMod(mod)));
128
137
  });
129
138
  const useMethodNameMap = new Map();
130
139
  this.getModNames().forEach((mod) => {
@@ -27,8 +27,7 @@ const ast_1 = require("../util/ast");
27
27
  function encoding$0(mapContext, entry) {
28
28
  const modNames = mapContext.getModNames();
29
29
  return modNames.flatMap((name) => {
30
- const mod = mapContext.getMod(name);
31
- const srcAst = (0, ast_1.parseSrcAst)(mod === null || mod === void 0 ? void 0 : mod.src);
30
+ const srcAst = (0, ast_1.parseSrcAst)(mapContext.getAbsoluteSrcPathByMod(name));
32
31
  // console.log(`import => ${name}`);
33
32
  const srcMethods = [];
34
33
  const self = name === "self";
package/core/util/ast.js CHANGED
@@ -327,7 +327,7 @@ exports.getDependentMethodNames = getDependentMethodNames;
327
327
  function importMods(outDir, srcAst, mapContext) {
328
328
  const depNames = mapContext.getDependencyNameByMod("self");
329
329
  const importMods = depNames.map((name) => {
330
- const srcPath = mapContext.getSrcPathByMod(name);
330
+ const srcPath = mapContext.getAbsoluteSrcPathByMod(name);
331
331
  const requireFrom = (0, path_1.relative)(outDir, srcPath.replace(/(index)?\.js$/, "")).replace(/[\\]+/g, "/");
332
332
  const variableDeclarator = core_1.types.variableDeclarator(core_1.types.identifier(name), core_1.types.callExpression(core_1.types.identifier("require"), [
333
333
  core_1.types.stringLiteral(requireFrom)
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 获取父项目根路径
3
+ * @returns 若被父项目依赖则返回父项目根目录,否则返回undefined
4
+ */
5
+ export declare function getParentRootDir(): string | undefined;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getParentRootDir = void 0;
4
+ /**
5
+ * 获取父项目根路径
6
+ * @returns 若被父项目依赖则返回父项目根目录,否则返回undefined
7
+ */
8
+ function getParentRootDir() {
9
+ const curDir = __dirname;
10
+ if (curDir.search(/node_modules.*/) === -1) {
11
+ return undefined;
12
+ }
13
+ return curDir.replace(/node_modules.*/g, "");
14
+ }
15
+ exports.getParentRootDir = getParentRootDir;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kezh/my-custom-js-converter",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "自定义代码转换器",
5
5
  "main": "index.js",
6
6
  "bin": {