@neuxnet/neux-cli 0.2.3

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.
Files changed (73) hide show
  1. package/README.md +438 -0
  2. package/README.zh-CN.md +554 -0
  3. package/assets/container/assets/index.css +1 -0
  4. package/assets/container/assets/index.js +137 -0
  5. package/assets/container/assets/pageFrame.css +1 -0
  6. package/assets/container/assets/pageFrame.js +56 -0
  7. package/assets/container/assets/service.js +6 -0
  8. package/assets/container/assets/vconsole.js +931 -0
  9. package/assets/container/favicon.ico +0 -0
  10. package/assets/container/images/icon-arrow.png +0 -0
  11. package/assets/container/images/mini-action-white.png +0 -0
  12. package/assets/container/images/mini-action.png +0 -0
  13. package/assets/container/images/mini-arrow-left-white.png +0 -0
  14. package/assets/container/images/mini-arrow-left.jpg +0 -0
  15. package/assets/container/images/mini-arrow-left.png +0 -0
  16. package/assets/container/images/mini-close-white.png +0 -0
  17. package/assets/container/images/mini-close.png +0 -0
  18. package/assets/container/images/more.png +0 -0
  19. package/assets/container/images/search.jpg +0 -0
  20. package/assets/container/index.html +1 -0
  21. package/assets/container/pageFrame.html +1 -0
  22. package/assets/init/tabbar/home-active.png +0 -0
  23. package/assets/init/tabbar/home.png +0 -0
  24. package/assets/init/tabbar/list-active.png +0 -0
  25. package/assets/init/tabbar/list.png +0 -0
  26. package/assets/init/types/neux-api.d.ts +1402 -0
  27. package/package.json +68 -0
  28. package/scripts/generate-init-types.js +210 -0
  29. package/scripts/sync-compiler.js +22 -0
  30. package/scripts/sync-web-container.js +34 -0
  31. package/src/bin/cli.js +646 -0
  32. package/src/core/brand.js +11 -0
  33. package/src/core/compiler.js +305 -0
  34. package/src/core/defaults.js +12 -0
  35. package/src/core/dev.js +15 -0
  36. package/src/core/errors.js +17 -0
  37. package/src/core/fs.js +66 -0
  38. package/src/core/i18n.js +37 -0
  39. package/src/core/init.js +866 -0
  40. package/src/core/lifecycle.js +32 -0
  41. package/src/core/manifest.js +72 -0
  42. package/src/core/pack.js +156 -0
  43. package/src/core/package-info.js +27 -0
  44. package/src/core/preview-server.js +123 -0
  45. package/src/core/project.js +101 -0
  46. package/src/core/prompts.js +71 -0
  47. package/src/core/proxy-security.js +141 -0
  48. package/src/core/proxy.js +156 -0
  49. package/src/core/qr.js +41 -0
  50. package/src/core/terminal-qr.js +72 -0
  51. package/src/core/update.js +278 -0
  52. package/src/core/watch.js +113 -0
  53. package/src/core/web.js +649 -0
  54. package/src/core/zip.js +120 -0
  55. package/src/index.js +20 -0
  56. package/src/providers/service-client.js +149 -0
  57. package/src/providers/service-config.js +264 -0
  58. package/src/providers/service.js +146 -0
  59. package/src/providers/upload.js +112 -0
  60. package/vendor/dimina-compiler/bin/index.cjs +265 -0
  61. package/vendor/dimina-compiler/bin/index.js +263 -0
  62. package/vendor/dimina-compiler/compatibility-B-DoZtUX.cjs +395 -0
  63. package/vendor/dimina-compiler/compatibility-Cl3-DO6V.js +366 -0
  64. package/vendor/dimina-compiler/core/logic-compiler.cjs +378 -0
  65. package/vendor/dimina-compiler/core/logic-compiler.js +374 -0
  66. package/vendor/dimina-compiler/core/style-compiler.cjs +392 -0
  67. package/vendor/dimina-compiler/core/style-compiler.js +377 -0
  68. package/vendor/dimina-compiler/core/view-compiler.cjs +1601 -0
  69. package/vendor/dimina-compiler/core/view-compiler.js +1582 -0
  70. package/vendor/dimina-compiler/index.cjs +762 -0
  71. package/vendor/dimina-compiler/index.js +751 -0
  72. package/vendor/dimina-compiler/sourcemap-BgtIgqkC.cjs +1377 -0
  73. package/vendor/dimina-compiler/sourcemap-CKjhV9h7.js +1135 -0
@@ -0,0 +1,374 @@
1
+ import { C as resetStoreInfo, E as collectAssets, O as hasCompileInfo, S as getWorkPath, _ as getTargetPath, a as getAppConfigInfo, d as getDependencyGraph, f as getNpmResolver, i as remapSourcemap, j as resolveAssetSourcePath, l as getComponent, o as getAppId, r as mergeSourcemap, u as getContentByPath, w as resolveAppAlias } from "../sourcemap-CKjhV9h7.js";
2
+ import { a as warnUnsupportedWxApi, i as takeCompatibilityWarnings, r as getWxMemberName } from "../compatibility-Cl3-DO6V.js";
3
+ import fs from "node:fs";
4
+ import { resolve, sep } from "node:path";
5
+ import { isMainThread, parentPort } from "node:worker_threads";
6
+ import { parseSync } from "oxc-parser";
7
+ import { walk } from "oxc-walker";
8
+ import MagicString from "magic-string";
9
+ import { transform } from "esbuild";
10
+ //#region src/core/logic-compiler.js
11
+ var processedModules = /* @__PURE__ */ new Set();
12
+ var enableSourcemap = false;
13
+ if (!isMainThread) parentPort.on("message", async ({ pages, storeInfo, sourcemap }) => {
14
+ try {
15
+ resetStoreInfo(storeInfo);
16
+ enableSourcemap = !!sourcemap;
17
+ const progress = {
18
+ _completedTasks: 0,
19
+ get completedTasks() {
20
+ return this._completedTasks;
21
+ },
22
+ set completedTasks(value) {
23
+ this._completedTasks = value;
24
+ parentPort.postMessage({ completedTasks: this.completedTasks });
25
+ }
26
+ };
27
+ const mainCompileRes = await compileJS(pages.mainPages, null, null, progress);
28
+ for (const [root, subPages] of Object.entries(pages.subPages)) try {
29
+ await writeCompileRes(await compileJS(subPages.info, root, subPages.independent ? [] : mainCompileRes, progress), root);
30
+ } catch (error) {
31
+ throw new Error(`Error processing subpackage ${root}: ${error.message}\n${error.stack}`);
32
+ }
33
+ await writeCompileRes(mainCompileRes, null);
34
+ processedModules.clear();
35
+ parentPort.postMessage({
36
+ success: true,
37
+ compatibilityWarnings: takeCompatibilityWarnings(),
38
+ dependencyGraph: getDependencyGraph().toJSON()
39
+ });
40
+ } catch (error) {
41
+ processedModules.clear();
42
+ parentPort.postMessage({
43
+ success: false,
44
+ error: {
45
+ message: error.message,
46
+ stack: error.stack,
47
+ name: error.name
48
+ }
49
+ });
50
+ }
51
+ });
52
+ async function writeCompileRes(compileRes, root) {
53
+ const outputDir = root ? `${getTargetPath()}/${root}` : `${getTargetPath()}/main`;
54
+ if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
55
+ if (enableSourcemap) {
56
+ const { bundleCode, sourcemap } = mergeSourcemap(compileRes);
57
+ const sourcemapFileName = "logic.js.map";
58
+ fs.writeFileSync(`${outputDir}/logic.js`, `${bundleCode}//# sourceMappingURL=${sourcemapFileName}\n`);
59
+ fs.writeFileSync(`${outputDir}/${sourcemapFileName}`, sourcemap);
60
+ } else {
61
+ let mergeCode = "";
62
+ for (const module of compileRes) {
63
+ const { code: minifiedCode } = await transform(`modDefine('${module.path}', function(require, module, exports) {
64
+ ${module.code}
65
+ });`, {
66
+ minify: true,
67
+ target: ["es2023"],
68
+ platform: "neutral"
69
+ });
70
+ mergeCode += minifiedCode;
71
+ }
72
+ fs.writeFileSync(`${outputDir}/logic.js`, mergeCode);
73
+ }
74
+ }
75
+ /**
76
+ * 编译 js 文件
77
+ */
78
+ async function compileJS(pages, root, mainCompileRes, progress) {
79
+ const compileRes = [];
80
+ if (!root) await buildJSByPath(root, { path: "app" }, compileRes, mainCompileRes, false);
81
+ for (const page of pages) {
82
+ await buildJSByPath(root, page, compileRes, mainCompileRes, true);
83
+ progress.completedTasks++;
84
+ }
85
+ return compileRes;
86
+ }
87
+ async function buildJSByPath(packageName, module, compileRes, mainCompileRes, addExtra, activePaths = /* @__PURE__ */ new Set(), putMain = false) {
88
+ const currentPath = module.path;
89
+ if (!currentPath) return;
90
+ if (activePaths.has(currentPath)) return;
91
+ if (hasCompileInfo(module.path, compileRes, mainCompileRes)) return;
92
+ const compileInfo = {
93
+ path: module.path,
94
+ code: "",
95
+ sourceFile: null
96
+ };
97
+ const src = module.path.startsWith("/") ? module.path : `/${module.path}`;
98
+ const modulePath = getJSAbsolutePath(src);
99
+ if (!modulePath) {
100
+ console.warn("[logic]", `找不到模块文件: ${src}`);
101
+ return;
102
+ }
103
+ getDependencyGraph().addFile(currentPath, modulePath, "logic");
104
+ const diagnosticSource = modulePath.startsWith(getWorkPath()) ? modulePath.slice(getWorkPath().length) : src;
105
+ const sourceCode = getContentByPath(modulePath);
106
+ if (!sourceCode) {
107
+ console.warn("[logic]", `无法读取模块文件: ${modulePath}`);
108
+ return;
109
+ }
110
+ const isTypeScript = modulePath.endsWith(".ts");
111
+ if (enableSourcemap) {
112
+ const workPath = getWorkPath();
113
+ compileInfo.sourceFile = modulePath.startsWith(workPath) ? modulePath.slice(workPath.length) : src;
114
+ }
115
+ const ast = parseSync(modulePath, sourceCode, {
116
+ sourceType: "module",
117
+ lang: isTypeScript ? "ts" : "js"
118
+ }).program;
119
+ const s = new MagicString(sourceCode);
120
+ const extraInfo = { path: module.path };
121
+ activePaths.add(currentPath);
122
+ if (module.component) extraInfo.component = true;
123
+ if (module.usingComponents) {
124
+ const componentsObj = {};
125
+ const allSubPackages = getAppConfigInfo().subPackages;
126
+ const graphDependencies = getDependencyGraph().getDirectDependencies(module.path, "component");
127
+ const componentDependencies = graphDependencies.length > 0 ? new Set(graphDependencies) : null;
128
+ for (const [name, path] of Object.entries(module.usingComponents)) {
129
+ if (componentDependencies && !componentDependencies.has(path)) continue;
130
+ let toMainSubPackage = true;
131
+ if (packageName) {
132
+ const normalizedPath = path.startsWith("/") ? path.substring(1) : path;
133
+ for (const subPackage of allSubPackages) if (normalizedPath.startsWith(`${subPackage.root}/`)) {
134
+ toMainSubPackage = false;
135
+ break;
136
+ }
137
+ } else toMainSubPackage = false;
138
+ const componentModule = getComponent(path);
139
+ if (!componentModule) continue;
140
+ await buildJSByPath(packageName, componentModule, compileRes, mainCompileRes, true, activePaths, putMain || toMainSubPackage);
141
+ componentsObj[name] = path;
142
+ }
143
+ extraInfo.usingComponents = componentsObj;
144
+ }
145
+ if (addExtra) {
146
+ const extraInfoCode = `globalThis.__extraInfo = ${JSON.stringify(extraInfo)};\n`;
147
+ if (enableSourcemap) compileInfo.extraInfoCode = extraInfoCode;
148
+ else s.prepend(extraInfoCode);
149
+ }
150
+ if (putMain) mainCompileRes.push(compileInfo);
151
+ else compileRes.push(compileInfo);
152
+ const pathReplacements = [];
153
+ const dependenciesToProcess = [];
154
+ walk(ast, { enter(node, parent) {
155
+ const wxMemberName = getWxMemberName(node);
156
+ if (wxMemberName) warnUnsupportedWxApi(wxMemberName, compileInfo.sourceFile || diagnosticSource, node.loc?.start?.line || getLineByIndex(sourceCode, node.start));
157
+ if ((node.type === "StringLiteral" || node.type === "Literal") && isLocalAssetString(node.value)) {
158
+ getDependencyGraph().addFile(currentPath, resolveAssetSourcePath(getWorkPath(), modulePath, node.value), "logic");
159
+ pathReplacements.push({
160
+ start: node.start,
161
+ end: node.end,
162
+ newValue: collectAssets(getWorkPath(), modulePath, node.value, getTargetPath(), getAppId())
163
+ });
164
+ }
165
+ if (node.type === "CallExpression") {
166
+ const isRequire = node.callee.type === "Identifier" && node.callee.name === "require";
167
+ const isRequireProperty = node.callee.type === "MemberExpression" && node.callee.object?.type === "Identifier" && node.callee.object?.name === "require";
168
+ if ((isRequire || isRequireProperty) && node.arguments.length > 0 && (node.arguments[0].type === "StringLiteral" || node.arguments[0].type === "Literal")) {
169
+ const arg = node.arguments[0];
170
+ const requirePath = arg.value;
171
+ if (requirePath) {
172
+ const { id, shouldProcess } = resolveDependencyId(requirePath, modulePath, false);
173
+ if (shouldProcess) {
174
+ getDependencyGraph().addDependency(currentPath, id, "logic");
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
+ getDependencyGraph().addDependency(currentPath, id, "logic");
191
+ pathReplacements.push({
192
+ start: node.source.start,
193
+ end: node.source.end,
194
+ newValue: id
195
+ });
196
+ if (!processedModules.has(packageName + id)) dependenciesToProcess.push(id);
197
+ }
198
+ }
199
+ }
200
+ if (node.type === "TSImportEqualsDeclaration" && node.moduleReference?.type === "TSExternalModuleReference") {
201
+ const importPathNode = node.moduleReference.expression;
202
+ const importPath = importPathNode?.value;
203
+ if (importPath) {
204
+ const { id, shouldProcess } = resolveDependencyId(importPath, modulePath, false);
205
+ if (shouldProcess) {
206
+ getDependencyGraph().addDependency(currentPath, id, "logic");
207
+ pathReplacements.push({
208
+ start: importPathNode.start,
209
+ end: importPathNode.end,
210
+ newValue: id
211
+ });
212
+ if (!processedModules.has(packageName + id)) dependenciesToProcess.push(id);
213
+ }
214
+ }
215
+ }
216
+ if ((node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration") && node.source) {
217
+ const exportPath = node.source.value;
218
+ if (exportPath) {
219
+ const { id, shouldProcess } = resolveDependencyId(exportPath, modulePath, true);
220
+ if (shouldProcess) {
221
+ getDependencyGraph().addDependency(currentPath, id, "logic");
222
+ pathReplacements.push({
223
+ start: node.source.start,
224
+ end: node.source.end,
225
+ newValue: id
226
+ });
227
+ if (!processedModules.has(packageName + id)) dependenciesToProcess.push(id);
228
+ }
229
+ }
230
+ }
231
+ } });
232
+ for (const depId of dependenciesToProcess) await buildJSByPath(packageName, { path: depId }, compileRes, mainCompileRes, false, activePaths, putMain);
233
+ for (const replacement of pathReplacements.reverse()) s.overwrite(replacement.start, replacement.end, `'${replacement.newValue}'`);
234
+ const modifiedCode = s.toString();
235
+ let preEsbuildMap = null;
236
+ if (enableSourcemap && compileInfo.sourceFile) {
237
+ const generatedMap = JSON.parse(s.generateMap({
238
+ file: compileInfo.sourceFile,
239
+ source: compileInfo.sourceFile,
240
+ includeContent: true,
241
+ hires: true
242
+ }).toString());
243
+ generatedMap.file = compileInfo.sourceFile;
244
+ generatedMap.sources = [compileInfo.sourceFile];
245
+ generatedMap.sourcesContent = [sourceCode];
246
+ preEsbuildMap = JSON.stringify(generatedMap);
247
+ }
248
+ try {
249
+ const esbuildOpts = {
250
+ format: "cjs",
251
+ target: "es2020",
252
+ platform: "neutral",
253
+ loader: isTypeScript ? "ts" : "js"
254
+ };
255
+ if (enableSourcemap && compileInfo.sourceFile) {
256
+ esbuildOpts.sourcemap = true;
257
+ esbuildOpts.sourcefile = compileInfo.sourceFile;
258
+ esbuildOpts.sourcesContent = true;
259
+ }
260
+ const esbuildResult = await transform(modifiedCode, esbuildOpts);
261
+ if (enableSourcemap && esbuildResult.map) compileInfo.map = preEsbuildMap ? remapSourcemap(esbuildResult.map, preEsbuildMap) : esbuildResult.map;
262
+ compileInfo.code = esbuildResult.code;
263
+ } catch (error) {
264
+ console.error(`[logic] esbuild 转换失败 ${modulePath}:`, error.message);
265
+ compileInfo.code = modifiedCode;
266
+ }
267
+ processedModules.add(packageName + currentPath);
268
+ activePaths.delete(currentPath);
269
+ }
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|mp3|m4a|aac|wav)(?:\?.*)?$/.test(value);
272
+ }
273
+ function getLineByIndex(content, index) {
274
+ if (typeof index !== "number" || index < 0) return null;
275
+ let line = 1;
276
+ for (let i = 0; i < index; i++) if (content.charCodeAt(i) === 10) line++;
277
+ return line;
278
+ }
279
+ /**
280
+ * 获取 JavaScript 或 TypeScript 文件的绝对路径
281
+ * @param {string} modulePath - 模块路径
282
+ * @returns {string|null} - 文件的绝对路径,如果找不到则返回 null
283
+ */
284
+ function getJSAbsolutePath(modulePath) {
285
+ const workPath = getWorkPath();
286
+ const resolvedModuleId = resolveModuleIdToExistingPath(modulePath);
287
+ if (!resolvedModuleId) return null;
288
+ for (const ext of [".js", ".ts"]) {
289
+ const fullPath = `${workPath}${resolvedModuleId}${ext}`;
290
+ if (fs.existsSync(fullPath)) return fullPath;
291
+ }
292
+ return null;
293
+ }
294
+ function resolveDependencyId(specifier, modulePath, allowAbsolute) {
295
+ if (!specifier) return {
296
+ id: specifier,
297
+ shouldProcess: false
298
+ };
299
+ if (specifier.startsWith("miniprogram_npm/")) {
300
+ const npmModuleId = normalizeModuleId(`/${specifier}`);
301
+ return {
302
+ id: resolveModuleIdToExistingPath(npmModuleId) || npmModuleId,
303
+ shouldProcess: true
304
+ };
305
+ }
306
+ if (specifier.startsWith("./") || specifier.startsWith("../")) return {
307
+ id: resolveRelativeModuleId(specifier, modulePath),
308
+ shouldProcess: true
309
+ };
310
+ if (specifier.startsWith("/")) return {
311
+ id: allowAbsolute ? normalizeModuleId(specifier) : resolveRelativeModuleId(specifier, modulePath),
312
+ shouldProcess: true
313
+ };
314
+ const aliasResolved = resolveAppAlias(specifier);
315
+ if (aliasResolved) return {
316
+ id: normalizeModuleId(aliasResolved),
317
+ shouldProcess: true
318
+ };
319
+ if (specifier.startsWith("@") || isBareModuleSpecifier(specifier)) {
320
+ const npmModuleId = resolveNpmModuleId(specifier, modulePath);
321
+ if (npmModuleId) return {
322
+ id: npmModuleId,
323
+ shouldProcess: true
324
+ };
325
+ const siblingModuleId = resolveBareSiblingModuleId(specifier, modulePath);
326
+ return {
327
+ id: siblingModuleId || specifier,
328
+ shouldProcess: Boolean(siblingModuleId)
329
+ };
330
+ }
331
+ return {
332
+ id: specifier,
333
+ shouldProcess: false
334
+ };
335
+ }
336
+ function isBareModuleSpecifier(specifier) {
337
+ return !specifier.startsWith(".") && !specifier.startsWith("/");
338
+ }
339
+ function resolveRelativeModuleId(specifier, modulePath) {
340
+ const relativeId = resolve(modulePath, `../${specifier}`).split(`${getWorkPath()}${sep}`)[1];
341
+ return normalizeModuleId(relativeId);
342
+ }
343
+ function resolveBareSiblingModuleId(specifier, modulePath) {
344
+ return resolveModuleIdToExistingPath(resolveRelativeModuleId(`./${specifier}`, modulePath));
345
+ }
346
+ function normalizeModuleId(moduleId) {
347
+ let normalized = moduleId.replace(/\.(js|ts)$/, "").replace(/\\/g, "/");
348
+ if (!normalized.startsWith("/")) normalized = `/${normalized}`;
349
+ return normalized;
350
+ }
351
+ function resolveNpmModuleId(specifier, modulePath) {
352
+ const npmResolver = getNpmResolver();
353
+ if (!npmResolver) return null;
354
+ return npmResolver.resolveScriptModule(specifier, modulePath, resolveModuleIdToExistingPath);
355
+ }
356
+ function resolveModuleIdToExistingPath(moduleId) {
357
+ const normalizedModuleId = normalizeModuleId(moduleId);
358
+ const workPath = getWorkPath();
359
+ for (const ext of [".js", ".ts"]) if (fs.existsSync(`${workPath}${normalizedModuleId}${ext}`)) return normalizedModuleId;
360
+ for (const ext of [".js", ".ts"]) if (fs.existsSync(`${workPath}${normalizedModuleId}/index${ext}`)) return `${normalizedModuleId}/index`;
361
+ const packageJsonPath = `${workPath}${normalizedModuleId}/package.json`;
362
+ if (fs.existsSync(packageJsonPath)) try {
363
+ const packageInfo = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
364
+ for (const entryField of ["miniprogram", "main"]) if (typeof packageInfo[entryField] === "string" && packageInfo[entryField]) {
365
+ const resolvedEntry = resolveModuleIdToExistingPath(normalizeModuleId(resolve(normalizedModuleId, packageInfo[entryField])));
366
+ if (resolvedEntry) return resolvedEntry;
367
+ }
368
+ } catch (error) {
369
+ console.warn("[logic]", `解析 package.json 失败: ${packageJsonPath}`, error.message);
370
+ }
371
+ return null;
372
+ }
373
+ //#endregion
374
+ export { buildJSByPath, compileJS };