@dimina/compiler 1.0.12-beta.8 → 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.
@@ -5,7 +5,7 @@ const process = require("node:process");
5
5
  const chokidar = require("chokidar");
6
6
  const commander = require("commander");
7
7
  const index = require("../index.cjs");
8
- const version = "1.0.12-beta.8";
8
+ const version = "1.0.12";
9
9
  const pack = {
10
10
  version
11
11
  };
package/dist/bin/index.js CHANGED
@@ -4,7 +4,7 @@ import process from "node:process";
4
4
  import chokidar from "chokidar";
5
5
  import { program } from "commander";
6
6
  import build from "../index.js";
7
- const version = "1.0.12-beta.8";
7
+ const version = "1.0.12";
8
8
  const pack = {
9
9
  version
10
10
  };
@@ -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 babel = require("@babel/core");
7
- const _traverse = require("@babel/traverse");
8
- const types = require("@babel/types");
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
- pages.forEach((page) => {
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.CommonJS,
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 ast = babel.parseSync(jsCode);
159
- const addedArgs = types.objectExpression([
160
- types.objectProperty(types.identifier("path"), types.stringLiteral(module2.path))
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
- const component = types.objectProperty(types.identifier("component"), types.booleanLiteral(true));
164
- addedArgs.properties.push(component);
159
+ extraInfo.component = true;
165
160
  }
166
161
  if (module2.usingComponents) {
167
- const components = types.objectProperty(types.identifier("usingComponents"), types.objectExpression([]));
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
- const props = types.objectProperty(types.identifier(`'${name}'`), types.stringLiteral(path2));
188
- components.value.properties.push(props);
181
+ await buildJSByPath(packageName, componentModule, compileRes, mainCompileRes, true, depthChain, toMainSubPackage);
182
+ componentsObj[name] = path2;
189
183
  }
190
- addedArgs.properties.push(components);
184
+ extraInfo.usingComponents = componentsObj;
191
185
  }
192
186
  if (addExtra) {
193
- ast.program.body.splice(0, 0, getExtraInfoStatement("this", addedArgs));
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
- traverse(ast, {
201
- CallExpression(ap) {
202
- if (ap.node.callee.name === "require" || ap.node.callee.object?.name === "require") {
203
- const requirePath = ap.node.arguments[0].value;
204
- if (requirePath) {
205
- const requireFullPath = path.resolve(modulePath, `../${requirePath}`);
206
- let id = requireFullPath.split(`${env.getWorkPath()}${path.sep}`)[1];
207
- id = id.replace(/\.(js|ts)$/, "").replace(/\\/g, "/");
208
- if (!id.startsWith("/")) {
209
- id = "/" + id;
210
- }
211
- ap.node.arguments[0] = types.stringLiteral(id);
212
- if (!processedModules.has(packageName + id)) {
213
- buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false, depthChain);
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
- ImportDeclaration(ap) {
219
- const importPath = ap.node.source.value;
220
- if (importPath) {
221
- let id;
222
- let shouldProcess = false;
223
- if (importPath.startsWith("@") || importPath.startsWith("miniprogram_npm/")) {
224
- if (importPath.startsWith("@")) {
225
- id = `/miniprogram_npm/${importPath}`;
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.startsWith("/") ? importPath : `/${importPath}`;
256
+ id = importPath;
257
+ shouldProcess = true;
228
258
  }
229
- shouldProcess = true;
230
- } else if (importPath.startsWith("./") || importPath.startsWith("../") || !importPath.startsWith("/")) {
231
- const importFullPath = path.resolve(modulePath, `../${importPath}`);
232
- id = importFullPath.split(`${env.getWorkPath()}${path.sep}`)[1];
233
- id = id.replace(/\.(js|ts)$/, "").replace(/\\/g, "/");
234
- if (!id.startsWith("/")) {
235
- id = "/" + id;
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
- if (shouldProcess) {
243
- ap.node.source = types.stringLiteral(id);
244
- if (!processedModules.has(packageName + id)) {
245
- buildJSByPath(packageName, { path: id }, compileRes, mainCompileRes, false, depthChain);
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
- const { code } = babel.transformFromAstSync(ast, "", BABEL_TRANSFORM_CONFIG);
252
- compileInfo.code = code;
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"];