@kezh/my-custom-js-converter 0.1.52 → 0.1.57
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/decode/index.js +2 -2
- package/core/scanf/index.js +12 -7
- package/core/util/ast.d.ts +1 -1
- package/core/util/ast.js +24 -22
- package/package.json +1 -1
package/core/decode/index.js
CHANGED
|
@@ -36,14 +36,14 @@ function decoding$0(inPath, outPath, config) {
|
|
|
36
36
|
let src = fs_1.default.readFileSync(inPath, {
|
|
37
37
|
encoding: "utf-8"
|
|
38
38
|
});
|
|
39
|
-
src = `const ${config.decode.mount} = {${src}};\r\nmodule.exports =
|
|
39
|
+
src = `const ${config.decode.mount} = {${src}};\r\nmodule.exports = ${config.decode.mount};`;
|
|
40
40
|
const srcAst = (0, parser_1.parse)(src);
|
|
41
41
|
if (!core_1.types.isNode(srcAst)) {
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
44
|
(0, ast_1.importMods)(path_1.default.dirname(outPath), srcAst, mapContext);
|
|
45
45
|
(0, ast_1.deleteModMethods)(srcAst, mapContext);
|
|
46
|
-
let { code: dist } = (0, generator_1.default)(srcAst);
|
|
46
|
+
let { code: dist } = (0, generator_1.default)(srcAst, { compact: true });
|
|
47
47
|
dist = dist.replace(/},\n\n/g, "},\n");
|
|
48
48
|
dist = prettier_1.default.format(dist, {
|
|
49
49
|
parser: "babel",
|
package/core/scanf/index.js
CHANGED
|
@@ -11,7 +11,7 @@ function scanfCodeFiles(dirPaths, filterName = "") {
|
|
|
11
11
|
return scanfCodeFiles$0(dirPaths, filterName);
|
|
12
12
|
}
|
|
13
13
|
if (dirPaths instanceof (Array)) {
|
|
14
|
-
return dirPaths.flatMap(p => scanfCodeFiles$0(p, filterName));
|
|
14
|
+
return dirPaths.flatMap((p) => scanfCodeFiles$0(p, filterName));
|
|
15
15
|
}
|
|
16
16
|
return [];
|
|
17
17
|
}
|
|
@@ -24,7 +24,7 @@ function scanfCodeFiles$0(dirPath, filterName = "") {
|
|
|
24
24
|
}
|
|
25
25
|
const files = fs_1.default.readdirSync(dirPath, {
|
|
26
26
|
encoding: "utf-8",
|
|
27
|
-
withFileTypes: true
|
|
27
|
+
withFileTypes: true
|
|
28
28
|
});
|
|
29
29
|
files
|
|
30
30
|
.filter((file) => file.isFile())
|
|
@@ -56,17 +56,22 @@ exports.scanfCodeDirs = scanfCodeDirs;
|
|
|
56
56
|
function scanfRequieMod(baseDir, modPath) {
|
|
57
57
|
let absolutePath = path_1.default.join(baseDir, modPath);
|
|
58
58
|
try {
|
|
59
|
-
fs_1.default.accessSync(absolutePath, fs_1.default.constants.
|
|
60
|
-
absolutePath = path_1.default.join(absolutePath, 'index.js');
|
|
61
|
-
fs_1.default.accessSync(absolutePath, fs_1.default.constants.F_OK);
|
|
59
|
+
fs_1.default.accessSync(absolutePath, fs_1.default.constants.R_OK);
|
|
62
60
|
}
|
|
63
61
|
catch (err) {
|
|
64
62
|
try {
|
|
65
|
-
absolutePath
|
|
63
|
+
fs_1.default.accessSync(absolutePath, fs_1.default.constants.O_DIRECTORY);
|
|
64
|
+
absolutePath = path_1.default.join(absolutePath, "index.js");
|
|
66
65
|
fs_1.default.accessSync(absolutePath, fs_1.default.constants.F_OK);
|
|
67
66
|
}
|
|
68
67
|
catch (err) {
|
|
69
|
-
|
|
68
|
+
try {
|
|
69
|
+
absolutePath = `${absolutePath}.js`;
|
|
70
|
+
fs_1.default.accessSync(absolutePath, fs_1.default.constants.F_OK);
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
throw new Error(`${absolutePath} is not exists!`);
|
|
74
|
+
}
|
|
70
75
|
}
|
|
71
76
|
}
|
|
72
77
|
return absolutePath;
|
package/core/util/ast.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ 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
|
/**
|
|
9
|
-
* 获取所有依赖第三方模块路径
|
|
9
|
+
* 获取所有依赖第三方模块路径(仅导入program下一级的require)
|
|
10
10
|
* val xx = require('path') => [xx, absolute path]
|
|
11
11
|
* @param baseDir - 源码所在文件夹目录
|
|
12
12
|
* @param srcAst - 源码AST
|
package/core/util/ast.js
CHANGED
|
@@ -37,7 +37,7 @@ function outputObjectMethods(outPath, methods) {
|
|
|
37
37
|
}
|
|
38
38
|
exports.outputObjectMethods = outputObjectMethods;
|
|
39
39
|
/**
|
|
40
|
-
* 获取所有依赖第三方模块路径
|
|
40
|
+
* 获取所有依赖第三方模块路径(仅导入program下一级的require)
|
|
41
41
|
* val xx = require('path') => [xx, absolute path]
|
|
42
42
|
* @param baseDir - 源码所在文件夹目录
|
|
43
43
|
* @param srcAst - 源码AST
|
|
@@ -47,28 +47,30 @@ exports.outputObjectMethods = outputObjectMethods;
|
|
|
47
47
|
function getRequireModPaths(baseDir, srcAst, ignoreMod) {
|
|
48
48
|
const modPathByName = new Map();
|
|
49
49
|
(0, traverse_1.default)(srcAst, {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const ignore = ignoreMod === null || ignoreMod === void 0 ? void 0 : ignoreMod.includes(id.name);
|
|
65
|
-
if (ignore) {
|
|
66
|
-
console.log(`ignore => ${id.name}`);
|
|
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)) {
|
|
54
|
+
if (item.init.callee.name === "require") {
|
|
55
|
+
if (core_1.types.isIdentifier(item.id)) {
|
|
56
|
+
if (ignoreMod === null || ignoreMod === void 0 ? void 0 : ignoreMod.includes(item.id.name)) {
|
|
57
|
+
console.log(`ignore module => ${item.id.name}`);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
67
64
|
}
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
return false;
|
|
66
|
+
}).forEach(item => {
|
|
67
|
+
const args = (item.init.arguments);
|
|
68
|
+
if (args.length) {
|
|
69
|
+
const modName = item.id.name;
|
|
70
|
+
const modPath = args[0].value;
|
|
71
|
+
modPathByName.set(modName, (0, scanf_1.scanfRequieMod)(baseDir, modPath));
|
|
70
72
|
}
|
|
71
|
-
}
|
|
73
|
+
});
|
|
72
74
|
}
|
|
73
75
|
});
|
|
74
76
|
return modPathByName;
|
|
@@ -300,7 +302,7 @@ function importMods(outDir, srcAst, mapContext) {
|
|
|
300
302
|
const depNames = mapContext.getDependencyNameByMod("self");
|
|
301
303
|
const importMods = depNames.map(name => {
|
|
302
304
|
const srcPath = mapContext.getSrcPathByMod(name);
|
|
303
|
-
const requireFrom = (0, path_1.relative)(outDir, srcPath.replace(/index
|
|
305
|
+
const requireFrom = (0, path_1.relative)(outDir, srcPath.replace(/(index)?\.js$/, "")).replace(/[\\]+/g, "/");
|
|
304
306
|
const variableDeclarator = core_1.types.variableDeclarator(core_1.types.identifier(name), core_1.types.callExpression(core_1.types.identifier("require"), [
|
|
305
307
|
core_1.types.stringLiteral(requireFrom)
|
|
306
308
|
]));
|