@dimina/compiler 1.0.12-beta.9 → 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 +350 -265
- package/dist/core/logic-compiler.js +343 -263
- package/dist/core/style-compiler.cjs +223 -209
- package/dist/core/style-compiler.js +202 -186
- package/dist/core/view-compiler.cjs +7254 -8461
- package/dist/core/view-compiler.js +7256 -8438
- package/dist/env-COaZNCzL.js +537 -0
- package/dist/env-lm96AacS.cjs +689 -0
- package/dist/index.cjs +2 -462
- package/dist/index.js +2 -462
- package/dist/src-D9P-SZeX.js +431 -0
- package/dist/src-DycLk1BL.cjs +440 -0
- package/package.json +22 -18
- package/dist/env-Cmen1qwy.cjs +0 -543
- package/dist/env-Csj3AHY4.js +0 -544
|
@@ -1,276 +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
|
-
}
|
|
53
|
-
await writeCompileRes(mainCompileRes, null);
|
|
54
|
-
processedModules.clear();
|
|
55
|
-
node_worker_threads.parentPort.postMessage({ success: true });
|
|
56
|
-
} catch (error) {
|
|
57
|
-
processedModules.clear();
|
|
58
|
-
node_worker_threads.parentPort.postMessage({
|
|
59
|
-
success: false,
|
|
60
|
-
error: {
|
|
61
|
-
message: error.message,
|
|
62
|
-
stack: error.stack,
|
|
63
|
-
name: error.name
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
}
|
|
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
|
+
});
|
|
69
51
|
async function writeCompileRes(compileRes, root) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
${
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const mainDir = `${env.getTargetPath()}/main`;
|
|
91
|
-
if (!fs.existsSync(mainDir)) {
|
|
92
|
-
fs.mkdirSync(mainDir, { recursive: true });
|
|
93
|
-
}
|
|
94
|
-
fs.writeFileSync(`${mainDir}/logic.js`, mergeCode);
|
|
95
|
-
}
|
|
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
|
+
}
|
|
96
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* 编译 js 文件
|
|
75
|
+
*/
|
|
97
76
|
async function compileJS(pages, root, mainCompileRes, progress) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
});
|
|
106
|
-
return compileRes;
|
|
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;
|
|
107
84
|
}
|
|
108
|
-
function buildJSByPath(packageName,
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
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);
|
|
254
269
|
}
|
|
255
|
-
function
|
|
256
|
-
|
|
257
|
-
const assignmentExpression = types.assignmentExpression(
|
|
258
|
-
"=",
|
|
259
|
-
types.memberExpression(types.identifier("globalThis"), propertyAssignment.key),
|
|
260
|
-
propertyAssignment.value
|
|
261
|
-
);
|
|
262
|
-
return types.expressionStatement(assignmentExpression);
|
|
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);
|
|
263
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* 获取 JavaScript 或 TypeScript 文件的绝对路径
|
|
275
|
+
* @param {string} modulePath - 模块路径
|
|
276
|
+
* @returns {string|null} - 文件的绝对路径,如果找不到则返回 null
|
|
277
|
+
*/
|
|
264
278
|
function getJSAbsolutePath(modulePath) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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;
|
|
274
358
|
}
|
|
359
|
+
//#endregion
|
|
275
360
|
exports.buildJSByPath = buildJSByPath;
|
|
276
361
|
exports.compileJS = compileJS;
|