@dimina/compiler 1.0.12 → 1.0.13
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/dist/bin/index.cjs +100 -29
- package/dist/bin/index.js +91 -23
- package/dist/core/logic-compiler.cjs +351 -361
- package/dist/core/logic-compiler.js +340 -355
- package/dist/core/style-compiler.cjs +220 -228
- package/dist/core/style-compiler.js +200 -206
- package/dist/core/view-compiler.cjs +7254 -8276
- package/dist/core/view-compiler.js +7256 -8253
- package/dist/env-COaZNCzL.js +537 -0
- package/dist/env-lm96AacS.cjs +689 -0
- package/dist/index.cjs +2 -476
- package/dist/index.js +2 -476
- package/dist/src-D9P-SZeX.js +431 -0
- package/dist/src-DycLk1BL.cjs +440 -0
- package/package.json +10 -10
- package/dist/env-Cmen1qwy.cjs +0 -543
- package/dist/env-QQjdhY-G.js +0 -544
|
@@ -1,371 +1,361 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
stack: error.stack,
|
|
53
|
-
name: error.name
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
}
|
|
2
|
+
const require_env = require("../env-lm96AacS.cjs");
|
|
3
|
+
let node_path = require("node:path");
|
|
4
|
+
let node_worker_threads = require("node:worker_threads");
|
|
5
|
+
let node_fs = require("node:fs");
|
|
6
|
+
node_fs = require_env.__toESM(node_fs);
|
|
7
|
+
let esbuild = require("esbuild");
|
|
8
|
+
let oxc_parser = require("oxc-parser");
|
|
9
|
+
let oxc_walker = require("oxc-walker");
|
|
10
|
+
let oxc_transform = require("oxc-transform");
|
|
11
|
+
let magic_string = require("magic-string");
|
|
12
|
+
magic_string = require_env.__toESM(magic_string);
|
|
13
|
+
let typescript = require("typescript");
|
|
14
|
+
typescript = require_env.__toESM(typescript);
|
|
15
|
+
//#region src/core/logic-compiler.js
|
|
16
|
+
var processedModules = /* @__PURE__ */ new Set();
|
|
17
|
+
if (!node_worker_threads.isMainThread) node_worker_threads.parentPort.on("message", async ({ pages, storeInfo }) => {
|
|
18
|
+
try {
|
|
19
|
+
require_env.resetStoreInfo(storeInfo);
|
|
20
|
+
const progress = {
|
|
21
|
+
_completedTasks: 0,
|
|
22
|
+
get completedTasks() {
|
|
23
|
+
return this._completedTasks;
|
|
24
|
+
},
|
|
25
|
+
set completedTasks(value) {
|
|
26
|
+
this._completedTasks = value;
|
|
27
|
+
node_worker_threads.parentPort.postMessage({ completedTasks: this.completedTasks });
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const mainCompileRes = await compileJS(pages.mainPages, null, null, progress);
|
|
31
|
+
for (const [root, subPages] of Object.entries(pages.subPages)) try {
|
|
32
|
+
await writeCompileRes(await compileJS(subPages.info, root, subPages.independent ? [] : mainCompileRes, progress), root);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
throw new Error(`Error processing subpackage ${root}: ${error.message}\n${error.stack}`);
|
|
35
|
+
}
|
|
36
|
+
await writeCompileRes(mainCompileRes, null);
|
|
37
|
+
processedModules.clear();
|
|
38
|
+
node_worker_threads.parentPort.postMessage({ success: true });
|
|
39
|
+
} catch (error) {
|
|
40
|
+
processedModules.clear();
|
|
41
|
+
node_worker_threads.parentPort.postMessage({
|
|
42
|
+
success: false,
|
|
43
|
+
error: {
|
|
44
|
+
message: error.message,
|
|
45
|
+
stack: error.stack,
|
|
46
|
+
name: error.name
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
59
51
|
async function writeCompileRes(compileRes, root) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
${
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const mainDir = `${env.getTargetPath()}/main`;
|
|
81
|
-
if (!fs.existsSync(mainDir)) {
|
|
82
|
-
fs.mkdirSync(mainDir, { recursive: true });
|
|
83
|
-
}
|
|
84
|
-
fs.writeFileSync(`${mainDir}/logic.js`, mergeCode);
|
|
85
|
-
}
|
|
52
|
+
let mergeCode = "";
|
|
53
|
+
for (const module of compileRes) {
|
|
54
|
+
const { code: minifiedCode } = await (0, esbuild.transform)(`modDefine('${module.path}', function(require, module, exports) {
|
|
55
|
+
${module.code}
|
|
56
|
+
});`, {
|
|
57
|
+
minify: true,
|
|
58
|
+
target: ["es2023"],
|
|
59
|
+
platform: "neutral"
|
|
60
|
+
});
|
|
61
|
+
mergeCode += minifiedCode;
|
|
62
|
+
}
|
|
63
|
+
if (root) {
|
|
64
|
+
const subDir = `${require_env.getTargetPath()}/${root}`;
|
|
65
|
+
if (!node_fs.default.existsSync(subDir)) node_fs.default.mkdirSync(subDir, { recursive: true });
|
|
66
|
+
node_fs.default.writeFileSync(`${subDir}/logic.js`, mergeCode);
|
|
67
|
+
} else {
|
|
68
|
+
const mainDir = `${require_env.getTargetPath()}/main`;
|
|
69
|
+
if (!node_fs.default.existsSync(mainDir)) node_fs.default.mkdirSync(mainDir, { recursive: true });
|
|
70
|
+
node_fs.default.writeFileSync(`${mainDir}/logic.js`, mergeCode);
|
|
71
|
+
}
|
|
86
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* 编译 js 文件
|
|
75
|
+
*/
|
|
87
76
|
async function compileJS(pages, root, mainCompileRes, progress) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
77
|
+
const compileRes = [];
|
|
78
|
+
if (!root) await buildJSByPath(root, { path: "app" }, compileRes, mainCompileRes, false);
|
|
79
|
+
for (const page of pages) {
|
|
80
|
+
await buildJSByPath(root, page, compileRes, mainCompileRes, true);
|
|
81
|
+
progress.completedTasks++;
|
|
82
|
+
}
|
|
83
|
+
return compileRes;
|
|
84
|
+
}
|
|
85
|
+
async function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra, depthChain = [], putMain = false) {
|
|
86
|
+
const currentPath = module.path;
|
|
87
|
+
if (depthChain.includes(currentPath)) {
|
|
88
|
+
console.warn("[logic]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (depthChain.length > 20) {
|
|
92
|
+
console.warn("[logic]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
depthChain = [...depthChain, currentPath];
|
|
96
|
+
if (!module.path) return;
|
|
97
|
+
if (require_env.hasCompileInfo(module.path, compileRes, mainCompileRes)) return;
|
|
98
|
+
const compileInfo = {
|
|
99
|
+
path: module.path,
|
|
100
|
+
code: ""
|
|
101
|
+
};
|
|
102
|
+
const src = module.path.startsWith("/") ? module.path : `/${module.path}`;
|
|
103
|
+
const modulePath = getJSAbsolutePath(src);
|
|
104
|
+
if (!modulePath) {
|
|
105
|
+
console.warn("[logic]", `找不到模块文件: ${src}`);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const sourceCode = require_env.getContentByPath(modulePath);
|
|
109
|
+
if (!sourceCode) {
|
|
110
|
+
console.warn("[logic]", `无法读取模块文件: ${modulePath}`);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
let jsCode = sourceCode;
|
|
114
|
+
if (modulePath.endsWith(".ts")) try {
|
|
115
|
+
jsCode = typescript.default.transpileModule(sourceCode, { compilerOptions: {
|
|
116
|
+
target: typescript.default.ScriptTarget.ES2020,
|
|
117
|
+
module: typescript.default.ModuleKind.ESNext,
|
|
118
|
+
strict: false,
|
|
119
|
+
esModuleInterop: true,
|
|
120
|
+
skipLibCheck: true
|
|
121
|
+
} }).outputText;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
console.error(`[logic] TypeScript 编译失败 ${modulePath}:`, error.message);
|
|
124
|
+
jsCode = sourceCode;
|
|
125
|
+
}
|
|
126
|
+
const ast = (0, oxc_parser.parseSync)(modulePath, jsCode, {
|
|
127
|
+
sourceType: "module",
|
|
128
|
+
lang: modulePath.endsWith(".ts") ? "ts" : "js"
|
|
129
|
+
}).program;
|
|
130
|
+
const s = new magic_string.default(jsCode);
|
|
131
|
+
const extraInfo = { path: module.path };
|
|
132
|
+
if (module.component) extraInfo.component = true;
|
|
133
|
+
if (module.usingComponents) {
|
|
134
|
+
const componentsObj = {};
|
|
135
|
+
const allSubPackages = require_env.getAppConfigInfo().subPackages;
|
|
136
|
+
for (const [name, path] of Object.entries(module.usingComponents)) {
|
|
137
|
+
let toMainSubPackage = true;
|
|
138
|
+
if (packageName) {
|
|
139
|
+
const normalizedPath = path.startsWith("/") ? path.substring(1) : path;
|
|
140
|
+
for (const subPackage of allSubPackages) if (normalizedPath.startsWith(`${subPackage.root}/`)) {
|
|
141
|
+
toMainSubPackage = false;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
} else toMainSubPackage = false;
|
|
145
|
+
const componentModule = require_env.getComponent(path);
|
|
146
|
+
if (!componentModule) continue;
|
|
147
|
+
await buildJSByPath(packageName, componentModule, compileRes, mainCompileRes, true, depthChain, putMain || toMainSubPackage);
|
|
148
|
+
componentsObj[name] = path;
|
|
149
|
+
}
|
|
150
|
+
extraInfo.usingComponents = componentsObj;
|
|
151
|
+
}
|
|
152
|
+
if (addExtra) {
|
|
153
|
+
const extraInfoCode = `globalThis.__extraInfo = ${JSON.stringify(extraInfo)};\n`;
|
|
154
|
+
s.prepend(extraInfoCode);
|
|
155
|
+
}
|
|
156
|
+
if (putMain) mainCompileRes.push(compileInfo);
|
|
157
|
+
else compileRes.push(compileInfo);
|
|
158
|
+
const pathReplacements = [];
|
|
159
|
+
const dependenciesToProcess = [];
|
|
160
|
+
(0, oxc_walker.walk)(ast, { enter(node, parent) {
|
|
161
|
+
if ((node.type === "StringLiteral" || node.type === "Literal") && isLocalAssetString(node.value)) pathReplacements.push({
|
|
162
|
+
start: node.start,
|
|
163
|
+
end: node.end,
|
|
164
|
+
newValue: require_env.collectAssets(require_env.getWorkPath(), modulePath, node.value, require_env.getTargetPath(), require_env.getAppId())
|
|
165
|
+
});
|
|
166
|
+
if (node.type === "CallExpression") {
|
|
167
|
+
const isRequire = node.callee.type === "Identifier" && node.callee.name === "require";
|
|
168
|
+
const isRequireProperty = node.callee.type === "MemberExpression" && node.callee.object?.type === "Identifier" && node.callee.object?.name === "require";
|
|
169
|
+
if ((isRequire || isRequireProperty) && node.arguments.length > 0 && (node.arguments[0].type === "StringLiteral" || node.arguments[0].type === "Literal")) {
|
|
170
|
+
const arg = node.arguments[0];
|
|
171
|
+
const requirePath = arg.value;
|
|
172
|
+
if (requirePath) {
|
|
173
|
+
const { id, shouldProcess } = resolveDependencyId(requirePath, modulePath, false);
|
|
174
|
+
if (shouldProcess) {
|
|
175
|
+
pathReplacements.push({
|
|
176
|
+
start: arg.start,
|
|
177
|
+
end: arg.end,
|
|
178
|
+
newValue: id
|
|
179
|
+
});
|
|
180
|
+
if (!processedModules.has(packageName + id)) dependenciesToProcess.push(id);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (node.type === "ImportDeclaration") {
|
|
186
|
+
const importPath = node.source.value;
|
|
187
|
+
if (importPath) {
|
|
188
|
+
const { id, shouldProcess } = resolveDependencyId(importPath, modulePath, true);
|
|
189
|
+
if (shouldProcess) {
|
|
190
|
+
pathReplacements.push({
|
|
191
|
+
start: node.source.start,
|
|
192
|
+
end: node.source.end,
|
|
193
|
+
newValue: id
|
|
194
|
+
});
|
|
195
|
+
if (!processedModules.has(packageName + id)) dependenciesToProcess.push(id);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if ((node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration") && node.source) {
|
|
200
|
+
const exportPath = node.source.value;
|
|
201
|
+
if (exportPath) {
|
|
202
|
+
const { id, shouldProcess } = resolveDependencyId(exportPath, modulePath, true);
|
|
203
|
+
if (shouldProcess) {
|
|
204
|
+
pathReplacements.push({
|
|
205
|
+
start: node.source.start,
|
|
206
|
+
end: node.source.end,
|
|
207
|
+
newValue: id
|
|
208
|
+
});
|
|
209
|
+
if (!processedModules.has(packageName + id)) dependenciesToProcess.push(id);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
} });
|
|
214
|
+
for (const depId of dependenciesToProcess) await buildJSByPath(packageName, { path: depId }, compileRes, mainCompileRes, false, depthChain, putMain);
|
|
215
|
+
for (const replacement of pathReplacements.reverse()) s.overwrite(replacement.start, replacement.end, `'${replacement.newValue}'`);
|
|
216
|
+
const modifiedCode = s.toString();
|
|
217
|
+
let transformedCode = modifiedCode;
|
|
218
|
+
if (modulePath.endsWith(".ts") || modulePath.endsWith(".tsx")) try {
|
|
219
|
+
transformedCode = (0, oxc_transform.transformSync)(modulePath, modifiedCode, {
|
|
220
|
+
sourceType: "module",
|
|
221
|
+
lang: modulePath.endsWith(".tsx") ? "tsx" : "ts",
|
|
222
|
+
target: "es2020",
|
|
223
|
+
typescript: { onlyRemoveTypeImports: true }
|
|
224
|
+
}).code;
|
|
225
|
+
} catch (error) {
|
|
226
|
+
console.error(`[logic] oxc-transform 转换失败 ${modulePath}:`, error.message);
|
|
227
|
+
transformedCode = modifiedCode;
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
const esbuildCode = (await (0, esbuild.transform)(transformedCode, {
|
|
231
|
+
format: "cjs",
|
|
232
|
+
target: "es2020",
|
|
233
|
+
platform: "neutral",
|
|
234
|
+
loader: "js"
|
|
235
|
+
})).code;
|
|
236
|
+
const esbuildAst = (0, oxc_parser.parseSync)(modulePath, esbuildCode, {
|
|
237
|
+
sourceType: "module",
|
|
238
|
+
lang: "js"
|
|
239
|
+
});
|
|
240
|
+
const postEsbuildReplacements = [];
|
|
241
|
+
(0, oxc_walker.walk)(esbuildAst.program, { enter(node) {
|
|
242
|
+
if (node.type === "CallExpression") {
|
|
243
|
+
const isRequire = node.callee.type === "Identifier" && node.callee.name === "require";
|
|
244
|
+
const isRequireProperty = node.callee.type === "MemberExpression" && node.callee.object?.type === "Identifier" && node.callee.object?.name === "require";
|
|
245
|
+
if ((isRequire || isRequireProperty) && node.arguments.length > 0 && (node.arguments[0].type === "StringLiteral" || node.arguments[0].type === "Literal")) {
|
|
246
|
+
const arg = node.arguments[0];
|
|
247
|
+
const requirePath = arg.value;
|
|
248
|
+
if (requirePath && (requirePath.startsWith("./") || requirePath.startsWith("../"))) {
|
|
249
|
+
const { id } = resolveDependencyId(requirePath, modulePath, false);
|
|
250
|
+
postEsbuildReplacements.push({
|
|
251
|
+
start: arg.start,
|
|
252
|
+
end: arg.end,
|
|
253
|
+
newValue: id
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
} });
|
|
259
|
+
if (postEsbuildReplacements.length > 0) {
|
|
260
|
+
const finalMagicString = new magic_string.default(esbuildCode);
|
|
261
|
+
for (const replacement of postEsbuildReplacements.reverse()) finalMagicString.overwrite(replacement.start, replacement.end, `"${replacement.newValue}"`);
|
|
262
|
+
compileInfo.code = finalMagicString.toString();
|
|
263
|
+
} else compileInfo.code = esbuildCode;
|
|
264
|
+
} catch (error) {
|
|
265
|
+
console.error(`[logic] esbuild 转换失败 ${modulePath}:`, error.message);
|
|
266
|
+
compileInfo.code = transformedCode;
|
|
267
|
+
}
|
|
268
|
+
processedModules.add(packageName + currentPath);
|
|
97
269
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (depthChain.includes(currentPath)) {
|
|
101
|
-
console.warn("[logic]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
if (depthChain.length > 20) {
|
|
105
|
-
console.warn("[logic]", `检测到深度依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
depthChain = [...depthChain, currentPath];
|
|
109
|
-
if (!module2.path) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
if (env.hasCompileInfo(module2.path, compileRes, mainCompileRes)) {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
const compileInfo = {
|
|
116
|
-
path: module2.path,
|
|
117
|
-
code: ""
|
|
118
|
-
};
|
|
119
|
-
const src = module2.path.startsWith("/") ? module2.path : `/${module2.path}`;
|
|
120
|
-
const modulePath = getJSAbsolutePath(src);
|
|
121
|
-
if (!modulePath) {
|
|
122
|
-
console.warn("[logic]", `找不到模块文件: ${src}`);
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
const sourceCode = env.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.ESNext,
|
|
137
|
-
// 保持 ES6 模块语法,让 oxc-transform 后续处理
|
|
138
|
-
strict: false,
|
|
139
|
-
esModuleInterop: true,
|
|
140
|
-
skipLibCheck: true
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
jsCode = result.outputText;
|
|
144
|
-
} catch (error) {
|
|
145
|
-
console.error(`[logic] TypeScript 编译失败 ${modulePath}:`, error.message);
|
|
146
|
-
jsCode = sourceCode;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
const parseResult = oxcParser.parseSync(modulePath, jsCode, {
|
|
150
|
-
sourceType: "module",
|
|
151
|
-
lang: modulePath.endsWith(".ts") ? "ts" : "js"
|
|
152
|
-
});
|
|
153
|
-
const ast = parseResult.program;
|
|
154
|
-
const s = new MagicString(jsCode);
|
|
155
|
-
const extraInfo = {
|
|
156
|
-
path: module2.path
|
|
157
|
-
};
|
|
158
|
-
if (module2.component) {
|
|
159
|
-
extraInfo.component = true;
|
|
160
|
-
}
|
|
161
|
-
if (module2.usingComponents) {
|
|
162
|
-
const componentsObj = {};
|
|
163
|
-
const allSubPackages = env.getAppConfigInfo().subPackages;
|
|
164
|
-
for (const [name, path2] of Object.entries(module2.usingComponents)) {
|
|
165
|
-
let toMainSubPackage = true;
|
|
166
|
-
if (packageName) {
|
|
167
|
-
const normalizedPath = path2.startsWith("/") ? path2.substring(1) : path2;
|
|
168
|
-
for (const subPackage of allSubPackages) {
|
|
169
|
-
if (normalizedPath.startsWith(`${subPackage.root}/`)) {
|
|
170
|
-
toMainSubPackage = false;
|
|
171
|
-
break;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
} else {
|
|
175
|
-
toMainSubPackage = false;
|
|
176
|
-
}
|
|
177
|
-
const componentModule = env.getComponent(path2);
|
|
178
|
-
if (!componentModule) {
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
await buildJSByPath(packageName, componentModule, compileRes, mainCompileRes, true, depthChain, toMainSubPackage);
|
|
182
|
-
componentsObj[name] = path2;
|
|
183
|
-
}
|
|
184
|
-
extraInfo.usingComponents = componentsObj;
|
|
185
|
-
}
|
|
186
|
-
if (addExtra) {
|
|
187
|
-
const extraInfoCode = `globalThis.__extraInfo = ${JSON.stringify(extraInfo)};
|
|
188
|
-
`;
|
|
189
|
-
s.prepend(extraInfoCode);
|
|
190
|
-
}
|
|
191
|
-
if (putMain) {
|
|
192
|
-
mainCompileRes.push(compileInfo);
|
|
193
|
-
} else {
|
|
194
|
-
compileRes.push(compileInfo);
|
|
195
|
-
}
|
|
196
|
-
const pathReplacements = [];
|
|
197
|
-
const dependenciesToProcess = [];
|
|
198
|
-
oxcWalker.walk(ast, {
|
|
199
|
-
enter(node, parent) {
|
|
200
|
-
if (node.type === "CallExpression") {
|
|
201
|
-
const isRequire = node.callee.type === "Identifier" && node.callee.name === "require";
|
|
202
|
-
const isRequireProperty = node.callee.type === "MemberExpression" && node.callee.object?.type === "Identifier" && node.callee.object?.name === "require";
|
|
203
|
-
if ((isRequire || isRequireProperty) && node.arguments.length > 0 && (node.arguments[0].type === "StringLiteral" || node.arguments[0].type === "Literal")) {
|
|
204
|
-
const arg = node.arguments[0];
|
|
205
|
-
const requirePath = arg.value;
|
|
206
|
-
if (requirePath) {
|
|
207
|
-
let id;
|
|
208
|
-
if (requirePath.startsWith("@") || requirePath.startsWith("miniprogram_npm/")) {
|
|
209
|
-
if (requirePath.startsWith("@")) {
|
|
210
|
-
id = `/miniprogram_npm/${requirePath}`;
|
|
211
|
-
} else {
|
|
212
|
-
id = requirePath.startsWith("/") ? requirePath : `/${requirePath}`;
|
|
213
|
-
}
|
|
214
|
-
} else {
|
|
215
|
-
const requireFullPath = path.resolve(modulePath, `../${requirePath}`);
|
|
216
|
-
id = requireFullPath.split(`${env.getWorkPath()}${path.sep}`)[1];
|
|
217
|
-
id = id.replace(/\.(js|ts)$/, "").replace(/\\/g, "/");
|
|
218
|
-
if (!id.startsWith("/")) {
|
|
219
|
-
id = "/" + id;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
{
|
|
223
|
-
pathReplacements.push({
|
|
224
|
-
start: arg.start,
|
|
225
|
-
end: arg.end,
|
|
226
|
-
newValue: id
|
|
227
|
-
});
|
|
228
|
-
if (!processedModules.has(packageName + id)) {
|
|
229
|
-
dependenciesToProcess.push(id);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
if (node.type === "ImportDeclaration") {
|
|
236
|
-
const importPath = node.source.value;
|
|
237
|
-
if (importPath) {
|
|
238
|
-
let id;
|
|
239
|
-
let shouldProcess = false;
|
|
240
|
-
if (importPath.startsWith("@") || importPath.startsWith("miniprogram_npm/")) {
|
|
241
|
-
if (importPath.startsWith("@")) {
|
|
242
|
-
id = `/miniprogram_npm/${importPath}`;
|
|
243
|
-
} else {
|
|
244
|
-
id = importPath.startsWith("/") ? importPath : `/${importPath}`;
|
|
245
|
-
}
|
|
246
|
-
shouldProcess = true;
|
|
247
|
-
} else if (importPath.startsWith("./") || importPath.startsWith("../") || !importPath.startsWith("/")) {
|
|
248
|
-
const importFullPath = path.resolve(modulePath, `../${importPath}`);
|
|
249
|
-
id = importFullPath.split(`${env.getWorkPath()}${path.sep}`)[1];
|
|
250
|
-
id = id.replace(/\.(js|ts)$/, "").replace(/\\/g, "/");
|
|
251
|
-
if (!id.startsWith("/")) {
|
|
252
|
-
id = "/" + id;
|
|
253
|
-
}
|
|
254
|
-
shouldProcess = true;
|
|
255
|
-
} else {
|
|
256
|
-
id = importPath;
|
|
257
|
-
shouldProcess = true;
|
|
258
|
-
}
|
|
259
|
-
if (shouldProcess) {
|
|
260
|
-
pathReplacements.push({
|
|
261
|
-
start: node.source.start,
|
|
262
|
-
end: node.source.end,
|
|
263
|
-
newValue: id
|
|
264
|
-
});
|
|
265
|
-
if (!processedModules.has(packageName + id)) {
|
|
266
|
-
dependenciesToProcess.push(id);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
});
|
|
273
|
-
for (const depId of dependenciesToProcess) {
|
|
274
|
-
await buildJSByPath(packageName, { path: depId }, compileRes, mainCompileRes, false, depthChain);
|
|
275
|
-
}
|
|
276
|
-
for (const replacement of pathReplacements.reverse()) {
|
|
277
|
-
s.overwrite(replacement.start, replacement.end, `'${replacement.newValue}'`);
|
|
278
|
-
}
|
|
279
|
-
const modifiedCode = s.toString();
|
|
280
|
-
let transformedCode = modifiedCode;
|
|
281
|
-
if (modulePath.endsWith(".ts") || modulePath.endsWith(".tsx")) {
|
|
282
|
-
try {
|
|
283
|
-
const result = oxcTransform.transformSync(modulePath, modifiedCode, {
|
|
284
|
-
sourceType: "module",
|
|
285
|
-
lang: modulePath.endsWith(".tsx") ? "tsx" : "ts",
|
|
286
|
-
target: "es2020",
|
|
287
|
-
typescript: {
|
|
288
|
-
onlyRemoveTypeImports: true
|
|
289
|
-
}
|
|
290
|
-
});
|
|
291
|
-
transformedCode = result.code;
|
|
292
|
-
} catch (error) {
|
|
293
|
-
console.error(`[logic] oxc-transform 转换失败 ${modulePath}:`, error.message);
|
|
294
|
-
transformedCode = modifiedCode;
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
try {
|
|
298
|
-
const esbuildResult = await esbuild.transform(transformedCode, {
|
|
299
|
-
format: "cjs",
|
|
300
|
-
target: "es2020",
|
|
301
|
-
platform: "neutral",
|
|
302
|
-
loader: "js"
|
|
303
|
-
});
|
|
304
|
-
const esbuildCode = esbuildResult.code;
|
|
305
|
-
const esbuildAst = oxcParser.parseSync(modulePath, esbuildCode, {
|
|
306
|
-
sourceType: "module",
|
|
307
|
-
lang: "js"
|
|
308
|
-
});
|
|
309
|
-
const postEsbuildReplacements = [];
|
|
310
|
-
oxcWalker.walk(esbuildAst.program, {
|
|
311
|
-
enter(node) {
|
|
312
|
-
if (node.type === "CallExpression") {
|
|
313
|
-
const isRequire = node.callee.type === "Identifier" && node.callee.name === "require";
|
|
314
|
-
const isRequireProperty = node.callee.type === "MemberExpression" && node.callee.object?.type === "Identifier" && node.callee.object?.name === "require";
|
|
315
|
-
if ((isRequire || isRequireProperty) && node.arguments.length > 0 && (node.arguments[0].type === "StringLiteral" || node.arguments[0].type === "Literal")) {
|
|
316
|
-
const arg = node.arguments[0];
|
|
317
|
-
const requirePath = arg.value;
|
|
318
|
-
if (requirePath && (requirePath.startsWith("./") || requirePath.startsWith("../"))) {
|
|
319
|
-
let id;
|
|
320
|
-
if (requirePath.startsWith("@") || requirePath.startsWith("miniprogram_npm/")) {
|
|
321
|
-
if (requirePath.startsWith("@")) {
|
|
322
|
-
id = `/miniprogram_npm/${requirePath}`;
|
|
323
|
-
} else {
|
|
324
|
-
id = requirePath.startsWith("/") ? requirePath : `/${requirePath}`;
|
|
325
|
-
}
|
|
326
|
-
} else {
|
|
327
|
-
const requireFullPath = path.resolve(modulePath, `../${requirePath}`);
|
|
328
|
-
id = requireFullPath.split(`${env.getWorkPath()}${path.sep}`)[1];
|
|
329
|
-
id = id.replace(/\.(js|ts)$/, "").replace(/\\/g, "/");
|
|
330
|
-
if (!id.startsWith("/")) {
|
|
331
|
-
id = "/" + id;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
postEsbuildReplacements.push({
|
|
335
|
-
start: arg.start,
|
|
336
|
-
end: arg.end,
|
|
337
|
-
newValue: id
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
});
|
|
344
|
-
if (postEsbuildReplacements.length > 0) {
|
|
345
|
-
const finalMagicString = new MagicString(esbuildCode);
|
|
346
|
-
for (const replacement of postEsbuildReplacements.reverse()) {
|
|
347
|
-
finalMagicString.overwrite(replacement.start, replacement.end, `"${replacement.newValue}"`);
|
|
348
|
-
}
|
|
349
|
-
compileInfo.code = finalMagicString.toString();
|
|
350
|
-
} else {
|
|
351
|
-
compileInfo.code = esbuildCode;
|
|
352
|
-
}
|
|
353
|
-
} catch (error) {
|
|
354
|
-
console.error(`[logic] esbuild 转换失败 ${modulePath}:`, error.message);
|
|
355
|
-
compileInfo.code = transformedCode;
|
|
356
|
-
}
|
|
357
|
-
processedModules.add(packageName + currentPath);
|
|
270
|
+
function isLocalAssetString(value) {
|
|
271
|
+
return typeof value === "string" && !value.startsWith("http") && !value.startsWith("//") && (value.startsWith("/") || value.startsWith("./") || value.startsWith("../")) && /\.(?:png|jpe?g|gif|svg)(?:\?.*)?$/.test(value);
|
|
358
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* 获取 JavaScript 或 TypeScript 文件的绝对路径
|
|
275
|
+
* @param {string} modulePath - 模块路径
|
|
276
|
+
* @returns {string|null} - 文件的绝对路径,如果找不到则返回 null
|
|
277
|
+
*/
|
|
359
278
|
function getJSAbsolutePath(modulePath) {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
279
|
+
const workPath = require_env.getWorkPath();
|
|
280
|
+
const resolvedModuleId = resolveModuleIdToExistingPath(modulePath);
|
|
281
|
+
if (!resolvedModuleId) return null;
|
|
282
|
+
for (const ext of [".js", ".ts"]) {
|
|
283
|
+
const fullPath = `${workPath}${resolvedModuleId}${ext}`;
|
|
284
|
+
if (node_fs.default.existsSync(fullPath)) return fullPath;
|
|
285
|
+
}
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
function resolveDependencyId(specifier, modulePath, allowAbsolute) {
|
|
289
|
+
if (!specifier) return {
|
|
290
|
+
id: specifier,
|
|
291
|
+
shouldProcess: false
|
|
292
|
+
};
|
|
293
|
+
if (specifier.startsWith("miniprogram_npm/")) {
|
|
294
|
+
const npmModuleId = normalizeModuleId(`/${specifier}`);
|
|
295
|
+
return {
|
|
296
|
+
id: resolveModuleIdToExistingPath(npmModuleId) || npmModuleId,
|
|
297
|
+
shouldProcess: true
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
if (specifier.startsWith("./") || specifier.startsWith("../")) return {
|
|
301
|
+
id: resolveRelativeModuleId(specifier, modulePath),
|
|
302
|
+
shouldProcess: true
|
|
303
|
+
};
|
|
304
|
+
if (specifier.startsWith("/")) return {
|
|
305
|
+
id: allowAbsolute ? normalizeModuleId(specifier) : resolveRelativeModuleId(specifier, modulePath),
|
|
306
|
+
shouldProcess: true
|
|
307
|
+
};
|
|
308
|
+
const aliasResolved = require_env.resolveAppAlias(specifier);
|
|
309
|
+
if (aliasResolved) return {
|
|
310
|
+
id: normalizeModuleId(aliasResolved),
|
|
311
|
+
shouldProcess: true
|
|
312
|
+
};
|
|
313
|
+
if (specifier.startsWith("@") || isBareModuleSpecifier(specifier)) {
|
|
314
|
+
const npmModuleId = resolveNpmModuleId(specifier, modulePath);
|
|
315
|
+
return {
|
|
316
|
+
id: npmModuleId || specifier,
|
|
317
|
+
shouldProcess: Boolean(npmModuleId)
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
return {
|
|
321
|
+
id: specifier,
|
|
322
|
+
shouldProcess: false
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
function isBareModuleSpecifier(specifier) {
|
|
326
|
+
return !specifier.startsWith(".") && !specifier.startsWith("/");
|
|
327
|
+
}
|
|
328
|
+
function resolveRelativeModuleId(specifier, modulePath) {
|
|
329
|
+
const relativeId = (0, node_path.resolve)(modulePath, `../${specifier}`).split(`${require_env.getWorkPath()}${node_path.sep}`)[1];
|
|
330
|
+
return normalizeModuleId(relativeId);
|
|
331
|
+
}
|
|
332
|
+
function normalizeModuleId(moduleId) {
|
|
333
|
+
let normalized = moduleId.replace(/\.(js|ts)$/, "").replace(/\\/g, "/");
|
|
334
|
+
if (!normalized.startsWith("/")) normalized = `/${normalized}`;
|
|
335
|
+
return normalized;
|
|
336
|
+
}
|
|
337
|
+
function resolveNpmModuleId(specifier, modulePath) {
|
|
338
|
+
const npmResolver = require_env.getNpmResolver();
|
|
339
|
+
if (!npmResolver) return null;
|
|
340
|
+
return npmResolver.resolveScriptModule(specifier, modulePath, resolveModuleIdToExistingPath);
|
|
341
|
+
}
|
|
342
|
+
function resolveModuleIdToExistingPath(moduleId) {
|
|
343
|
+
const normalizedModuleId = normalizeModuleId(moduleId);
|
|
344
|
+
const workPath = require_env.getWorkPath();
|
|
345
|
+
for (const ext of [".js", ".ts"]) if (node_fs.default.existsSync(`${workPath}${normalizedModuleId}${ext}`)) return normalizedModuleId;
|
|
346
|
+
for (const ext of [".js", ".ts"]) if (node_fs.default.existsSync(`${workPath}${normalizedModuleId}/index${ext}`)) return `${normalizedModuleId}/index`;
|
|
347
|
+
const packageJsonPath = `${workPath}${normalizedModuleId}/package.json`;
|
|
348
|
+
if (node_fs.default.existsSync(packageJsonPath)) try {
|
|
349
|
+
const packageInfo = JSON.parse(node_fs.default.readFileSync(packageJsonPath, "utf-8"));
|
|
350
|
+
for (const entryField of ["miniprogram", "main"]) if (typeof packageInfo[entryField] === "string" && packageInfo[entryField]) {
|
|
351
|
+
const resolvedEntry = resolveModuleIdToExistingPath(normalizeModuleId((0, node_path.resolve)(normalizedModuleId, packageInfo[entryField])));
|
|
352
|
+
if (resolvedEntry) return resolvedEntry;
|
|
353
|
+
}
|
|
354
|
+
} catch (error) {
|
|
355
|
+
console.warn("[logic]", `解析 package.json 失败: ${packageJsonPath}`, error.message);
|
|
356
|
+
}
|
|
357
|
+
return null;
|
|
369
358
|
}
|
|
359
|
+
//#endregion
|
|
370
360
|
exports.buildJSByPath = buildJSByPath;
|
|
371
361
|
exports.compileJS = compileJS;
|