@kezh/my-custom-js-converter 1.0.1 → 1.1.2
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
|
|
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.
|
|
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.
|
|
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) => {
|
package/core/decode/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const path_1 = __importDefault(require("path"));
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const os_1 = require("os");
|
|
8
9
|
const prettier_1 = __importDefault(require("prettier"));
|
|
9
10
|
const config_1 = require("../config");
|
|
10
11
|
const scanf_1 = require("../scanf");
|
|
@@ -36,7 +37,7 @@ function decoding$0(inPath, outPath, config) {
|
|
|
36
37
|
let src = fs_1.default.readFileSync(inPath, {
|
|
37
38
|
encoding: "utf-8"
|
|
38
39
|
});
|
|
39
|
-
src = `const ${config.decode.mount} = {${src}}
|
|
40
|
+
src = `const ${config.decode.mount} = {${src}};${os_1.EOL}module.exports = ${config.decode.mount};`;
|
|
40
41
|
const srcAst = (0, parser_1.parse)(src);
|
|
41
42
|
if (!core_1.types.isNode(srcAst)) {
|
|
42
43
|
return;
|
|
@@ -44,7 +45,7 @@ function decoding$0(inPath, outPath, config) {
|
|
|
44
45
|
(0, ast_1.importMods)(path_1.default.dirname(outPath), srcAst, mapContext);
|
|
45
46
|
(0, ast_1.deleteModMethods)(srcAst, mapContext);
|
|
46
47
|
let { code: dist } = (0, generator_1.default)(srcAst, { compact: true });
|
|
47
|
-
dist = dist.replace(/},\n\n/g,
|
|
48
|
+
dist = dist.replace(/},\n\n/g, `},${os_1.EOL}`);
|
|
48
49
|
dist = prettier_1.default.format(dist, {
|
|
49
50
|
parser: "babel",
|
|
50
51
|
trailingComma: "none"
|
package/core/encode/index.js
CHANGED
|
@@ -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
|
|
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
|
@@ -11,6 +11,7 @@ const generator_1 = __importDefault(require("@babel/generator"));
|
|
|
11
11
|
const core_1 = require("@babel/core");
|
|
12
12
|
const scanf_1 = require("../scanf");
|
|
13
13
|
const path_1 = require("path");
|
|
14
|
+
const os_1 = require("os");
|
|
14
15
|
function parseSrcAst(srcPath) {
|
|
15
16
|
if (!srcPath) {
|
|
16
17
|
return;
|
|
@@ -32,7 +33,7 @@ function outputObjectMethods(outPath, methods) {
|
|
|
32
33
|
})
|
|
33
34
|
.map((m) => (0, generator_1.default)(m))
|
|
34
35
|
.map((m) => m.code)
|
|
35
|
-
.join(
|
|
36
|
+
.join(`,${os_1.EOL}`);
|
|
36
37
|
fs_1.default.writeFileSync(outPath, dist, "utf-8");
|
|
37
38
|
}
|
|
38
39
|
exports.outputObjectMethods = outputObjectMethods;
|
|
@@ -327,7 +328,7 @@ exports.getDependentMethodNames = getDependentMethodNames;
|
|
|
327
328
|
function importMods(outDir, srcAst, mapContext) {
|
|
328
329
|
const depNames = mapContext.getDependencyNameByMod("self");
|
|
329
330
|
const importMods = depNames.map((name) => {
|
|
330
|
-
const srcPath = mapContext.
|
|
331
|
+
const srcPath = mapContext.getAbsoluteSrcPathByMod(name);
|
|
331
332
|
const requireFrom = (0, path_1.relative)(outDir, srcPath.replace(/(index)?\.js$/, "")).replace(/[\\]+/g, "/");
|
|
332
333
|
const variableDeclarator = core_1.types.variableDeclarator(core_1.types.identifier(name), core_1.types.callExpression(core_1.types.identifier("require"), [
|
|
333
334
|
core_1.types.stringLiteral(requireFrom)
|
|
@@ -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;
|