@dimina/compiler 1.0.6 → 1.0.8
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/README.md +48 -0
- package/dist/bin/index.cjs +3 -3
- package/dist/bin/index.js +3 -3
- package/dist/core/logic-compiler.cjs +125 -35
- package/dist/core/logic-compiler.js +125 -36
- package/dist/core/style-compiler.cjs +77 -19
- package/dist/core/style-compiler.js +59 -19
- package/dist/core/view-compiler.cjs +647 -145
- package/dist/core/view-compiler.js +645 -144
- package/dist/env-Cmen1qwy.cjs +543 -0
- package/dist/env-Csj3AHY4.js +544 -0
- package/dist/index.cjs +259 -37
- package/dist/index.js +259 -37
- package/package.json +14 -11
- package/dist/env-CezfCSQz.js +0 -299
- package/dist/env-Chow6VXH.cjs +0 -298
package/README.md
CHANGED
|
@@ -98,11 +98,59 @@ dmcc build --no-app-id-dir
|
|
|
98
98
|
|
|
99
99
|
```txt
|
|
100
100
|
app.js, index.js -> logic.js (逻辑文件)
|
|
101
|
+
app.ts, index.ts -> logic.js (TypeScript 逻辑文件)
|
|
101
102
|
index.wxml -> view.js (视图文件)
|
|
102
103
|
app.wxss, index.wxss -> style.css (样式文件)
|
|
104
|
+
app.less, index.less -> style.css (Less 样式文件)
|
|
105
|
+
app.scss, index.scss -> style.css (SCSS 样式文件)
|
|
106
|
+
app.sass, index.sass -> style.css (Sass 样式文件)
|
|
103
107
|
app.json, index.json -> config.json (配置文件)
|
|
108
|
+
miniprogram_npm/ -> npm包构建 (npm组件支持)
|
|
104
109
|
```
|
|
105
110
|
|
|
111
|
+
### TypeScript、Less 和 SCSS 支持
|
|
112
|
+
|
|
113
|
+
编译器现已支持现代前端开发工具:
|
|
114
|
+
|
|
115
|
+
- ✅ **TypeScript 支持**: `.ts` 文件自动编译为 JavaScript
|
|
116
|
+
- ✅ **ES6 Import 语句**: 支持相对路径、npm 包和绝对路径导入
|
|
117
|
+
- ✅ **Less 支持**: `.less` 文件编译为 CSS,支持变量、mixin 和嵌套
|
|
118
|
+
- ✅ **SCSS/Sass 支持**: `.scss` 和 `.sass` 文件编译为 CSS
|
|
119
|
+
- ✅ **错误处理**: 编译失败时自动回退,不中断构建流程
|
|
120
|
+
- ✅ **向后兼容**: 完全兼容现有的 `.js` 和 `.wxss` 文件
|
|
121
|
+
|
|
122
|
+
#### 支持的文件类型
|
|
123
|
+
|
|
124
|
+
**逻辑文件查找顺序**: `.js` → `.ts`
|
|
125
|
+
**样式文件查找顺序**: `.wxss` → `.ddss` → `.less` → `.scss` → `.sass`
|
|
126
|
+
|
|
127
|
+
详细使用说明请参考:[TypeScript、Less 和 SCSS 支持文档](./docs/typescript-less-scss-support.md)
|
|
128
|
+
|
|
129
|
+
### npm 组件支持
|
|
130
|
+
|
|
131
|
+
编译器现已支持微信小程序的 npm 包功能,遵循微信官方的 npm 支持规范:
|
|
132
|
+
|
|
133
|
+
- ✅ 支持 `miniprogram_npm` 目录中的组件解析
|
|
134
|
+
- ✅ 按照微信小程序寻址顺序查找组件
|
|
135
|
+
- ✅ 自动构建和复制 npm 包文件
|
|
136
|
+
- ✅ 支持组件依赖关系处理
|
|
137
|
+
- ✅ 缓存机制提升编译性能
|
|
138
|
+
- ✅ 兼容现有的相对路径组件引用
|
|
139
|
+
|
|
140
|
+
#### npm 组件使用示例
|
|
141
|
+
|
|
142
|
+
```json
|
|
143
|
+
// pages/index/index.json
|
|
144
|
+
{
|
|
145
|
+
"usingComponents": {
|
|
146
|
+
"lib-button": "lib-weapp/button",
|
|
147
|
+
"custom-component": "./components/custom"
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
详细使用说明请参考:[npm 支持文档](./docs/npm-support.md)
|
|
153
|
+
|
|
106
154
|
### 编译产物目录结构
|
|
107
155
|
|
|
108
156
|
编译后,默认会在目标目录生成以小程序 id 命名的文件夹,项目结构如下:
|
package/dist/bin/index.cjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
const process = require("node:process");
|
|
4
3
|
const path = require("node:path");
|
|
5
|
-
const
|
|
4
|
+
const process = require("node:process");
|
|
6
5
|
const chokidar = require("chokidar");
|
|
6
|
+
const commander = require("commander");
|
|
7
7
|
const index = require("../index.cjs");
|
|
8
|
-
const version = "1.0.
|
|
8
|
+
const version = "1.0.7";
|
|
9
9
|
const pack = {
|
|
10
10
|
version
|
|
11
11
|
};
|
package/dist/bin/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import process from "node:process";
|
|
3
2
|
import path from "node:path";
|
|
4
|
-
import
|
|
3
|
+
import process from "node:process";
|
|
5
4
|
import chokidar from "chokidar";
|
|
5
|
+
import { program } from "commander";
|
|
6
6
|
import build from "../index.js";
|
|
7
|
-
const version = "1.0.
|
|
7
|
+
const version = "1.0.7";
|
|
8
8
|
const pack = {
|
|
9
9
|
version
|
|
10
10
|
};
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
3
|
const fs = require("node:fs");
|
|
3
4
|
const path = require("node:path");
|
|
4
5
|
const node_worker_threads = require("node:worker_threads");
|
|
5
6
|
const babel = require("@babel/core");
|
|
6
|
-
const types = require("@babel/types");
|
|
7
7
|
const _traverse = require("@babel/traverse");
|
|
8
|
+
const types = require("@babel/types");
|
|
8
9
|
const esbuild = require("esbuild");
|
|
9
|
-
const
|
|
10
|
+
const ts = require("typescript");
|
|
11
|
+
const transformModulesCommonjs = require("@babel/plugin-transform-modules-commonjs");
|
|
12
|
+
const env = require("../env-Cmen1qwy.cjs");
|
|
10
13
|
const traverse = _traverse.default ? _traverse.default : _traverse;
|
|
11
14
|
const processedModules = /* @__PURE__ */ new Set();
|
|
12
15
|
if (!node_worker_threads.isMainThread) {
|
|
@@ -25,18 +28,33 @@ if (!node_worker_threads.isMainThread) {
|
|
|
25
28
|
};
|
|
26
29
|
const mainCompileRes = await compileJS(pages.mainPages, null, null, progress);
|
|
27
30
|
for (const [root, subPages] of Object.entries(pages.subPages)) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
try {
|
|
32
|
+
const subCompileRes = await compileJS(
|
|
33
|
+
subPages.info,
|
|
34
|
+
root,
|
|
35
|
+
subPages.independent ? [] : mainCompileRes,
|
|
36
|
+
progress
|
|
37
|
+
);
|
|
38
|
+
await writeCompileRes(subCompileRes, root);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new Error(`Error processing subpackage ${root}: ${error.message}
|
|
41
|
+
${error.stack}`);
|
|
42
|
+
}
|
|
35
43
|
}
|
|
36
44
|
await writeCompileRes(mainCompileRes, null);
|
|
37
45
|
node_worker_threads.parentPort.postMessage({ success: true });
|
|
38
46
|
} catch (error) {
|
|
39
|
-
node_worker_threads.parentPort.postMessage({
|
|
47
|
+
node_worker_threads.parentPort.postMessage({
|
|
48
|
+
success: false,
|
|
49
|
+
error: {
|
|
50
|
+
message: error.message,
|
|
51
|
+
stack: error.stack,
|
|
52
|
+
name: error.name,
|
|
53
|
+
file: error.file || null,
|
|
54
|
+
line: error.line || null,
|
|
55
|
+
code: error.code || null
|
|
56
|
+
}
|
|
57
|
+
});
|
|
40
58
|
}
|
|
41
59
|
});
|
|
42
60
|
}
|
|
@@ -63,7 +81,7 @@ ${module2.code}
|
|
|
63
81
|
} else {
|
|
64
82
|
const mainDir = `${env.getTargetPath()}/main`;
|
|
65
83
|
if (!fs.existsSync(mainDir)) {
|
|
66
|
-
fs.mkdirSync(mainDir);
|
|
84
|
+
fs.mkdirSync(mainDir, { recursive: true });
|
|
67
85
|
}
|
|
68
86
|
fs.writeFileSync(`${mainDir}/logic.js`, mergeCode);
|
|
69
87
|
}
|
|
@@ -82,10 +100,12 @@ async function compileJS(pages, root, mainCompileRes, progress) {
|
|
|
82
100
|
function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtra, depthChain = [], putMain = false) {
|
|
83
101
|
const currentPath = module2.path;
|
|
84
102
|
if (depthChain.includes(currentPath)) {
|
|
85
|
-
console.warn(`检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
103
|
+
console.warn("[logic]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
104
|
+
return;
|
|
86
105
|
}
|
|
87
|
-
if (depthChain.length >
|
|
88
|
-
console.warn(`检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
106
|
+
if (depthChain.length > 20) {
|
|
107
|
+
console.warn("[logic]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
108
|
+
return;
|
|
89
109
|
}
|
|
90
110
|
depthChain = [...depthChain, currentPath];
|
|
91
111
|
if (!module2.path) {
|
|
@@ -99,9 +119,35 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
|
|
|
99
119
|
code: ""
|
|
100
120
|
};
|
|
101
121
|
const src = module2.path.startsWith("/") ? module2.path : `/${module2.path}`;
|
|
102
|
-
const modulePath =
|
|
103
|
-
|
|
104
|
-
|
|
122
|
+
const modulePath = getJSAbsolutePath(src);
|
|
123
|
+
if (!modulePath) {
|
|
124
|
+
console.warn("[logic]", `找不到模块文件: ${src}`);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const sourceCode = env.getContentByPath(modulePath);
|
|
128
|
+
if (!sourceCode) {
|
|
129
|
+
console.warn("[logic]", `无法读取模块文件: ${modulePath}`);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
let jsCode = sourceCode;
|
|
133
|
+
if (modulePath.endsWith(".ts")) {
|
|
134
|
+
try {
|
|
135
|
+
const result = ts.transpileModule(sourceCode, {
|
|
136
|
+
compilerOptions: {
|
|
137
|
+
target: ts.ScriptTarget.ES2020,
|
|
138
|
+
module: ts.ModuleKind.CommonJS,
|
|
139
|
+
strict: false,
|
|
140
|
+
esModuleInterop: true,
|
|
141
|
+
skipLibCheck: true
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
jsCode = result.outputText;
|
|
145
|
+
} catch (error) {
|
|
146
|
+
console.error(`[logic] TypeScript 编译失败 ${modulePath}:`, error.message);
|
|
147
|
+
jsCode = sourceCode;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const ast = babel.parseSync(jsCode);
|
|
105
151
|
const addedArgs = types.objectExpression([
|
|
106
152
|
types.objectProperty(types.identifier("path"), types.stringLiteral(module2.path))
|
|
107
153
|
]);
|
|
@@ -115,7 +161,6 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
|
|
|
115
161
|
for (const [name, path2] of Object.entries(module2.usingComponents)) {
|
|
116
162
|
let toMainSubPackage = true;
|
|
117
163
|
if (packageName) {
|
|
118
|
-
const rootPackageName = packageName.startsWith("sub_") ? packageName.slice(4) : packageName;
|
|
119
164
|
const normalizedPath = path2.startsWith("/") ? path2.substring(1) : path2;
|
|
120
165
|
for (const subPackage of allSubPackages) {
|
|
121
166
|
if (normalizedPath.startsWith(`${subPackage.root}/`)) {
|
|
@@ -123,11 +168,6 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
|
|
|
123
168
|
break;
|
|
124
169
|
}
|
|
125
170
|
}
|
|
126
|
-
if (!toMainSubPackage) {
|
|
127
|
-
if (!normalizedPath.startsWith(`${rootPackageName}/`)) {
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
171
|
} else {
|
|
132
172
|
toMainSubPackage = false;
|
|
133
173
|
}
|
|
@@ -155,9 +195,45 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
|
|
|
155
195
|
const requirePath = ap.node.arguments[0].value;
|
|
156
196
|
if (requirePath) {
|
|
157
197
|
const requireFullPath = path.resolve(modulePath, `../${requirePath}`);
|
|
158
|
-
|
|
198
|
+
let id = requireFullPath.split(`${env.getWorkPath()}${path.sep}`)[1];
|
|
199
|
+
id = id.replace(/\.(js|ts)$/, "").replace(/\\/g, "/");
|
|
200
|
+
if (!id.startsWith("/")) {
|
|
201
|
+
id = "/" + id;
|
|
202
|
+
}
|
|
159
203
|
ap.node.arguments[0] = types.stringLiteral(id);
|
|
160
|
-
if (!processedModules.has(id)) {
|
|
204
|
+
if (!processedModules.has(packageName + id)) {
|
|
205
|
+
buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false, depthChain);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
ImportDeclaration(ap) {
|
|
211
|
+
const importPath = ap.node.source.value;
|
|
212
|
+
if (importPath) {
|
|
213
|
+
let id;
|
|
214
|
+
let shouldProcess = false;
|
|
215
|
+
if (importPath.startsWith("@") || importPath.startsWith("miniprogram_npm/")) {
|
|
216
|
+
if (importPath.startsWith("@")) {
|
|
217
|
+
id = `/miniprogram_npm/${importPath}`;
|
|
218
|
+
} else {
|
|
219
|
+
id = importPath.startsWith("/") ? importPath : `/${importPath}`;
|
|
220
|
+
}
|
|
221
|
+
shouldProcess = true;
|
|
222
|
+
} else if (importPath.startsWith("./") || importPath.startsWith("../") || !importPath.startsWith("/")) {
|
|
223
|
+
const importFullPath = path.resolve(modulePath, `../${importPath}`);
|
|
224
|
+
id = importFullPath.split(`${env.getWorkPath()}${path.sep}`)[1];
|
|
225
|
+
id = id.replace(/\.(js|ts)$/, "").replace(/\\/g, "/");
|
|
226
|
+
if (!id.startsWith("/")) {
|
|
227
|
+
id = "/" + id;
|
|
228
|
+
}
|
|
229
|
+
shouldProcess = true;
|
|
230
|
+
} else {
|
|
231
|
+
id = importPath;
|
|
232
|
+
shouldProcess = true;
|
|
233
|
+
}
|
|
234
|
+
if (shouldProcess) {
|
|
235
|
+
ap.node.source = types.stringLiteral(id);
|
|
236
|
+
if (!processedModules.has(packageName + id)) {
|
|
161
237
|
buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false, depthChain);
|
|
162
238
|
}
|
|
163
239
|
}
|
|
@@ -165,20 +241,34 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
|
|
|
165
241
|
}
|
|
166
242
|
});
|
|
167
243
|
const { code } = babel.transformFromAstSync(ast, "", {
|
|
168
|
-
comments: false
|
|
244
|
+
comments: false,
|
|
245
|
+
plugins: [
|
|
246
|
+
// 将 ES6 import/export 转换为 CommonJS
|
|
247
|
+
transformModulesCommonjs
|
|
248
|
+
]
|
|
169
249
|
});
|
|
170
250
|
compileInfo.code = code;
|
|
171
|
-
processedModules.add(currentPath);
|
|
251
|
+
processedModules.add(packageName + currentPath);
|
|
172
252
|
}
|
|
173
253
|
function getExtraInfoStatement(type, addedArgs) {
|
|
174
254
|
const propertyAssignment = types.objectProperty(types.identifier("__extraInfo"), addedArgs);
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
propertyAssignment.value
|
|
180
|
-
)
|
|
255
|
+
const assignmentExpression = types.assignmentExpression(
|
|
256
|
+
"=",
|
|
257
|
+
types.memberExpression(types.identifier("globalThis"), propertyAssignment.key),
|
|
258
|
+
propertyAssignment.value
|
|
181
259
|
);
|
|
182
|
-
return
|
|
260
|
+
return types.expressionStatement(assignmentExpression);
|
|
261
|
+
}
|
|
262
|
+
function getJSAbsolutePath(modulePath) {
|
|
263
|
+
const workPath = env.getWorkPath();
|
|
264
|
+
const fileTypes = [".js", ".ts"];
|
|
265
|
+
for (const ext of fileTypes) {
|
|
266
|
+
const fullPath = `${workPath}${modulePath}${ext}`;
|
|
267
|
+
if (fs.existsSync(fullPath)) {
|
|
268
|
+
return fullPath;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return null;
|
|
183
272
|
}
|
|
184
|
-
|
|
273
|
+
exports.buildJSByPath = buildJSByPath;
|
|
274
|
+
exports.compileJS = compileJS;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
|
-
import { resolve } from "node:path";
|
|
2
|
+
import { resolve, sep } from "node:path";
|
|
3
3
|
import { isMainThread, parentPort } from "node:worker_threads";
|
|
4
4
|
import babel from "@babel/core";
|
|
5
|
-
import types from "@babel/types";
|
|
6
5
|
import _traverse from "@babel/traverse";
|
|
6
|
+
import types from "@babel/types";
|
|
7
7
|
import { transform } from "esbuild";
|
|
8
|
-
import
|
|
8
|
+
import ts from "typescript";
|
|
9
|
+
import transformModulesCommonjs from "@babel/plugin-transform-modules-commonjs";
|
|
10
|
+
import { r as resetStoreInfo, g as getTargetPath, i as hasCompileInfo, b as getContentByPath, j as getAppConfigInfo, a as getComponent, d as getWorkPath } from "../env-Csj3AHY4.js";
|
|
9
11
|
const traverse = _traverse.default ? _traverse.default : _traverse;
|
|
10
12
|
const processedModules = /* @__PURE__ */ new Set();
|
|
11
13
|
if (!isMainThread) {
|
|
@@ -24,18 +26,33 @@ if (!isMainThread) {
|
|
|
24
26
|
};
|
|
25
27
|
const mainCompileRes = await compileJS(pages.mainPages, null, null, progress);
|
|
26
28
|
for (const [root, subPages] of Object.entries(pages.subPages)) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
try {
|
|
30
|
+
const subCompileRes = await compileJS(
|
|
31
|
+
subPages.info,
|
|
32
|
+
root,
|
|
33
|
+
subPages.independent ? [] : mainCompileRes,
|
|
34
|
+
progress
|
|
35
|
+
);
|
|
36
|
+
await writeCompileRes(subCompileRes, root);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new Error(`Error processing subpackage ${root}: ${error.message}
|
|
39
|
+
${error.stack}`);
|
|
40
|
+
}
|
|
34
41
|
}
|
|
35
42
|
await writeCompileRes(mainCompileRes, null);
|
|
36
43
|
parentPort.postMessage({ success: true });
|
|
37
44
|
} catch (error) {
|
|
38
|
-
parentPort.postMessage({
|
|
45
|
+
parentPort.postMessage({
|
|
46
|
+
success: false,
|
|
47
|
+
error: {
|
|
48
|
+
message: error.message,
|
|
49
|
+
stack: error.stack,
|
|
50
|
+
name: error.name,
|
|
51
|
+
file: error.file || null,
|
|
52
|
+
line: error.line || null,
|
|
53
|
+
code: error.code || null
|
|
54
|
+
}
|
|
55
|
+
});
|
|
39
56
|
}
|
|
40
57
|
});
|
|
41
58
|
}
|
|
@@ -62,7 +79,7 @@ ${module.code}
|
|
|
62
79
|
} else {
|
|
63
80
|
const mainDir = `${getTargetPath()}/main`;
|
|
64
81
|
if (!fs.existsSync(mainDir)) {
|
|
65
|
-
fs.mkdirSync(mainDir);
|
|
82
|
+
fs.mkdirSync(mainDir, { recursive: true });
|
|
66
83
|
}
|
|
67
84
|
fs.writeFileSync(`${mainDir}/logic.js`, mergeCode);
|
|
68
85
|
}
|
|
@@ -81,10 +98,12 @@ async function compileJS(pages, root, mainCompileRes, progress) {
|
|
|
81
98
|
function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra, depthChain = [], putMain = false) {
|
|
82
99
|
const currentPath = module.path;
|
|
83
100
|
if (depthChain.includes(currentPath)) {
|
|
84
|
-
console.warn(`检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
101
|
+
console.warn("[logic]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
102
|
+
return;
|
|
85
103
|
}
|
|
86
|
-
if (depthChain.length >
|
|
87
|
-
console.warn(`检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
104
|
+
if (depthChain.length > 20) {
|
|
105
|
+
console.warn("[logic]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
106
|
+
return;
|
|
88
107
|
}
|
|
89
108
|
depthChain = [...depthChain, currentPath];
|
|
90
109
|
if (!module.path) {
|
|
@@ -98,9 +117,35 @@ function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra
|
|
|
98
117
|
code: ""
|
|
99
118
|
};
|
|
100
119
|
const src = module.path.startsWith("/") ? module.path : `/${module.path}`;
|
|
101
|
-
const modulePath =
|
|
102
|
-
|
|
103
|
-
|
|
120
|
+
const modulePath = getJSAbsolutePath(src);
|
|
121
|
+
if (!modulePath) {
|
|
122
|
+
console.warn("[logic]", `找不到模块文件: ${src}`);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const sourceCode = getContentByPath(modulePath);
|
|
126
|
+
if (!sourceCode) {
|
|
127
|
+
console.warn("[logic]", `无法读取模块文件: ${modulePath}`);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
let jsCode = sourceCode;
|
|
131
|
+
if (modulePath.endsWith(".ts")) {
|
|
132
|
+
try {
|
|
133
|
+
const result = ts.transpileModule(sourceCode, {
|
|
134
|
+
compilerOptions: {
|
|
135
|
+
target: ts.ScriptTarget.ES2020,
|
|
136
|
+
module: ts.ModuleKind.CommonJS,
|
|
137
|
+
strict: false,
|
|
138
|
+
esModuleInterop: true,
|
|
139
|
+
skipLibCheck: true
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
jsCode = result.outputText;
|
|
143
|
+
} catch (error) {
|
|
144
|
+
console.error(`[logic] TypeScript 编译失败 ${modulePath}:`, error.message);
|
|
145
|
+
jsCode = sourceCode;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const ast = babel.parseSync(jsCode);
|
|
104
149
|
const addedArgs = types.objectExpression([
|
|
105
150
|
types.objectProperty(types.identifier("path"), types.stringLiteral(module.path))
|
|
106
151
|
]);
|
|
@@ -114,7 +159,6 @@ function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra
|
|
|
114
159
|
for (const [name, path] of Object.entries(module.usingComponents)) {
|
|
115
160
|
let toMainSubPackage = true;
|
|
116
161
|
if (packageName) {
|
|
117
|
-
const rootPackageName = packageName.startsWith("sub_") ? packageName.slice(4) : packageName;
|
|
118
162
|
const normalizedPath = path.startsWith("/") ? path.substring(1) : path;
|
|
119
163
|
for (const subPackage of allSubPackages) {
|
|
120
164
|
if (normalizedPath.startsWith(`${subPackage.root}/`)) {
|
|
@@ -122,11 +166,6 @@ function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra
|
|
|
122
166
|
break;
|
|
123
167
|
}
|
|
124
168
|
}
|
|
125
|
-
if (!toMainSubPackage) {
|
|
126
|
-
if (!normalizedPath.startsWith(`${rootPackageName}/`)) {
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
169
|
} else {
|
|
131
170
|
toMainSubPackage = false;
|
|
132
171
|
}
|
|
@@ -154,9 +193,45 @@ function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra
|
|
|
154
193
|
const requirePath = ap.node.arguments[0].value;
|
|
155
194
|
if (requirePath) {
|
|
156
195
|
const requireFullPath = resolve(modulePath, `../${requirePath}`);
|
|
157
|
-
|
|
196
|
+
let id = requireFullPath.split(`${getWorkPath()}${sep}`)[1];
|
|
197
|
+
id = id.replace(/\.(js|ts)$/, "").replace(/\\/g, "/");
|
|
198
|
+
if (!id.startsWith("/")) {
|
|
199
|
+
id = "/" + id;
|
|
200
|
+
}
|
|
158
201
|
ap.node.arguments[0] = types.stringLiteral(id);
|
|
159
|
-
if (!processedModules.has(id)) {
|
|
202
|
+
if (!processedModules.has(packageName + id)) {
|
|
203
|
+
buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false, depthChain);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
ImportDeclaration(ap) {
|
|
209
|
+
const importPath = ap.node.source.value;
|
|
210
|
+
if (importPath) {
|
|
211
|
+
let id;
|
|
212
|
+
let shouldProcess = false;
|
|
213
|
+
if (importPath.startsWith("@") || importPath.startsWith("miniprogram_npm/")) {
|
|
214
|
+
if (importPath.startsWith("@")) {
|
|
215
|
+
id = `/miniprogram_npm/${importPath}`;
|
|
216
|
+
} else {
|
|
217
|
+
id = importPath.startsWith("/") ? importPath : `/${importPath}`;
|
|
218
|
+
}
|
|
219
|
+
shouldProcess = true;
|
|
220
|
+
} else if (importPath.startsWith("./") || importPath.startsWith("../") || !importPath.startsWith("/")) {
|
|
221
|
+
const importFullPath = resolve(modulePath, `../${importPath}`);
|
|
222
|
+
id = importFullPath.split(`${getWorkPath()}${sep}`)[1];
|
|
223
|
+
id = id.replace(/\.(js|ts)$/, "").replace(/\\/g, "/");
|
|
224
|
+
if (!id.startsWith("/")) {
|
|
225
|
+
id = "/" + id;
|
|
226
|
+
}
|
|
227
|
+
shouldProcess = true;
|
|
228
|
+
} else {
|
|
229
|
+
id = importPath;
|
|
230
|
+
shouldProcess = true;
|
|
231
|
+
}
|
|
232
|
+
if (shouldProcess) {
|
|
233
|
+
ap.node.source = types.stringLiteral(id);
|
|
234
|
+
if (!processedModules.has(packageName + id)) {
|
|
160
235
|
buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false, depthChain);
|
|
161
236
|
}
|
|
162
237
|
}
|
|
@@ -164,22 +239,36 @@ function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra
|
|
|
164
239
|
}
|
|
165
240
|
});
|
|
166
241
|
const { code } = babel.transformFromAstSync(ast, "", {
|
|
167
|
-
comments: false
|
|
242
|
+
comments: false,
|
|
243
|
+
plugins: [
|
|
244
|
+
// 将 ES6 import/export 转换为 CommonJS
|
|
245
|
+
transformModulesCommonjs
|
|
246
|
+
]
|
|
168
247
|
});
|
|
169
248
|
compileInfo.code = code;
|
|
170
|
-
processedModules.add(currentPath);
|
|
249
|
+
processedModules.add(packageName + currentPath);
|
|
171
250
|
}
|
|
172
251
|
function getExtraInfoStatement(type, addedArgs) {
|
|
173
252
|
const propertyAssignment = types.objectProperty(types.identifier("__extraInfo"), addedArgs);
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
propertyAssignment.value
|
|
179
|
-
)
|
|
253
|
+
const assignmentExpression = types.assignmentExpression(
|
|
254
|
+
"=",
|
|
255
|
+
types.memberExpression(types.identifier("globalThis"), propertyAssignment.key),
|
|
256
|
+
propertyAssignment.value
|
|
180
257
|
);
|
|
181
|
-
return
|
|
258
|
+
return types.expressionStatement(assignmentExpression);
|
|
259
|
+
}
|
|
260
|
+
function getJSAbsolutePath(modulePath) {
|
|
261
|
+
const workPath = getWorkPath();
|
|
262
|
+
const fileTypes = [".js", ".ts"];
|
|
263
|
+
for (const ext of fileTypes) {
|
|
264
|
+
const fullPath = `${workPath}${modulePath}${ext}`;
|
|
265
|
+
if (fs.existsSync(fullPath)) {
|
|
266
|
+
return fullPath;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return null;
|
|
182
270
|
}
|
|
183
271
|
export {
|
|
184
|
-
|
|
272
|
+
buildJSByPath,
|
|
273
|
+
compileJS
|
|
185
274
|
};
|