@dimina/compiler 1.0.12-beta.9 → 1.0.12
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 +1 -1
- package/dist/bin/index.js +1 -1
- package/dist/core/logic-compiler.cjs +178 -83
- package/dist/core/logic-compiler.js +179 -84
- package/dist/core/style-compiler.cjs +28 -6
- package/dist/core/style-compiler.js +30 -8
- package/dist/core/view-compiler.cjs +3403 -3588
- package/dist/core/view-compiler.js +3404 -3589
- package/dist/{env-Csj3AHY4.js → env-QQjdhY-G.js} +7 -7
- package/dist/index.cjs +22 -8
- package/dist/index.js +23 -9
- package/package.json +21 -17
package/dist/bin/index.cjs
CHANGED
package/dist/bin/index.js
CHANGED
|
@@ -3,23 +3,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const fs = require("node:fs");
|
|
4
4
|
const path = require("node:path");
|
|
5
5
|
const node_worker_threads = require("node:worker_threads");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
6
|
+
const oxcParser = require("oxc-parser");
|
|
7
|
+
const oxcWalker = require("oxc-walker");
|
|
8
|
+
const oxcTransform = require("oxc-transform");
|
|
9
|
+
const MagicString = require("magic-string");
|
|
9
10
|
const esbuild = require("esbuild");
|
|
10
11
|
const ts = require("typescript");
|
|
11
|
-
const transformModulesCommonjs = require("@babel/plugin-transform-modules-commonjs");
|
|
12
12
|
const env = require("../env-Cmen1qwy.cjs");
|
|
13
|
-
const traverse = _traverse.default ? _traverse.default : _traverse;
|
|
14
|
-
const BABEL_TRANSFORM_CONFIG = {
|
|
15
|
-
comments: false,
|
|
16
|
-
configFile: false,
|
|
17
|
-
babelrc: false,
|
|
18
|
-
plugins: [
|
|
19
|
-
// 将 ES6 import/export 转换为 CommonJS
|
|
20
|
-
transformModulesCommonjs
|
|
21
|
-
]
|
|
22
|
-
};
|
|
23
13
|
const processedModules = /* @__PURE__ */ new Set();
|
|
24
14
|
if (!node_worker_threads.isMainThread) {
|
|
25
15
|
node_worker_threads.parentPort.on("message", async ({ pages, storeInfo }) => {
|
|
@@ -97,15 +87,15 @@ ${module2.code}
|
|
|
97
87
|
async function compileJS(pages, root, mainCompileRes, progress) {
|
|
98
88
|
const compileRes = [];
|
|
99
89
|
if (!root) {
|
|
100
|
-
buildJSByPath(root, { path: "app" }, compileRes, mainCompileRes, false);
|
|
90
|
+
await buildJSByPath(root, { path: "app" }, compileRes, mainCompileRes, false);
|
|
101
91
|
}
|
|
102
|
-
|
|
103
|
-
buildJSByPath(root, page, compileRes, mainCompileRes, true);
|
|
92
|
+
for (const page of pages) {
|
|
93
|
+
await buildJSByPath(root, page, compileRes, mainCompileRes, true);
|
|
104
94
|
progress.completedTasks++;
|
|
105
|
-
}
|
|
95
|
+
}
|
|
106
96
|
return compileRes;
|
|
107
97
|
}
|
|
108
|
-
function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtra, depthChain = [], putMain = false) {
|
|
98
|
+
async function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtra, depthChain = [], putMain = false) {
|
|
109
99
|
const currentPath = module2.path;
|
|
110
100
|
if (depthChain.includes(currentPath)) {
|
|
111
101
|
console.warn("[logic]", `检测到循环依赖: ${[...depthChain, currentPath].join(" -> ")}`);
|
|
@@ -143,7 +133,8 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
|
|
|
143
133
|
const result = ts.transpileModule(sourceCode, {
|
|
144
134
|
compilerOptions: {
|
|
145
135
|
target: ts.ScriptTarget.ES2020,
|
|
146
|
-
module: ts.ModuleKind.
|
|
136
|
+
module: ts.ModuleKind.ESNext,
|
|
137
|
+
// 保持 ES6 模块语法,让 oxc-transform 后续处理
|
|
147
138
|
strict: false,
|
|
148
139
|
esModuleInterop: true,
|
|
149
140
|
skipLibCheck: true
|
|
@@ -155,16 +146,20 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
|
|
|
155
146
|
jsCode = sourceCode;
|
|
156
147
|
}
|
|
157
148
|
}
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
+
};
|
|
162
158
|
if (module2.component) {
|
|
163
|
-
|
|
164
|
-
addedArgs.properties.push(component);
|
|
159
|
+
extraInfo.component = true;
|
|
165
160
|
}
|
|
166
161
|
if (module2.usingComponents) {
|
|
167
|
-
const
|
|
162
|
+
const componentsObj = {};
|
|
168
163
|
const allSubPackages = env.getAppConfigInfo().subPackages;
|
|
169
164
|
for (const [name, path2] of Object.entries(module2.usingComponents)) {
|
|
170
165
|
let toMainSubPackage = true;
|
|
@@ -183,84 +178,184 @@ function buildJSByPath(packageName, module2, compileRes, mainCompileRes, addExtr
|
|
|
183
178
|
if (!componentModule) {
|
|
184
179
|
continue;
|
|
185
180
|
}
|
|
186
|
-
buildJSByPath(packageName, componentModule, compileRes, mainCompileRes, true, depthChain, toMainSubPackage);
|
|
187
|
-
|
|
188
|
-
components.value.properties.push(props);
|
|
181
|
+
await buildJSByPath(packageName, componentModule, compileRes, mainCompileRes, true, depthChain, toMainSubPackage);
|
|
182
|
+
componentsObj[name] = path2;
|
|
189
183
|
}
|
|
190
|
-
|
|
184
|
+
extraInfo.usingComponents = componentsObj;
|
|
191
185
|
}
|
|
192
186
|
if (addExtra) {
|
|
193
|
-
|
|
187
|
+
const extraInfoCode = `globalThis.__extraInfo = ${JSON.stringify(extraInfo)};
|
|
188
|
+
`;
|
|
189
|
+
s.prepend(extraInfoCode);
|
|
194
190
|
}
|
|
195
191
|
if (putMain) {
|
|
196
192
|
mainCompileRes.push(compileInfo);
|
|
197
193
|
} else {
|
|
198
194
|
compileRes.push(compileInfo);
|
|
199
195
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
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
|
+
}
|
|
214
232
|
}
|
|
215
233
|
}
|
|
216
234
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
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;
|
|
226
255
|
} else {
|
|
227
|
-
id = importPath
|
|
256
|
+
id = importPath;
|
|
257
|
+
shouldProcess = true;
|
|
228
258
|
}
|
|
229
|
-
shouldProcess
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
+
}
|
|
236
268
|
}
|
|
237
|
-
shouldProcess = true;
|
|
238
|
-
} else {
|
|
239
|
-
id = importPath;
|
|
240
|
-
shouldProcess = true;
|
|
241
269
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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
|
+
}
|
|
246
340
|
}
|
|
247
341
|
}
|
|
248
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;
|
|
249
352
|
}
|
|
250
|
-
})
|
|
251
|
-
|
|
252
|
-
|
|
353
|
+
} catch (error) {
|
|
354
|
+
console.error(`[logic] esbuild 转换失败 ${modulePath}:`, error.message);
|
|
355
|
+
compileInfo.code = transformedCode;
|
|
356
|
+
}
|
|
253
357
|
processedModules.add(packageName + currentPath);
|
|
254
358
|
}
|
|
255
|
-
function getExtraInfoStatement(type, addedArgs) {
|
|
256
|
-
const propertyAssignment = types.objectProperty(types.identifier("__extraInfo"), addedArgs);
|
|
257
|
-
const assignmentExpression = types.assignmentExpression(
|
|
258
|
-
"=",
|
|
259
|
-
types.memberExpression(types.identifier("globalThis"), propertyAssignment.key),
|
|
260
|
-
propertyAssignment.value
|
|
261
|
-
);
|
|
262
|
-
return types.expressionStatement(assignmentExpression);
|
|
263
|
-
}
|
|
264
359
|
function getJSAbsolutePath(modulePath) {
|
|
265
360
|
const workPath = env.getWorkPath();
|
|
266
361
|
const fileTypes = [".js", ".ts"];
|