@weapp-tailwindcss/env 0.0.0-stage → 0.2.0

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/index.cjs ADDED
@@ -0,0 +1,670 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+ //#endregion
24
+ let node_process = require("node:process");
25
+ node_process = __toESM(node_process, 1);
26
+ let node_path = require("node:path");
27
+ node_path = __toESM(node_path, 1);
28
+ let node_fs = require("node:fs");
29
+ //#region src/environment.ts
30
+ function getProcessEnv() {
31
+ return {
32
+ MPX_CLI_MODE: node_process.default.env["MPX_CLI_MODE"],
33
+ MPX_CURRENT_TARGET_MODE: node_process.default.env["MPX_CURRENT_TARGET_MODE"],
34
+ NODE_PATH: node_process.default.env["NODE_PATH"],
35
+ TARO_ENV: node_process.default.env["TARO_ENV"],
36
+ UNI_PLATFORM: node_process.default.env["UNI_PLATFORM"],
37
+ UNI_UTS_PLATFORM: node_process.default.env["UNI_UTS_PLATFORM"],
38
+ WEAPP_TAILWINDCSS_TARGET: node_process.default.env["WEAPP_TAILWINDCSS_TARGET"],
39
+ WEAPP_TW_TARGET: node_process.default.env["WEAPP_TW_TARGET"]
40
+ };
41
+ }
42
+ //#endregion
43
+ //#region src/hbuilderx.ts
44
+ const KNOWN_MAC_HBUILDERX_PLUGIN_DIRS = ["/Applications/HBuilderX.app/Contents/HBuilderX/plugins/uniapp-cli-vite", "/Applications/HBuilderX.app/Contents/HBuilderX/plugins/uniapp-cli"];
45
+ const HBUILDERX_PLUGIN_CWD_RE = /[\\/]HBuilderX(?:\.[^\\/]*)?(?:[\\/]Contents[\\/]HBuilderX)?[\\/]plugins[\\/]uniapp-cli(?:-vite)?(?:[\\/]|$)/i;
46
+ function hasKnownHBuilderXPluginPrefix(normalizedCwd) {
47
+ for (const dir of KNOWN_MAC_HBUILDERX_PLUGIN_DIRS) {
48
+ const normalizedDir = node_path.default.normalize(dir);
49
+ if (normalizedCwd === normalizedDir || normalizedCwd.startsWith(`${normalizedDir}${node_path.default.sep}`)) return true;
50
+ }
51
+ return false;
52
+ }
53
+ function matchesHBuilderXPluginCwd(cwd) {
54
+ const normalized = node_path.default.normalize(cwd);
55
+ if (hasKnownHBuilderXPluginPrefix(normalized)) return true;
56
+ return HBUILDERX_PLUGIN_CWD_RE.test(normalized);
57
+ }
58
+ function isRunningInHBuilderX(options = {}) {
59
+ const env = options.env ?? getProcessEnv();
60
+ const nodePath = "nodePath" in options ? options.nodePath : env.NODE_PATH;
61
+ if (!(nodePath == null || nodePath.trim().length === 0)) return false;
62
+ return matchesHBuilderXPluginCwd(options.cwd ?? node_process.default.cwd());
63
+ }
64
+ //#endregion
65
+ //#region src/matching.ts
66
+ function evidence(source, key, value, file) {
67
+ return {
68
+ source,
69
+ key,
70
+ ...value !== void 0 ? { value } : {},
71
+ ...file !== void 0 ? { path: file } : {}
72
+ };
73
+ }
74
+ function collectMatches(matches, all) {
75
+ const result = [];
76
+ for (const candidate of matches) {
77
+ result.push(candidate);
78
+ if (!all) break;
79
+ }
80
+ return result;
81
+ }
82
+ function detectionReport(candidates) {
83
+ const grouped = /* @__PURE__ */ new Map();
84
+ for (const candidate of candidates) {
85
+ const existing = grouped.get(candidate.appType);
86
+ if (existing) existing.evidence.push(...candidate.evidence);
87
+ else grouped.set(candidate.appType, {
88
+ ...candidate,
89
+ evidence: [...candidate.evidence]
90
+ });
91
+ }
92
+ const unique = [...grouped.values()];
93
+ const appType = unique[0]?.appType;
94
+ const family = (value) => value.startsWith("uni-app") ? "uni-app" : value;
95
+ return {
96
+ appType,
97
+ candidates: unique,
98
+ conflicts: appType === void 0 ? [] : unique.filter((candidate) => family(candidate.appType) !== family(appType)).map((candidate) => candidate.appType)
99
+ };
100
+ }
101
+ //#endregion
102
+ //#region src/platform.ts
103
+ const MPX_TO_UNI_PLATFORM = /* @__PURE__ */ new Map([
104
+ ["ali", "mp-alipay"],
105
+ ["dd", "mp-dingtalk"],
106
+ ["jd", "mp-jd"],
107
+ ["qq", "mp-qq"],
108
+ ["swan", "mp-baidu"],
109
+ ["tt", "mp-toutiao"],
110
+ ["wx", "mp-weixin"]
111
+ ]);
112
+ const MINI_PROGRAM_PLATFORMS = new Map([
113
+ ...[...MPX_TO_UNI_PLATFORM].filter(([alias]) => alias !== "ali" && alias !== "dd"),
114
+ ["alipay", "mp-alipay"],
115
+ ["baidu", "mp-baidu"],
116
+ ["weapp", "mp-weixin"],
117
+ ["wechat", "mp-weixin"]
118
+ ]);
119
+ const CANONICAL_PLATFORMS = new Map([
120
+ ...[...MPX_TO_UNI_PLATFORM.values()].map((value) => [value, value]),
121
+ ["web", "web"],
122
+ ["h5", "web"],
123
+ ["app", "app"],
124
+ ["app-plus", "app-plus"],
125
+ ["app-android", "app-android"],
126
+ ["app-ios", "app-ios"],
127
+ ["app-harmony", "app-harmony"]
128
+ ]);
129
+ function normalizePlatform(value) {
130
+ return value?.trim().toLowerCase() || void 0;
131
+ }
132
+ function resolvePlatformInfo(value, appType) {
133
+ const normalized = normalizePlatform(value);
134
+ const isAppAndroid = normalized === "app-android";
135
+ const isAppIos = normalized === "app-ios";
136
+ const isAppHarmony = normalized === "app-harmony";
137
+ return {
138
+ raw: value,
139
+ normalized,
140
+ isApp: normalized?.startsWith("app-") === true || normalized === "app" || normalized === "app-plus",
141
+ isAppAndroid,
142
+ isAppHarmony,
143
+ isAppIos,
144
+ isMp: normalized?.startsWith("mp-") === true || (normalized ? MINI_PROGRAM_PLATFORMS.has(normalized) || appType === "mpx" && MPX_TO_UNI_PLATFORM.has(normalized) : false),
145
+ isWeb: normalized?.startsWith("web") === true || normalized === "h5"
146
+ };
147
+ }
148
+ function resolvePlatform(value) {
149
+ return resolvePlatformInfo(value);
150
+ }
151
+ function resolveTaroPlatform(value = getProcessEnv().TARO_ENV) {
152
+ return resolvePlatform(value);
153
+ }
154
+ /** 读取 Taro 环境快照;省略快照时才使用调用时的进程环境。 */
155
+ function resolveTaroPlatformFromEnv(env = getProcessEnv()) {
156
+ return resolvePlatform(env.TARO_ENV);
157
+ }
158
+ function resolveMpxPlatformValue(env = getProcessEnv()) {
159
+ return env.MPX_CURRENT_TARGET_MODE ?? env.MPX_CLI_MODE;
160
+ }
161
+ function resolveMpxPlatform(value = resolveMpxPlatformValue()) {
162
+ return resolvePlatformInfo(value, "mpx");
163
+ }
164
+ /** 读取 MPX 环境快照,保留 current mode 的空字符串优先级。 */
165
+ function resolveMpxPlatformFromEnv(env = getProcessEnv()) {
166
+ return resolvePlatformInfo(resolveMpxPlatformValue(env), "mpx");
167
+ }
168
+ function resolveUniPlatform(value = getProcessEnv().UNI_PLATFORM) {
169
+ return resolvePlatform(value);
170
+ }
171
+ function resolveUniUtsPlatform(value = getProcessEnv().UNI_UTS_PLATFORM) {
172
+ return resolvePlatform(value);
173
+ }
174
+ function resolveUniAppXPlatform(value = getProcessEnv().UNI_UTS_PLATFORM) {
175
+ return resolvePlatform(value);
176
+ }
177
+ function resolveUniPlatformsFromEnv(env = getProcessEnv()) {
178
+ return {
179
+ uniPlatform: resolvePlatform(env.UNI_PLATFORM),
180
+ uniAppXPlatform: resolvePlatform(env.UNI_UTS_PLATFORM),
181
+ uniUtsPlatform: resolvePlatform(env.UNI_UTS_PLATFORM)
182
+ };
183
+ }
184
+ function normalizeFrameworkStylePlatform(platform, appType) {
185
+ const normalized = platform?.trim().toLowerCase();
186
+ if (!normalized) return;
187
+ if (appType === "mpx") return MPX_TO_UNI_PLATFORM.get(normalized) ?? normalized;
188
+ return normalized;
189
+ }
190
+ function resolvePlatformIdentity(platform, appType) {
191
+ const normalized = normalizePlatform(platform);
192
+ return {
193
+ raw: platform,
194
+ normalized,
195
+ canonical: normalized ? (appType === "mpx" ? MPX_TO_UNI_PLATFORM.get(normalized) : void 0) ?? MINI_PROGRAM_PLATFORMS.get(normalized) ?? CANONICAL_PLATFORMS.get(normalized) : void 0
196
+ };
197
+ }
198
+ //#endregion
199
+ //#region src/detect-env.ts
200
+ function* matchEnvironment(env, options = {}) {
201
+ for (const key of ["WEAPP_TW_TARGET", "WEAPP_TAILWINDCSS_TARGET"]) {
202
+ const value = env[key];
203
+ if (value === "weapp-vite") yield {
204
+ appType: "weapp-vite",
205
+ evidence: [evidence("env", key, value)]
206
+ };
207
+ }
208
+ const taro = env.TARO_ENV;
209
+ if (taro) yield {
210
+ appType: "taro",
211
+ evidence: [evidence("env", "TARO_ENV", taro)]
212
+ };
213
+ for (const key of ["MPX_CLI_MODE", "MPX_CURRENT_TARGET_MODE"]) {
214
+ const value = env[key];
215
+ if (value) yield {
216
+ appType: "mpx",
217
+ evidence: [evidence("env", key, value)]
218
+ };
219
+ }
220
+ const { uniPlatform, uniUtsPlatform } = resolveUniPlatformsFromEnv(env);
221
+ if (uniUtsPlatform.normalized) yield {
222
+ appType: "uni-app-x",
223
+ evidence: [evidence("env", "UNI_UTS_PLATFORM", uniUtsPlatform.raw)]
224
+ };
225
+ if (uniPlatform.normalized) yield {
226
+ appType: "uni-app-vite",
227
+ evidence: [evidence("env", "UNI_PLATFORM", uniPlatform.raw)]
228
+ };
229
+ if (isRunningInHBuilderX({
230
+ cwd: options.cwd,
231
+ env
232
+ })) yield {
233
+ appType: options.hbuilderxVite === false ? "uni-app" : "uni-app-vite",
234
+ evidence: [evidence("hbuilderx", "cwd", options.cwd ?? node_process.default.cwd())]
235
+ };
236
+ }
237
+ function detectAppTypeFromEnv(env = getProcessEnv(), options = {}) {
238
+ return collectMatches(matchEnvironment(env, options), false)[0]?.appType;
239
+ }
240
+ //#endregion
241
+ //#region src/script-command.ts
242
+ const ASSIGNMENT_RE = /^[A-Z_]\w*=/iu;
243
+ const COMMAND_TYPES = /* @__PURE__ */ new Map([
244
+ ["mpx", "mpx"],
245
+ ["mpx-cli-service", "mpx"],
246
+ ["taro", "taro"],
247
+ ["uni", "uni-app-vite"],
248
+ ["weapp-vite", "weapp-vite"]
249
+ ]);
250
+ const PNPM_VALUE_OPTIONS = /* @__PURE__ */ new Set([
251
+ "-C",
252
+ "--dir",
253
+ "-F",
254
+ "--filter",
255
+ "--filter-prod",
256
+ "--reporter"
257
+ ]);
258
+ const PNPM_FLAGS = /* @__PURE__ */ new Set([
259
+ "-w",
260
+ "--workspace-root",
261
+ "-r",
262
+ "--recursive",
263
+ "-s",
264
+ "--silent"
265
+ ]);
266
+ const NPX_VALUE_OPTIONS = /* @__PURE__ */ new Set([
267
+ "-p",
268
+ "--package",
269
+ "--cache",
270
+ "--userconfig"
271
+ ]);
272
+ const NPX_FLAGS = /* @__PURE__ */ new Set([
273
+ "-y",
274
+ "--yes",
275
+ "--no",
276
+ "--no-install",
277
+ "-q",
278
+ "--quiet"
279
+ ]);
280
+ const ENV_VALUE_OPTIONS = /* @__PURE__ */ new Set([
281
+ "-u",
282
+ "--unset",
283
+ "-C",
284
+ "--chdir"
285
+ ]);
286
+ function readExpansionEnd(script, start, opening, closing) {
287
+ let depth = 1;
288
+ let quote;
289
+ for (let index = start + 2; index < script.length; index++) {
290
+ const character = script[index];
291
+ if (character === "\\" && quote !== "'") index++;
292
+ else if (quote) {
293
+ if (character === quote) quote = void 0;
294
+ } else if (character === "\"" || character === "'" || character === "`") quote = character;
295
+ else if (character === opening) depth++;
296
+ else if (character === closing && --depth === 0) return index;
297
+ }
298
+ return -1;
299
+ }
300
+ function tokenizeCommands(script) {
301
+ const commands = [];
302
+ let command = [];
303
+ let value = "";
304
+ let active = false;
305
+ let quoted = false;
306
+ let shellAssignment = false;
307
+ let protectedPrefix = false;
308
+ let quote;
309
+ function flushToken() {
310
+ if (active) {
311
+ command.push({
312
+ value,
313
+ quoted,
314
+ shellAssignment
315
+ });
316
+ value = "";
317
+ active = false;
318
+ quoted = false;
319
+ shellAssignment = false;
320
+ protectedPrefix = false;
321
+ }
322
+ }
323
+ function flushCommand() {
324
+ flushToken();
325
+ if (command.length) {
326
+ commands.push(command);
327
+ command = [];
328
+ }
329
+ }
330
+ for (let index = 0; index < script.length; index++) {
331
+ const character = script[index];
332
+ const next = script[index + 1];
333
+ if (character === "\\" && quote !== "'" && next && /[\s"'\\$`;&|]/u.test(next) || character === "^" && !quote && next && /[\s"^;&|]/u.test(next)) {
334
+ if (next !== "\n" && next !== "\r") {
335
+ protectedPrefix = true;
336
+ value += next;
337
+ active = true;
338
+ } else if (next === "\r" && script[index + 2] === "\n") index++;
339
+ index++;
340
+ } else if (character === "$" && (next === "(" || next === "{") && quote !== "'") {
341
+ const end = readExpansionEnd(script, index, next, next === "(" ? ")" : "}");
342
+ if (end === -1) return [];
343
+ value += script.slice(index, end + 1);
344
+ protectedPrefix = true;
345
+ active = true;
346
+ index = end;
347
+ } else if (quote) {
348
+ if (character === quote) quote = void 0;
349
+ else value += character;
350
+ } else if (character === "\"" || character === "'" || character === "`") {
351
+ quote = character;
352
+ quoted = true;
353
+ protectedPrefix = true;
354
+ active = true;
355
+ if (character === "`") value += character;
356
+ } else if (character === "<" && next === "<") return [];
357
+ else if ((character === ">" || character === "<") && next === "&" || character === "&" && next === ">") {
358
+ value += character + next;
359
+ active = true;
360
+ index++;
361
+ } else if (character === "#" && !active) while (script[index + 1] && script[index + 1] !== "\n") index++;
362
+ else if (character === ";" || character === "&" || character === "|" || character === "\n" || character === "\r") flushCommand();
363
+ else if (/\s/u.test(character)) flushToken();
364
+ else {
365
+ if (character === "=" && !protectedPrefix && ASSIGNMENT_RE.test(`${value}=`)) shellAssignment = true;
366
+ value += character;
367
+ active = true;
368
+ }
369
+ }
370
+ if (quote) return [];
371
+ flushCommand();
372
+ return commands;
373
+ }
374
+ function executableName(value, allowVersion = false) {
375
+ if (allowVersion && value.startsWith("@")) return value;
376
+ const name = node_path.default.win32.basename(node_path.default.posix.basename(value));
377
+ const executable = /\.(?:cmd|bat|exe)$/iu.test(name) ? name.replace(/\.(?:cmd|bat|exe)$/iu, "").toLowerCase() : name;
378
+ return allowVersion ? executable.replace(/@[^@]+$/u, "") : executable;
379
+ }
380
+ function skipAssignments(tokens, index, shellPosition = false) {
381
+ while (tokens[index] && (shellPosition ? tokens[index].shellAssignment : ASSIGNMENT_RE.test(tokens[index].value))) index++;
382
+ return index;
383
+ }
384
+ function skipOptions(tokens, index, flags, valueOptions) {
385
+ while (tokens[index]?.value.startsWith("-")) {
386
+ const value = tokens[index].value;
387
+ if (value === "--") return index + 1;
388
+ if (flags.has(value)) index++;
389
+ else if (valueOptions.has(value)) {
390
+ if (!tokens[index + 1]) return tokens.length;
391
+ index += 2;
392
+ } else if (value.includes("=") && valueOptions.has(value.slice(0, value.indexOf("=")))) index++;
393
+ else return tokens.length;
394
+ }
395
+ return index;
396
+ }
397
+ function collectCommands(script, detected, depth = 0) {
398
+ if (depth > 8) return;
399
+ for (const tokens of tokenizeCommands(script)) {
400
+ let index = skipAssignments(tokens, 0, true);
401
+ let allowVersion = false;
402
+ while (tokens[index]) {
403
+ const name = executableName(tokens[index].value, allowVersion);
404
+ const appType = COMMAND_TYPES.get(name);
405
+ if (appType) {
406
+ detected.add(appType);
407
+ break;
408
+ }
409
+ allowVersion = false;
410
+ index++;
411
+ if (name === "cross-env" || name === "cross-env-shell") {
412
+ index = skipAssignments(tokens, index);
413
+ const payload = tokens[index];
414
+ if (name === "cross-env-shell" && payload?.quoted) {
415
+ collectCommands(payload.value, detected, depth + 1);
416
+ break;
417
+ }
418
+ } else if (name === "env") {
419
+ index = skipOptions(tokens, index, /* @__PURE__ */ new Set(["-i", "--ignore-environment"]), ENV_VALUE_OPTIONS);
420
+ index = skipAssignments(tokens, index);
421
+ } else if (name === "pnpm") {
422
+ index = skipOptions(tokens, index, PNPM_FLAGS, PNPM_VALUE_OPTIONS);
423
+ if (tokens[index]?.value === "exec" || tokens[index]?.value === "dlx") {
424
+ const dlx = tokens[index].value === "dlx";
425
+ index++;
426
+ index = skipOptions(tokens, index, dlx ? NPX_FLAGS : PNPM_FLAGS, dlx ? NPX_VALUE_OPTIONS : PNPM_VALUE_OPTIONS);
427
+ allowVersion = dlx;
428
+ }
429
+ } else if (name === "npx") {
430
+ index = skipOptions(tokens, index, NPX_FLAGS, NPX_VALUE_OPTIONS);
431
+ allowVersion = true;
432
+ } else break;
433
+ }
434
+ }
435
+ }
436
+ /**
437
+ * 仅识别脚本中明确的命令位置,不执行脚本或推断任意参数中的框架名称。
438
+ */
439
+ function detectFrameworkCommands(script) {
440
+ const detected = /* @__PURE__ */ new Set();
441
+ collectCommands(script, detected);
442
+ return [...detected];
443
+ }
444
+ //#endregion
445
+ //#region src/package.ts
446
+ const FRAMEWORK_PRIORITY = [
447
+ "weapp-vite",
448
+ "mpx",
449
+ "taro",
450
+ "uni-app-x",
451
+ "uni-app-vite",
452
+ "uni-app"
453
+ ];
454
+ const DEPENDENCY_FIELDS = [
455
+ "dependencies",
456
+ "devDependencies",
457
+ "peerDependencies",
458
+ "optionalDependencies"
459
+ ];
460
+ function matchesDependency(appType, name) {
461
+ switch (appType) {
462
+ case "weapp-vite": return name === "weapp-vite" || name.startsWith("@weapp-vite/");
463
+ case "mpx": return name.startsWith("@mpxjs/");
464
+ case "taro": return name.startsWith("@tarojs/");
465
+ case "uni-app-x": return name === "@dcloudio/uni-uts-v1" || name === "@weapp-tailwindcss/debug-uni-app-x";
466
+ case "uni-app-vite": return name === "@dcloudio/vite-plugin-uni";
467
+ case "uni-app": return name === "@dcloudio/vue-cli-plugin-uni" || name === "@dcloudio/uni-app";
468
+ }
469
+ }
470
+ function packageMatcher(pkg, file) {
471
+ let scripts;
472
+ return (appType, all) => {
473
+ const result = [];
474
+ for (const field of DEPENDENCY_FIELDS) for (const [name, version] of Object.entries(pkg[field] ?? {})) if (matchesDependency(appType, name)) {
475
+ result.push(evidence("dependency", `${field}.${name}`, version, file));
476
+ if (!all) return result;
477
+ }
478
+ if (appType === "uni-app" && Object.hasOwn(pkg, "uni-app")) {
479
+ result.push(evidence("package-field", "uni-app", void 0, file));
480
+ if (!all) return result;
481
+ }
482
+ if (appType === "uni-app-x") return result;
483
+ scripts ??= Object.entries(pkg.scripts ?? {}).map(([name, value]) => ({
484
+ name,
485
+ value,
486
+ commands: detectFrameworkCommands(value)
487
+ }));
488
+ for (const script of scripts) {
489
+ const commandType = appType === "uni-app" ? "uni-app-vite" : appType;
490
+ if (script.commands.includes(commandType)) {
491
+ result.push(evidence("script", `scripts.${script.name}`, script.value, file));
492
+ if (!all) return result;
493
+ }
494
+ }
495
+ return result;
496
+ };
497
+ }
498
+ function* matchPackageJson(pkg, all, file) {
499
+ const match = packageMatcher(pkg, file);
500
+ for (const appType of FRAMEWORK_PRIORITY) {
501
+ const evidence = match(appType, all);
502
+ if (evidence.length) yield {
503
+ appType,
504
+ evidence
505
+ };
506
+ }
507
+ }
508
+ function isWeappVitePackage(pkg) {
509
+ return packageMatcher(pkg)("weapp-vite", false).length > 0;
510
+ }
511
+ function isMpxPackage(pkg) {
512
+ return packageMatcher(pkg)("mpx", false).length > 0;
513
+ }
514
+ function isTaroPackage(pkg) {
515
+ return packageMatcher(pkg)("taro", false).length > 0;
516
+ }
517
+ function isUniAppVitePackage(pkg) {
518
+ return packageMatcher(pkg)("uni-app-vite", false).length > 0;
519
+ }
520
+ function isUniAppXPackage(pkg) {
521
+ return packageMatcher(pkg)("uni-app-x", false).length > 0;
522
+ }
523
+ function isUniAppPackage(pkg) {
524
+ return packageMatcher(pkg)("uni-app", false).length > 0;
525
+ }
526
+ function isUniAppXManifest(manifest) {
527
+ return Boolean(manifest && Object.hasOwn(manifest, "uni-app-x"));
528
+ }
529
+ function detectAppTypeFromPackageJson(pkg) {
530
+ return collectMatches(matchPackageJson(pkg, false), false)[0]?.appType;
531
+ }
532
+ //#endregion
533
+ //#region src/manifest.ts
534
+ /** 仅移除字符串外的注释,其余语法仍交给严格 JSON 解析。 */
535
+ function parseManifestJson(source) {
536
+ const characters = source.split("");
537
+ let inString = false;
538
+ for (let index = 0; index < source.length; index++) {
539
+ const character = source[index];
540
+ if (inString) {
541
+ if (character === "\\") index++;
542
+ else if (character === "\"") inString = false;
543
+ continue;
544
+ }
545
+ if (character === "\"") {
546
+ inString = true;
547
+ continue;
548
+ }
549
+ if (character !== "/") continue;
550
+ if (source[index + 1] === "/") {
551
+ characters[index] = " ";
552
+ characters[++index] = " ";
553
+ while (index + 1 < source.length && source[index + 1] !== "\n" && source[index + 1] !== "\r") characters[++index] = " ";
554
+ } else if (source[index + 1] === "*") {
555
+ characters[index] = " ";
556
+ characters[++index] = " ";
557
+ let closed = false;
558
+ while (++index < source.length) {
559
+ if (source[index] === "*" && source[index + 1] === "/") {
560
+ characters[index] = " ";
561
+ characters[++index] = " ";
562
+ closed = true;
563
+ break;
564
+ }
565
+ if (source[index] !== "\n" && source[index] !== "\r") characters[index] = " ";
566
+ }
567
+ if (!closed) throw new SyntaxError("manifest.json 中存在未闭合的块注释");
568
+ }
569
+ }
570
+ return JSON.parse(characters.join(""));
571
+ }
572
+ //#endregion
573
+ //#region src/project.ts
574
+ const PACKAGE_JSON_FILE = "package.json";
575
+ const MANIFEST_JSON_FILE = "manifest.json";
576
+ const MPX_MARKERS = [["src", "app.mpx"], ["app.mpx"]];
577
+ function tryReadJson(file, parse = JSON.parse) {
578
+ if (!(0, node_fs.existsSync)(file)) return;
579
+ try {
580
+ return parse((0, node_fs.readFileSync)(file, "utf8"));
581
+ } catch {}
582
+ }
583
+ function manifestMatches(manifest, file) {
584
+ return isUniAppXManifest(manifest) ? [{
585
+ appType: "uni-app-x",
586
+ evidence: [evidence("manifest", "uni-app-x", void 0, file)]
587
+ }] : [];
588
+ }
589
+ function matchRoot(root, searchUp, all) {
590
+ const resolvedRoot = node_path.default.resolve(root);
591
+ if (!(0, node_fs.existsSync)(resolvedRoot)) return [];
592
+ const markers = [];
593
+ for (const segments of MPX_MARKERS) {
594
+ const file = node_path.default.join(resolvedRoot, ...segments);
595
+ if ((0, node_fs.existsSync)(file)) {
596
+ markers.push(evidence("marker", "app.mpx", void 0, file));
597
+ if (!all) break;
598
+ }
599
+ }
600
+ if (markers.length) return [{
601
+ appType: "mpx",
602
+ evidence: markers
603
+ }];
604
+ let current = resolvedRoot;
605
+ while (true) {
606
+ const manifestFile = node_path.default.join(current, MANIFEST_JSON_FILE);
607
+ const manifest = manifestMatches(tryReadJson(manifestFile, parseManifestJson), manifestFile);
608
+ if (manifest.length) return manifest;
609
+ const packageFile = node_path.default.join(current, PACKAGE_JSON_FILE);
610
+ const pkg = tryReadJson(packageFile);
611
+ if (pkg) {
612
+ const candidates = collectMatches(matchPackageJson(pkg, all, packageFile), all);
613
+ if (candidates.length) return candidates;
614
+ }
615
+ if (!searchUp) break;
616
+ const parent = node_path.default.dirname(current);
617
+ if (parent === current) break;
618
+ current = parent;
619
+ }
620
+ return [];
621
+ }
622
+ function matchAppType(options, all) {
623
+ const manifest = manifestMatches(options.manifest);
624
+ if (manifest.length) return manifest;
625
+ if (options.packageJson) {
626
+ const candidates = collectMatches(matchPackageJson(options.packageJson, all), all);
627
+ if (candidates.length) return candidates;
628
+ }
629
+ if (options.root) {
630
+ const candidates = matchRoot(options.root, options.searchUp ?? true, all);
631
+ if (candidates.length) return candidates;
632
+ }
633
+ const env = options.env ?? (options.detectEnv ? getProcessEnv() : void 0);
634
+ if (!env) return [];
635
+ return collectMatches(matchEnvironment(env, {
636
+ cwd: options.cwd ?? options.root ?? (options.detectEnv ? node_process.default.cwd() : void 0),
637
+ hbuilderxVite: options.hbuilderxVite
638
+ }), all);
639
+ }
640
+ function detectAppType(options = {}) {
641
+ return matchAppType(options, false)[0]?.appType;
642
+ }
643
+ /** 解释首次命中阶段的框架候选与冲突,不扩大项目搜索范围。 */
644
+ function detectAppTypeReport(options = {}) {
645
+ return detectionReport(matchAppType(options, true));
646
+ }
647
+ //#endregion
648
+ exports.detectAppType = detectAppType;
649
+ exports.detectAppTypeFromEnv = detectAppTypeFromEnv;
650
+ exports.detectAppTypeFromPackageJson = detectAppTypeFromPackageJson;
651
+ exports.detectAppTypeReport = detectAppTypeReport;
652
+ exports.isMpxPackage = isMpxPackage;
653
+ exports.isRunningInHBuilderX = isRunningInHBuilderX;
654
+ exports.isTaroPackage = isTaroPackage;
655
+ exports.isUniAppPackage = isUniAppPackage;
656
+ exports.isUniAppVitePackage = isUniAppVitePackage;
657
+ exports.isUniAppXManifest = isUniAppXManifest;
658
+ exports.isUniAppXPackage = isUniAppXPackage;
659
+ exports.isWeappVitePackage = isWeappVitePackage;
660
+ exports.normalizeFrameworkStylePlatform = normalizeFrameworkStylePlatform;
661
+ exports.resolveMpxPlatform = resolveMpxPlatform;
662
+ exports.resolveMpxPlatformFromEnv = resolveMpxPlatformFromEnv;
663
+ exports.resolvePlatform = resolvePlatform;
664
+ exports.resolvePlatformIdentity = resolvePlatformIdentity;
665
+ exports.resolveTaroPlatform = resolveTaroPlatform;
666
+ exports.resolveTaroPlatformFromEnv = resolveTaroPlatformFromEnv;
667
+ exports.resolveUniAppXPlatform = resolveUniAppXPlatform;
668
+ exports.resolveUniPlatform = resolveUniPlatform;
669
+ exports.resolveUniPlatformsFromEnv = resolveUniPlatformsFromEnv;
670
+ exports.resolveUniUtsPlatform = resolveUniUtsPlatform;