@kezh/my-custom-js-converter 1.1.11 → 1.4.0
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/.claude/plan.md +53 -0
- package/CLAUDE.md +50 -0
- package/README.en.md +116 -0
- package/README.md +116 -0
- package/TASKS.md +36 -0
- package/core/config/index.d.ts +2 -2
- package/core/config/index.js +69 -44
- package/core/config/main_config.d.ts +20 -19
- package/core/config/main_config.js +2 -2
- package/core/config/map_config.d.ts +15 -15
- package/core/config/map_config.js +22 -22
- package/core/context/map_context.d.ts +28 -28
- package/core/context/map_context.js +180 -179
- package/core/decode/index.d.ts +2 -2
- package/core/decode/index.js +128 -58
- package/core/encode/index.d.ts +2 -2
- package/core/encode/index.js +103 -44
- package/core/scanf/index.d.ts +18 -12
- package/core/scanf/index.js +92 -79
- package/core/util/ast.d.ts +84 -83
- package/core/util/ast.js +454 -433
- package/core/util/parent_path.d.ts +5 -5
- package/core/util/parent_path.js +17 -15
- package/core/util/version.d.ts +43 -0
- package/core/util/version.js +103 -0
- package/package.json +42 -35
- package/test/fixtures/sample_project/config.json +17 -0
- package/test/manual_review.js +288 -0
- package/test/run_tests.js +413 -0
- package/tsconfig.json +1 -1
- package/tsconfig.tsbuildinfo +1 -0
- package/.vscode/settings.json +0 -9
package/.claude/plan.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# 实现 @ 路径别名支持
|
|
2
|
+
|
|
3
|
+
## 目标
|
|
4
|
+
在 `config.js` 中添加开关配置,支持 `@` 写法:`const mod = require('@/util/@3.0.0/mod.js')`,其中 `@` 代表项目根目录(baseDir)。
|
|
5
|
+
|
|
6
|
+
## 修改文件
|
|
7
|
+
|
|
8
|
+
### 1. `src/config/main_config.ts` — 添加配置项
|
|
9
|
+
在 `EncodeConfig` 中添加 `useAlias: boolean` 开关(默认 `false`)。
|
|
10
|
+
|
|
11
|
+
### 2. `src/config/index.ts` — 添加默认值
|
|
12
|
+
在 `defConfig.encode` 中添加 `useAlias: false`。
|
|
13
|
+
|
|
14
|
+
### 3. `src/scanf/index.ts` — `scanfRequieMod` 支持 @ 路径
|
|
15
|
+
当 `modPath` 以 `@/` 或 `@\\` 开头时,将 `@` 替换为 `baseDir`(项目根目录),然后再进行路径解析。
|
|
16
|
+
|
|
17
|
+
修改逻辑:
|
|
18
|
+
```typescript
|
|
19
|
+
export function scanfRequieMod(baseDir: string, modPath: string): string {
|
|
20
|
+
// 支持 @ 别名:@/xxx => baseDir/xxx
|
|
21
|
+
if (modPath.startsWith("@/") || modPath.startsWith("@\\")) {
|
|
22
|
+
modPath = modPath.replace(/^@[/\\]/, baseDir + path.sep);
|
|
23
|
+
}
|
|
24
|
+
// ... 原有逻辑不变
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### 4. `src/util/ast.ts` — `importMods` 支持 @ 路径生成
|
|
29
|
+
decode 时生成 require 语句,如果 `config.encode.useAlias` 为 `true`,则生成 `@/...` 格式而非相对路径。
|
|
30
|
+
|
|
31
|
+
需要将 `config` 传入 `importMods` 函数,或在函数内读取全局 config。
|
|
32
|
+
|
|
33
|
+
修改逻辑:
|
|
34
|
+
```typescript
|
|
35
|
+
// 当 useAlias 开启时,使用 @ 别名路径替代相对路径
|
|
36
|
+
if (useAlias) {
|
|
37
|
+
const aliasPath = srcPath.replace(baseDir, "").replace(/\\/g, "/");
|
|
38
|
+
requireFrom = "@" + aliasPath;
|
|
39
|
+
} else {
|
|
40
|
+
requireFrom = relative(outDir, srcPath.replace(/(index)?\.js$/, "")).replace(/[\\]+/g, "/");
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 5. `src/decode/index.ts` — 传递 config 给 importMods
|
|
45
|
+
`importMods` 调用处需要传递 config 信息。
|
|
46
|
+
|
|
47
|
+
## 实现顺序
|
|
48
|
+
1. 修改 `MainConfig` 添加 `useAlias` 字段
|
|
49
|
+
2. 修改 `initConfig` 添加默认值
|
|
50
|
+
3. 修改 `scanfRequieMod` 支持 `@` 路径解析
|
|
51
|
+
4. 修改 `importMods` 支持 `@` 路径生成
|
|
52
|
+
5. 修改 `decode/index.ts` 传递 config
|
|
53
|
+
6. 编译验证
|
package/CLAUDE.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
+
|
|
5
|
+
## Project Overview
|
|
6
|
+
|
|
7
|
+
`@kezh/my-custom-js-converter` is a Babel-based JavaScript code transformer that **encodes** (flattens) and **decodes** (restores) CommonJS module structures. It takes JavaScript files that use `require()` + object-oriented patterns and flattens them into a single object with renamed methods, then can reverse the process.
|
|
8
|
+
|
|
9
|
+
## Build & Run
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm install # install dependencies
|
|
13
|
+
pnpm build # compile TypeScript (npx tsc --build)
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The compiled output goes to `core/` (gitignored). Source is in `src/`.
|
|
17
|
+
|
|
18
|
+
Two CLI entry points (declared in `package.json` bin):
|
|
19
|
+
- `encode` → `core/encode/index.js` — flattens modules into a single file
|
|
20
|
+
- `decode` → `core/decode/index.js` — restores the original module structure
|
|
21
|
+
|
|
22
|
+
No test framework is configured.
|
|
23
|
+
|
|
24
|
+
## Architecture
|
|
25
|
+
|
|
26
|
+
### Data Flow
|
|
27
|
+
|
|
28
|
+
**Encode**: Scan target dirs → find entry JS files → build dependency map (`MapContext`) → extract & rename methods from all modules → output single flattened file + `mod.map`
|
|
29
|
+
|
|
30
|
+
**Decode**: Read `mod.map` → read encoded file → re-insert `require` statements → remove inlined dependency methods → restore original method names → output restored file
|
|
31
|
+
|
|
32
|
+
### Key Modules
|
|
33
|
+
|
|
34
|
+
- **`src/config/`** — Configuration loading. `MainConfig` defines encode/decode settings (entry object name, file names, ignore list). Reads `config.json` from the parent project root when installed as a dependency.
|
|
35
|
+
- **`src/context/map_context.ts`** — Core orchestration. `MapContext` builds a dependency graph by recursively walking `require()` calls, then performs **filtering** (removing invalid method references) and **tree-shaking** (removing unused methods). Serializes to `mod.map` JSON file.
|
|
36
|
+
- **`src/util/ast.ts`** — All Babel AST manipulation: parsing, traversing, extracting object methods, renaming methods (appending `__modName` suffix), generating import statements, and reversing the renaming during decode.
|
|
37
|
+
- **`src/scanf/`** — File system scanning: finding target files by name, resolving `require()` paths (handles both `dir/index.js` and `file.js` patterns).
|
|
38
|
+
- **`src/util/parent_path.ts`** — Detects whether the tool is running inside `node_modules/` and resolves the parent project root.
|
|
39
|
+
|
|
40
|
+
### Encoding Convention
|
|
41
|
+
|
|
42
|
+
Methods from dependency modules are renamed with a `__modName` suffix (e.g., `helperMethod` becomes `helperMethod__util`). `this.method()` calls in the root module stay as-is; in dependency modules, `this.method()` becomes `this.method__modName()`. The decode step reverses this convention.
|
|
43
|
+
|
|
44
|
+
### Configuration
|
|
45
|
+
|
|
46
|
+
When used as a dependency, the tool reads `config.json` from the parent project's root. Default config:
|
|
47
|
+
- `encode.file`: `"index.js"`, `encode.output`: `"coded.js"`, `encode.entry`: `"customEvent"`
|
|
48
|
+
- `decode.file`: `"coded.js"`, `decode.output`: `"decoded.js"`, `decode.mount`: `"customEvent"`
|
|
49
|
+
- `settingDir`: `".setting"` (where `mod.map` is stored)
|
|
50
|
+
- `target`: directories to scan relative to `baseDir`
|
package/README.en.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
[中文](README.md)
|
|
2
|
+
|
|
3
|
+
# my-custom-js-converter
|
|
4
|
+
|
|
5
|
+
A Babel-based JavaScript code transformer that flattens and restores CommonJS module structures.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Encode** — Recursively scans `require()` dependencies, extracts used methods from each module, and flattens them into a single file with renamed methods (e.g., `helperMethod__util`).
|
|
10
|
+
- **Decode** — Reads the generated dependency map and restores the original module structure by re-inserting `require()` statements and reversing method renaming.
|
|
11
|
+
- **Tree-shaking** — Only methods that are actually used are included in the encoded output; unused methods are stripped.
|
|
12
|
+
- **Configurable** — Entry object name, file names, ignore list, and scan directories are all configurable via `config.json`.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @kezh/my-custom-js-converter
|
|
18
|
+
# or
|
|
19
|
+
pnpm add @kezh/my-custom-js-converter
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### 1. Create a config file
|
|
25
|
+
|
|
26
|
+
Create `config.json` in your project root:
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
{
|
|
30
|
+
"encode": {
|
|
31
|
+
"file": "index.js",
|
|
32
|
+
"output": "coded.js",
|
|
33
|
+
"entry": "customEvent",
|
|
34
|
+
"ignoreMod": ["commonUtil"]
|
|
35
|
+
},
|
|
36
|
+
"decode": {
|
|
37
|
+
"file": "coded.js",
|
|
38
|
+
"output": "decoded.js",
|
|
39
|
+
"mount": "customEvent"
|
|
40
|
+
},
|
|
41
|
+
"target": ["src"],
|
|
42
|
+
"settingDir": ".setting"
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
#### Config Options
|
|
47
|
+
|
|
48
|
+
| Field | Description |
|
|
49
|
+
|---|---|
|
|
50
|
+
| `encode.file` | Source file name to encode |
|
|
51
|
+
| `encode.output` | Encoded output file name |
|
|
52
|
+
| `encode.entry` | Entry object name in source |
|
|
53
|
+
| `encode.ignoreMod` | Module names to skip during encoding |
|
|
54
|
+
| `decode.file` | Encoded file to decode |
|
|
55
|
+
| `decode.output` | Decoded output file name |
|
|
56
|
+
| `decode.mount` | Object name to mount decoded code |
|
|
57
|
+
| `target` | Directories to scan (relative to project root) |
|
|
58
|
+
| `settingDir` | Directory for the dependency map file |
|
|
59
|
+
|
|
60
|
+
### 2. Encode
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npx encode
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Scans the `target` directories for files matching `encode.file`, builds a dependency map, and outputs:
|
|
67
|
+
- `encode.output` — the flattened code file
|
|
68
|
+
- `settingDir/mod.map` — the dependency map (needed for decoding)
|
|
69
|
+
|
|
70
|
+
### 3. Decode
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npx decode
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Reads the dependency map from `settingDir/mod.map`, restores `require()` imports, reverses method renaming, and outputs `decode.output`.
|
|
77
|
+
|
|
78
|
+
## How It Works
|
|
79
|
+
|
|
80
|
+
### Encode Flow
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
index.js (entry: customEvent)
|
|
84
|
+
├── require('./util') → util.methodA(), util.methodB()
|
|
85
|
+
└── require('./helper') → helper.process()
|
|
86
|
+
|
|
87
|
+
↓ encode
|
|
88
|
+
|
|
89
|
+
coded.js
|
|
90
|
+
customEvent = {
|
|
91
|
+
ownMethod() { ... },
|
|
92
|
+
methodA__util() { ... }, // from util
|
|
93
|
+
methodB__util() { ... }, // from util
|
|
94
|
+
process__helper() { ... } // from helper
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Decode Flow
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
coded.js + mod.map
|
|
102
|
+
|
|
103
|
+
↓ decode
|
|
104
|
+
|
|
105
|
+
decoded.js
|
|
106
|
+
const util = require('./util');
|
|
107
|
+
const helper = require('./helper');
|
|
108
|
+
customEvent = {
|
|
109
|
+
ownMethod() { ... },
|
|
110
|
+
// methodA__util, methodB__util, process__helper removed
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
MIT
|
package/README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
[English](README.en.md)
|
|
2
|
+
|
|
3
|
+
# my-custom-js-converter
|
|
4
|
+
|
|
5
|
+
基于 Babel 的 JavaScript 代码转换器,用于扁平化和还原 CommonJS 模块结构。
|
|
6
|
+
|
|
7
|
+
## 功能
|
|
8
|
+
|
|
9
|
+
- **编码(Encode)** — 递归扫描 `require()` 依赖,从每个模块中提取被使用的方法,扁平化到单个文件中并重命名方法(如 `helperMethod__util`)。
|
|
10
|
+
- **解码(Decode)** — 读取生成的依赖映射文件,还原原始模块结构,重新插入 `require()` 语句并恢复方法命名。
|
|
11
|
+
- **摇树优化(Tree-shaking)** — 编码输出仅包含实际使用的方法,未使用的方法会被移除。
|
|
12
|
+
- **可配置** — 入口对象名、文件名、忽略列表和扫描目录均可通过 `config.json` 配置。
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @kezh/my-custom-js-converter
|
|
18
|
+
# 或
|
|
19
|
+
pnpm add @kezh/my-custom-js-converter
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 使用方式
|
|
23
|
+
|
|
24
|
+
### 1. 创建配置文件
|
|
25
|
+
|
|
26
|
+
在项目根目录创建 `config.json`:
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
{
|
|
30
|
+
"encode": {
|
|
31
|
+
"file": "index.js",
|
|
32
|
+
"output": "coded.js",
|
|
33
|
+
"entry": "customEvent",
|
|
34
|
+
"ignoreMod": ["commonUtil"]
|
|
35
|
+
},
|
|
36
|
+
"decode": {
|
|
37
|
+
"file": "coded.js",
|
|
38
|
+
"output": "decoded.js",
|
|
39
|
+
"mount": "customEvent"
|
|
40
|
+
},
|
|
41
|
+
"target": ["src"],
|
|
42
|
+
"settingDir": ".setting"
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
#### 配置项
|
|
47
|
+
|
|
48
|
+
| 字段 | 说明 |
|
|
49
|
+
|---|---|
|
|
50
|
+
| `encode.file` | 编码的源文件名 |
|
|
51
|
+
| `encode.output` | 编码输出文件名 |
|
|
52
|
+
| `encode.entry` | 源码中的入口对象名 |
|
|
53
|
+
| `encode.ignoreMod` | 编码时忽略的模块名 |
|
|
54
|
+
| `decode.file` | 待解码的文件名 |
|
|
55
|
+
| `decode.output` | 解码输出文件名 |
|
|
56
|
+
| `decode.mount` | 解码代码的挂载对象名 |
|
|
57
|
+
| `target` | 扫描目录(相对于项目根目录) |
|
|
58
|
+
| `settingDir` | 依赖映射文件存放目录 |
|
|
59
|
+
|
|
60
|
+
### 2. 编码
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npx encode
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
扫描 `target` 目录中匹配 `encode.file` 的文件,构建依赖映射,输出:
|
|
67
|
+
- `encode.output` — 扁平化后的代码文件
|
|
68
|
+
- `settingDir/mod.map` — 依赖映射文件(解码时需要)
|
|
69
|
+
|
|
70
|
+
### 3. 解码
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npx decode
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
读取 `settingDir/mod.map` 中的依赖映射,还原 `require()` 导入,恢复方法命名,输出 `decode.output`。
|
|
77
|
+
|
|
78
|
+
## 工作原理
|
|
79
|
+
|
|
80
|
+
### 编码流程
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
index.js (entry: customEvent)
|
|
84
|
+
├── require('./util') → util.methodA(), util.methodB()
|
|
85
|
+
└── require('./helper') → helper.process()
|
|
86
|
+
|
|
87
|
+
↓ encode
|
|
88
|
+
|
|
89
|
+
coded.js
|
|
90
|
+
customEvent = {
|
|
91
|
+
ownMethod() { ... },
|
|
92
|
+
methodA__util() { ... }, // 来自 util
|
|
93
|
+
methodB__util() { ... }, // 来自 util
|
|
94
|
+
process__helper() { ... } // 来自 helper
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### 解码流程
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
coded.js + mod.map
|
|
102
|
+
|
|
103
|
+
↓ decode
|
|
104
|
+
|
|
105
|
+
decoded.js
|
|
106
|
+
const util = require('./util');
|
|
107
|
+
const helper = require('./helper');
|
|
108
|
+
customEvent = {
|
|
109
|
+
ownMethod() { ... },
|
|
110
|
+
// methodA__util 等已移除
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## 许可证
|
|
115
|
+
|
|
116
|
+
MIT
|
package/TASKS.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# 任务
|
|
2
|
+
编写体验优化,当前编写导入模块仅支持`const mod = require('../../util/@3.0.0/mod.js');`,我希望能够在`config.js`配置中加入开关配置,支持@写法:`const mod = require('@/util/@3.0.0/mod.js');`
|
|
3
|
+
|
|
4
|
+
# 实现方案
|
|
5
|
+
|
|
6
|
+
在 `EncodeConfig` 中添加 `useAlias: boolean` 开关:
|
|
7
|
+
- `useAlias: true` — encode 阶段解析 `@/` 路径别名(`@` 代表项目根目录),decode 阶段生成 `@/` 路径
|
|
8
|
+
- `useAlias: false`(默认)— 使用传统的相对路径
|
|
9
|
+
|
|
10
|
+
### 修改的文件
|
|
11
|
+
|
|
12
|
+
| 文件 | 改动 |
|
|
13
|
+
|------|------|
|
|
14
|
+
| `src/config/main_config.ts` | `EncodeConfig` 添加 `useAlias` 字段 |
|
|
15
|
+
| `src/config/index.ts` | 默认 `useAlias: false`;`__dirname` 不含 `node_modules` 时回退到 `process.cwd()`;尝试 `config.js` 和 `config.json` 两种配置文件 |
|
|
16
|
+
| `src/scanf/index.ts` | `scanfRequieMod` 添加 `rootDir` 参数,支持 `@/` 路径解析 |
|
|
17
|
+
| `src/util/ast.ts` | `getRequireModPaths` 传递 `rootDir`;`importMods` 根据 `useAlias` 生成 `@/` 或相对路径;修复相对路径缺少 `./` 前缀的问题 |
|
|
18
|
+
| `src/util/parent_path.ts` | `getParentRootDir` 在 junction symlink 场景下回退到 `config.baseDir` |
|
|
19
|
+
| `src/context/map_context.ts` | `buildMapContext` 根据 `useAlias` 条件传递 `rootDir` |
|
|
20
|
+
| `src/decode/index.ts` | 传递 `config.encode.useAlias` 给 `importMods` |
|
|
21
|
+
|
|
22
|
+
# 测试结果 ✅
|
|
23
|
+
|
|
24
|
+
### useAlias: true
|
|
25
|
+
- **Encode**: `@/src/util/index` 正确解析为绝对路径,方法正确内联并添加 `__modName` 后缀
|
|
26
|
+
- **Decode**: 生成 `require("@/src/util")` 和 `require("@/src/helper")`,`__modName` 后缀正确移除
|
|
27
|
+
|
|
28
|
+
### useAlias: false
|
|
29
|
+
- **Encode**: 相对路径 `./util/index` 正常解析
|
|
30
|
+
- **Decode**: 生成 `require("./util")` 和 `require("./helper")`,路径以 `./` 开头
|
|
31
|
+
|
|
32
|
+
### 修复的问题
|
|
33
|
+
1. `useAlias` 无效 — `scanfRequieMod` 缺少 `rootDir` 参数,`@` 路径无法解析
|
|
34
|
+
2. `Error: src\@\util\index.js is not exists!` — junction symlink 导致 `__dirname` 不含 `node_modules`,`getParentRootDir()` 返回 undefined
|
|
35
|
+
3. `require("util")` 缺少 `./` 前缀 — `path.relative()` 返回的路径不以 `./` 开头,Node.js 会当作 npm 包名
|
|
36
|
+
4. 配置文件查找失败 — 只尝试 `config.js`,不尝试 `config.json`
|
package/core/config/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { MainConfig } from "./main_config";
|
|
2
|
-
export declare const config: MainConfig;
|
|
1
|
+
import { MainConfig } from "./main_config";
|
|
2
|
+
export declare const config: MainConfig;
|
package/core/config/index.js
CHANGED
|
@@ -1,44 +1,69 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.config = void 0;
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
exports.config = initConfig();
|
|
10
|
-
function initConfig() {
|
|
11
|
-
const defConfig = {
|
|
12
|
-
encode: {
|
|
13
|
-
file: "index.js",
|
|
14
|
-
output: "coded.js",
|
|
15
|
-
entry: "customEvent",
|
|
16
|
-
ignoreMod: ["commonUtil"],
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.config = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
exports.config = initConfig();
|
|
10
|
+
function initConfig() {
|
|
11
|
+
const defConfig = {
|
|
12
|
+
encode: {
|
|
13
|
+
file: "index.js",
|
|
14
|
+
output: "coded.js",
|
|
15
|
+
entry: "customEvent",
|
|
16
|
+
ignoreMod: ["commonUtil"],
|
|
17
|
+
useAlias: false,
|
|
18
|
+
},
|
|
19
|
+
decode: {
|
|
20
|
+
file: "coded.js",
|
|
21
|
+
output: "decoded.js",
|
|
22
|
+
mount: "customEvent",
|
|
23
|
+
},
|
|
24
|
+
baseDir: "",
|
|
25
|
+
target: [""],
|
|
26
|
+
settingDir: ".setting",
|
|
27
|
+
customConfig: "config.js",
|
|
28
|
+
};
|
|
29
|
+
const curDir = __dirname;
|
|
30
|
+
let baseDir = curDir.replace(/node_modules.*/g, "");
|
|
31
|
+
// 当 __dirname 不包含 node_modules 时(如 junction symlink 场景),
|
|
32
|
+
// 尝试从 process.cwd() 查找配置文件
|
|
33
|
+
if (!curDir.includes("node_modules")) {
|
|
34
|
+
baseDir = process.cwd();
|
|
35
|
+
}
|
|
36
|
+
// 尝试查找配置文件:先 customConfig 指定的文件名,再 config.json
|
|
37
|
+
const configCandidates = [defConfig.customConfig, "config.json"];
|
|
38
|
+
for (const configName of configCandidates) {
|
|
39
|
+
try {
|
|
40
|
+
const configPath = path_1.default.join(baseDir, configName);
|
|
41
|
+
fs_1.default.accessSync(configPath);
|
|
42
|
+
let customConfig;
|
|
43
|
+
if (configPath.endsWith(".js")) {
|
|
44
|
+
// 支持 .js 配置文件,可使用注释
|
|
45
|
+
customConfig = require(configPath);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
// 兼容 .json 配置文件
|
|
49
|
+
const json = fs_1.default.readFileSync(configPath, { encoding: "utf-8" });
|
|
50
|
+
customConfig = JSON.parse(json);
|
|
51
|
+
}
|
|
52
|
+
defConfig.baseDir = baseDir;
|
|
53
|
+
Object.keys(customConfig).map(key => key).forEach((key) => {
|
|
54
|
+
if (customConfig[key]) {
|
|
55
|
+
defConfig[key] = customConfig[key];
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
// baseDir 允许从配置文件中显式覆盖
|
|
59
|
+
if (customConfig.baseDir) {
|
|
60
|
+
defConfig.baseDir = customConfig.baseDir;
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return defConfig;
|
|
69
|
+
}
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
export interface MainConfig {
|
|
2
|
-
encode: EncodeConfig;
|
|
3
|
-
decode: DecodeConfig;
|
|
4
|
-
baseDir: string;
|
|
5
|
-
target: string[];
|
|
6
|
-
settingDir: string;
|
|
7
|
-
customConfig: string;
|
|
8
|
-
}
|
|
9
|
-
export interface EncodeConfig {
|
|
10
|
-
file: string;
|
|
11
|
-
output: string;
|
|
12
|
-
entry: string;
|
|
13
|
-
ignoreMod: string[];
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
export interface MainConfig {
|
|
2
|
+
encode: EncodeConfig;
|
|
3
|
+
decode: DecodeConfig;
|
|
4
|
+
baseDir: string;
|
|
5
|
+
target: string[];
|
|
6
|
+
settingDir: string;
|
|
7
|
+
customConfig: string;
|
|
8
|
+
}
|
|
9
|
+
export interface EncodeConfig {
|
|
10
|
+
file: string;
|
|
11
|
+
output: string;
|
|
12
|
+
entry: string;
|
|
13
|
+
ignoreMod: string[];
|
|
14
|
+
useAlias: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface DecodeConfig {
|
|
17
|
+
file: string;
|
|
18
|
+
output: string;
|
|
19
|
+
mount: string;
|
|
20
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
export declare class MapConfig {
|
|
2
|
-
[mod: string]: Mod;
|
|
3
|
-
}
|
|
4
|
-
export declare class Mod {
|
|
5
|
-
src: string;
|
|
6
|
-
dependencies: Dependencies;
|
|
7
|
-
constructor(src: string, dependencies?: Dependencies);
|
|
8
|
-
}
|
|
9
|
-
export declare class Dependencies {
|
|
10
|
-
[name: string]: Dependency;
|
|
11
|
-
}
|
|
12
|
-
export declare class Dependency {
|
|
13
|
-
methods: string[];
|
|
14
|
-
constructor(methods?: string[]);
|
|
15
|
-
}
|
|
1
|
+
export declare class MapConfig {
|
|
2
|
+
[mod: string]: Mod;
|
|
3
|
+
}
|
|
4
|
+
export declare class Mod {
|
|
5
|
+
src: string;
|
|
6
|
+
dependencies: Dependencies;
|
|
7
|
+
constructor(src: string, dependencies?: Dependencies);
|
|
8
|
+
}
|
|
9
|
+
export declare class Dependencies {
|
|
10
|
+
[name: string]: Dependency;
|
|
11
|
+
}
|
|
12
|
+
export declare class Dependency {
|
|
13
|
+
methods: string[];
|
|
14
|
+
constructor(methods?: string[]);
|
|
15
|
+
}
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Dependency = exports.Dependencies = exports.Mod = exports.MapConfig = void 0;
|
|
4
|
-
class MapConfig {
|
|
5
|
-
}
|
|
6
|
-
exports.MapConfig = MapConfig;
|
|
7
|
-
class Mod {
|
|
8
|
-
constructor(src, dependencies = new Dependencies()) {
|
|
9
|
-
this.src = src;
|
|
10
|
-
this.dependencies = dependencies;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
exports.Mod = Mod;
|
|
14
|
-
class Dependencies {
|
|
15
|
-
}
|
|
16
|
-
exports.Dependencies = Dependencies;
|
|
17
|
-
class Dependency {
|
|
18
|
-
constructor(methods = []) {
|
|
19
|
-
this.methods = methods;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
exports.Dependency = Dependency;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Dependency = exports.Dependencies = exports.Mod = exports.MapConfig = void 0;
|
|
4
|
+
class MapConfig {
|
|
5
|
+
}
|
|
6
|
+
exports.MapConfig = MapConfig;
|
|
7
|
+
class Mod {
|
|
8
|
+
constructor(src, dependencies = new Dependencies()) {
|
|
9
|
+
this.src = src;
|
|
10
|
+
this.dependencies = dependencies;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.Mod = Mod;
|
|
14
|
+
class Dependencies {
|
|
15
|
+
}
|
|
16
|
+
exports.Dependencies = Dependencies;
|
|
17
|
+
class Dependency {
|
|
18
|
+
constructor(methods = []) {
|
|
19
|
+
this.methods = methods;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Dependency = Dependency;
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { Mod } from "../config/map_config";
|
|
2
|
-
export declare class MapContext {
|
|
3
|
-
static SAVE_FILE: string;
|
|
4
|
-
private srcPath;
|
|
5
|
-
private entry;
|
|
6
|
-
private outDir;
|
|
7
|
-
private map;
|
|
8
|
-
constructor(srcPath?: string, entry?: string, outDir?: string, initMap?: boolean);
|
|
9
|
-
appendModMap(mod: Mod, self: boolean, name?: string): void;
|
|
10
|
-
getModNames(): string[];
|
|
11
|
-
getMod(name: string): Mod | null | undefined;
|
|
12
|
-
getMethodNamesByMod(name: string): string[];
|
|
13
|
-
getDependencyNameByMod(name: string): string[];
|
|
14
|
-
getSrcPathByMod(name: string): string | undefined;
|
|
15
|
-
getAbsoluteSrcPathByMod(name: string): string | undefined;
|
|
16
|
-
static readFromLocal(inDir: string): MapContext;
|
|
17
|
-
private init;
|
|
18
|
-
private writeToLocal;
|
|
19
|
-
private buildMapContext;
|
|
20
|
-
/**
|
|
21
|
-
* 过滤self的dependencies中无效方法
|
|
22
|
-
*/
|
|
23
|
-
private filtering;
|
|
24
|
-
/**
|
|
25
|
-
* 去除dependencies中未使用方法
|
|
26
|
-
*/
|
|
27
|
-
private shaking;
|
|
28
|
-
}
|
|
1
|
+
import { Mod } from "../config/map_config";
|
|
2
|
+
export declare class MapContext {
|
|
3
|
+
static SAVE_FILE: string;
|
|
4
|
+
private srcPath;
|
|
5
|
+
private entry;
|
|
6
|
+
private outDir;
|
|
7
|
+
private map;
|
|
8
|
+
constructor(srcPath?: string, entry?: string, outDir?: string, initMap?: boolean);
|
|
9
|
+
appendModMap(mod: Mod, self: boolean, name?: string): void;
|
|
10
|
+
getModNames(): string[];
|
|
11
|
+
getMod(name: string): Mod | null | undefined;
|
|
12
|
+
getMethodNamesByMod(name: string): string[];
|
|
13
|
+
getDependencyNameByMod(name: string): string[];
|
|
14
|
+
getSrcPathByMod(name: string): string | undefined;
|
|
15
|
+
getAbsoluteSrcPathByMod(name: string): string | undefined;
|
|
16
|
+
static readFromLocal(inDir: string): MapContext;
|
|
17
|
+
private init;
|
|
18
|
+
private writeToLocal;
|
|
19
|
+
private buildMapContext;
|
|
20
|
+
/**
|
|
21
|
+
* 过滤self的dependencies中无效方法
|
|
22
|
+
*/
|
|
23
|
+
private filtering;
|
|
24
|
+
/**
|
|
25
|
+
* 去除dependencies中未使用方法
|
|
26
|
+
*/
|
|
27
|
+
private shaking;
|
|
28
|
+
}
|