@weapp-tailwindcss/env 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +34 -2
- package/README.zh-CN.md +35 -0
- package/dist/index.cjs +274 -0
- package/dist/index.d.cts +108 -0
- package/dist/index.d.ts +108 -0
- package/dist/index.js +231 -0
- package/package.json +57 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 ice breaker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,35 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @weapp-tailwindcss/env
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> English | [简体中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
Node.js build-time helpers for detecting mini-app frameworks, platforms and HBuilderX. The package has no runtime dependencies and supports ESM and CommonJS.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @weapp-tailwindcss/env
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { detectAppType, normalizeFrameworkStylePlatform, resolveUniPlatformsFromEnv } from '@weapp-tailwindcss/env'
|
|
17
|
+
|
|
18
|
+
const appType = detectAppType({ root: process.cwd(), detectEnv: true })
|
|
19
|
+
const platform = normalizeFrameworkStylePlatform('wx', 'mpx') // mp-weixin
|
|
20
|
+
const { uniPlatform } = resolveUniPlatformsFromEnv({ UNI_PLATFORM: 'h5' })
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Framework detection supports weapp-vite, MPX, Taro, uni-app, uni-app Vite and uni-app x. `detectAppType` checks an explicit manifest, explicit package metadata, project files, then environment hints. Parent directory search defaults to enabled; use `searchUp: false` to inspect only the supplied root. Unknown frameworks return `undefined`.
|
|
24
|
+
|
|
25
|
+
An explicit `env` object is an isolated snapshot: missing fields never inherit process variables. Without a snapshot, environment helpers read the current process on each call. `detectAppType` only reads process variables when `detectEnv: true`; individual platform resolvers retain their default environment value when called without a value or with `undefined`.
|
|
26
|
+
|
|
27
|
+
## API
|
|
28
|
+
|
|
29
|
+
- Framework detection: `detectAppType`, `detectAppTypeFromEnv`, `detectAppTypeFromPackageJson`.
|
|
30
|
+
- Metadata predicates: `isMpxPackage`, `isTaroPackage`, `isUniAppPackage`, `isUniAppVitePackage`, `isUniAppXPackage`, `isWeappVitePackage`, `isUniAppXManifest`.
|
|
31
|
+
- Environment hints: `isRunningInHBuilderX`.
|
|
32
|
+
- Platforms: `resolvePlatform`, `resolveMpxPlatform`, `resolveTaroPlatform`, `resolveUniPlatform`, `resolveUniUtsPlatform`, `resolveUniAppXPlatform`, `resolveUniPlatformsFromEnv`, `normalizeFrameworkStylePlatform`.
|
|
33
|
+
- Types: `DetectableAppType`, `DetectAppTypeOptions`, `FrameworkEnv`, `HBuilderXRuntimeHints`, `PackageJsonLike`, `UniAppManifestLike`, `UniPlatformInfo`.
|
|
34
|
+
|
|
35
|
+
These helpers report facts. Bundler selection, generator targets and CSS policy belong to the caller. Existing imports from `weapp-tailwindcss` and `weapp-tailwindcss/framework` remain supported. The Vite-specific `resolveImplicitAppTypeFromViteRoot` stays in the main package.
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# @weapp-tailwindcss/env
|
|
2
|
+
|
|
3
|
+
> [English](./README.md) | 简体中文
|
|
4
|
+
|
|
5
|
+
用于 Node.js 构建期的环境、框架、平台与 HBuilderX 识别工具。没有运行时依赖,提供 ESM 和 CommonJS 入口。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @weapp-tailwindcss/env
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 使用
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { detectAppType, normalizeFrameworkStylePlatform, resolveUniPlatformsFromEnv } from '@weapp-tailwindcss/env'
|
|
17
|
+
|
|
18
|
+
const appType = detectAppType({ root: process.cwd(), detectEnv: true })
|
|
19
|
+
const platform = normalizeFrameworkStylePlatform('wx', 'mpx') // mp-weixin
|
|
20
|
+
const { uniPlatform } = resolveUniPlatformsFromEnv({ UNI_PLATFORM: 'h5' })
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
支持 weapp-vite、MPX、Taro、uni-app、uni-app Vite 和 uni-app x。`detectAppType` 按显式 manifest、显式 package 信息、项目文件、环境提示的顺序判断。默认向父目录查找,`searchUp: false` 仅检查传入根目录;无法识别时返回 `undefined`。
|
|
24
|
+
|
|
25
|
+
显式 `env` 是隔离快照:缺失字段不会继承进程环境。省略快照时,环境函数在每次调用时读取当前进程。`detectAppType` 仅在 `detectEnv: true` 时读取进程环境;单值平台函数省略参数或传入 `undefined` 时仍使用对应环境变量。
|
|
26
|
+
|
|
27
|
+
## API
|
|
28
|
+
|
|
29
|
+
- 框架检测:`detectAppType`、`detectAppTypeFromEnv`、`detectAppTypeFromPackageJson`。
|
|
30
|
+
- 配置判断:`isMpxPackage`、`isTaroPackage`、`isUniAppPackage`、`isUniAppVitePackage`、`isUniAppXPackage`、`isWeappVitePackage`、`isUniAppXManifest`。
|
|
31
|
+
- 环境提示:`isRunningInHBuilderX`。
|
|
32
|
+
- 平台:`resolvePlatform`、`resolveMpxPlatform`、`resolveTaroPlatform`、`resolveUniPlatform`、`resolveUniUtsPlatform`、`resolveUniAppXPlatform`、`resolveUniPlatformsFromEnv`、`normalizeFrameworkStylePlatform`。
|
|
33
|
+
- 类型:`DetectableAppType`、`DetectAppTypeOptions`、`FrameworkEnv`、`HBuilderXRuntimeHints`、`PackageJsonLike`、`UniAppManifestLike`、`UniPlatformInfo`。
|
|
34
|
+
|
|
35
|
+
本包提供事实识别,由调用方决定构建器分支、生成器 target 和样式策略。`weapp-tailwindcss` 与 `weapp-tailwindcss/framework` 的既有导入继续可用;Vite 专用的 `resolveImplicitAppTypeFromViteRoot` 保留在主包。
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
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/platform.ts
|
|
66
|
+
const MINI_PROGRAM_PLATFORMS = /* @__PURE__ */ new Set([
|
|
67
|
+
"alipay",
|
|
68
|
+
"baidu",
|
|
69
|
+
"jd",
|
|
70
|
+
"qq",
|
|
71
|
+
"swan",
|
|
72
|
+
"tt",
|
|
73
|
+
"weapp",
|
|
74
|
+
"wechat",
|
|
75
|
+
"wx"
|
|
76
|
+
]);
|
|
77
|
+
function normalizePlatform(value) {
|
|
78
|
+
return value?.trim().toLowerCase() || void 0;
|
|
79
|
+
}
|
|
80
|
+
function resolvePlatformInfo(value) {
|
|
81
|
+
const normalized = normalizePlatform(value);
|
|
82
|
+
const isAppAndroid = normalized === "app-android";
|
|
83
|
+
const isAppIos = normalized === "app-ios";
|
|
84
|
+
const isAppHarmony = normalized === "app-harmony";
|
|
85
|
+
return {
|
|
86
|
+
raw: value,
|
|
87
|
+
normalized,
|
|
88
|
+
isApp: normalized?.startsWith("app-") === true || normalized === "app" || normalized === "app-plus",
|
|
89
|
+
isAppAndroid,
|
|
90
|
+
isAppHarmony,
|
|
91
|
+
isAppIos,
|
|
92
|
+
isMp: normalized?.startsWith("mp-") === true || (normalized ? MINI_PROGRAM_PLATFORMS.has(normalized) : false),
|
|
93
|
+
isWeb: normalized?.startsWith("web") === true || normalized === "h5"
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function resolvePlatform(value) {
|
|
97
|
+
return resolvePlatformInfo(value);
|
|
98
|
+
}
|
|
99
|
+
function resolveTaroPlatform(value = getProcessEnv().TARO_ENV) {
|
|
100
|
+
return resolvePlatform(value);
|
|
101
|
+
}
|
|
102
|
+
function resolveMpxPlatformValue(env = getProcessEnv()) {
|
|
103
|
+
return env.MPX_CURRENT_TARGET_MODE ?? env.MPX_CLI_MODE;
|
|
104
|
+
}
|
|
105
|
+
function resolveMpxPlatform(value = resolveMpxPlatformValue()) {
|
|
106
|
+
return resolvePlatform(value);
|
|
107
|
+
}
|
|
108
|
+
function resolveUniPlatform(value = getProcessEnv().UNI_PLATFORM) {
|
|
109
|
+
return resolvePlatform(value);
|
|
110
|
+
}
|
|
111
|
+
function resolveUniUtsPlatform(value = getProcessEnv().UNI_UTS_PLATFORM) {
|
|
112
|
+
return resolvePlatform(value);
|
|
113
|
+
}
|
|
114
|
+
function resolveUniAppXPlatform(value = getProcessEnv().UNI_UTS_PLATFORM) {
|
|
115
|
+
return resolvePlatform(value);
|
|
116
|
+
}
|
|
117
|
+
function resolveUniPlatformsFromEnv(env = getProcessEnv()) {
|
|
118
|
+
return {
|
|
119
|
+
uniPlatform: resolvePlatform(env.UNI_PLATFORM),
|
|
120
|
+
uniAppXPlatform: resolvePlatform(env.UNI_UTS_PLATFORM),
|
|
121
|
+
uniUtsPlatform: resolvePlatform(env.UNI_UTS_PLATFORM)
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const MPX_TO_UNI_PLATFORM = {
|
|
125
|
+
ali: "mp-alipay",
|
|
126
|
+
dd: "mp-dingtalk",
|
|
127
|
+
jd: "mp-jd",
|
|
128
|
+
qq: "mp-qq",
|
|
129
|
+
swan: "mp-baidu",
|
|
130
|
+
tt: "mp-toutiao",
|
|
131
|
+
wx: "mp-weixin"
|
|
132
|
+
};
|
|
133
|
+
function normalizeFrameworkStylePlatform(platform, appType) {
|
|
134
|
+
const normalized = platform?.trim().toLowerCase();
|
|
135
|
+
if (!normalized) return;
|
|
136
|
+
if (appType === "mpx") return MPX_TO_UNI_PLATFORM[normalized] ?? normalized;
|
|
137
|
+
return normalized;
|
|
138
|
+
}
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/detect-env.ts
|
|
141
|
+
function detectAppTypeFromEnv(env = getProcessEnv(), options = {}) {
|
|
142
|
+
if (env.WEAPP_TW_TARGET === "weapp-vite" || env.WEAPP_TAILWINDCSS_TARGET === "weapp-vite") return "weapp-vite";
|
|
143
|
+
if (env.TARO_ENV) return "taro";
|
|
144
|
+
if (env.MPX_CLI_MODE || env.MPX_CURRENT_TARGET_MODE) return "mpx";
|
|
145
|
+
const { uniPlatform, uniUtsPlatform } = resolveUniPlatformsFromEnv(env);
|
|
146
|
+
if (uniUtsPlatform.normalized) return "uni-app-x";
|
|
147
|
+
if (uniPlatform.normalized) return "uni-app-vite";
|
|
148
|
+
if (isRunningInHBuilderX({
|
|
149
|
+
cwd: options.cwd,
|
|
150
|
+
env
|
|
151
|
+
})) return options.hbuilderxVite === false ? "uni-app" : "uni-app-vite";
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region src/package.ts
|
|
155
|
+
const MPX_SCRIPT_RE = /\bmpx(?:-cli-service)?\b/u;
|
|
156
|
+
const TARO_SCRIPT_RE = /\btaro\b/u;
|
|
157
|
+
const WEAPP_VITE_SCRIPT_RE = /\bweapp-vite\b/u;
|
|
158
|
+
const UNI_APP_SCRIPT_RE = /\buni(?:\s|$)/u;
|
|
159
|
+
const UNI_APP_VITE_SCRIPT_RE = /\buni(?:\s|$)/u;
|
|
160
|
+
function resolveDependencyNames(pkg) {
|
|
161
|
+
return /* @__PURE__ */ new Set([
|
|
162
|
+
...Object.keys(pkg.dependencies ?? {}),
|
|
163
|
+
...Object.keys(pkg.devDependencies ?? {}),
|
|
164
|
+
...Object.keys(pkg.peerDependencies ?? {}),
|
|
165
|
+
...Object.keys(pkg.optionalDependencies ?? {})
|
|
166
|
+
]);
|
|
167
|
+
}
|
|
168
|
+
function hasDependencyPrefix(dependencyNames, prefix) {
|
|
169
|
+
return [...dependencyNames].some((name) => name.startsWith(prefix));
|
|
170
|
+
}
|
|
171
|
+
function hasScriptMatch(pkg, pattern) {
|
|
172
|
+
return Object.values(pkg.scripts ?? {}).some((script) => pattern.test(script));
|
|
173
|
+
}
|
|
174
|
+
function isWeappVitePackage(pkg) {
|
|
175
|
+
const dependencyNames = resolveDependencyNames(pkg);
|
|
176
|
+
return dependencyNames.has("weapp-vite") || hasDependencyPrefix(dependencyNames, "@weapp-vite/") || hasScriptMatch(pkg, WEAPP_VITE_SCRIPT_RE);
|
|
177
|
+
}
|
|
178
|
+
function isMpxPackage(pkg) {
|
|
179
|
+
return hasDependencyPrefix(resolveDependencyNames(pkg), "@mpxjs/") || hasScriptMatch(pkg, MPX_SCRIPT_RE);
|
|
180
|
+
}
|
|
181
|
+
function isTaroPackage(pkg) {
|
|
182
|
+
return hasDependencyPrefix(resolveDependencyNames(pkg), "@tarojs/") || hasScriptMatch(pkg, TARO_SCRIPT_RE);
|
|
183
|
+
}
|
|
184
|
+
function isUniAppVitePackage(pkg) {
|
|
185
|
+
return resolveDependencyNames(pkg).has("@dcloudio/vite-plugin-uni") || hasScriptMatch(pkg, UNI_APP_VITE_SCRIPT_RE);
|
|
186
|
+
}
|
|
187
|
+
function isUniAppXPackage(pkg) {
|
|
188
|
+
const dependencyNames = resolveDependencyNames(pkg);
|
|
189
|
+
return dependencyNames.has("@dcloudio/uni-uts-v1") || dependencyNames.has("@weapp-tailwindcss/debug-uni-app-x");
|
|
190
|
+
}
|
|
191
|
+
function isUniAppPackage(pkg) {
|
|
192
|
+
const dependencyNames = resolveDependencyNames(pkg);
|
|
193
|
+
return dependencyNames.has("@dcloudio/vue-cli-plugin-uni") || dependencyNames.has("@dcloudio/uni-app") || Object.hasOwn(pkg, "uni-app") || hasScriptMatch(pkg, UNI_APP_SCRIPT_RE);
|
|
194
|
+
}
|
|
195
|
+
function isUniAppXManifest(manifest) {
|
|
196
|
+
return Boolean(manifest && Object.hasOwn(manifest, "uni-app-x"));
|
|
197
|
+
}
|
|
198
|
+
function detectAppTypeFromPackageJson(pkg) {
|
|
199
|
+
if (isWeappVitePackage(pkg)) return "weapp-vite";
|
|
200
|
+
if (isMpxPackage(pkg)) return "mpx";
|
|
201
|
+
if (isTaroPackage(pkg)) return "taro";
|
|
202
|
+
if (isUniAppXPackage(pkg)) return "uni-app-x";
|
|
203
|
+
if (isUniAppVitePackage(pkg)) return "uni-app-vite";
|
|
204
|
+
if (isUniAppPackage(pkg)) return "uni-app";
|
|
205
|
+
}
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/project.ts
|
|
208
|
+
const PACKAGE_JSON_FILE = "package.json";
|
|
209
|
+
const MANIFEST_JSON_FILE = "manifest.json";
|
|
210
|
+
const MPX_MARKERS = [["src", "app.mpx"], ["app.mpx"]];
|
|
211
|
+
function tryReadJson(file) {
|
|
212
|
+
if (!(0, node_fs.existsSync)(file)) return;
|
|
213
|
+
try {
|
|
214
|
+
return JSON.parse((0, node_fs.readFileSync)(file, "utf8"));
|
|
215
|
+
} catch {}
|
|
216
|
+
}
|
|
217
|
+
function detectAppTypeFromMarkers(root) {
|
|
218
|
+
for (const segments of MPX_MARKERS) if ((0, node_fs.existsSync)(node_path.default.join(root, ...segments))) return "mpx";
|
|
219
|
+
}
|
|
220
|
+
function detectAppTypeFromRoot(root, searchUp = true) {
|
|
221
|
+
const resolvedRoot = node_path.default.resolve(root);
|
|
222
|
+
if (!(0, node_fs.existsSync)(resolvedRoot)) return;
|
|
223
|
+
const markerDetected = detectAppTypeFromMarkers(resolvedRoot);
|
|
224
|
+
if (markerDetected) return markerDetected;
|
|
225
|
+
let current = resolvedRoot;
|
|
226
|
+
while (true) {
|
|
227
|
+
if (isUniAppXManifest(tryReadJson(node_path.default.join(current, MANIFEST_JSON_FILE)))) return "uni-app-x";
|
|
228
|
+
const pkg = tryReadJson(node_path.default.join(current, PACKAGE_JSON_FILE));
|
|
229
|
+
if (pkg) {
|
|
230
|
+
const detected = detectAppTypeFromPackageJson(pkg);
|
|
231
|
+
if (detected) return detected;
|
|
232
|
+
}
|
|
233
|
+
if (!searchUp) break;
|
|
234
|
+
const parent = node_path.default.dirname(current);
|
|
235
|
+
if (parent === current) break;
|
|
236
|
+
current = parent;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
function detectAppType(options = {}) {
|
|
240
|
+
if (isUniAppXManifest(options.manifest)) return "uni-app-x";
|
|
241
|
+
if (options.packageJson) {
|
|
242
|
+
const detected = detectAppTypeFromPackageJson(options.packageJson);
|
|
243
|
+
if (detected) return detected;
|
|
244
|
+
}
|
|
245
|
+
if (options.root) {
|
|
246
|
+
const detected = detectAppTypeFromRoot(options.root, options.searchUp ?? true);
|
|
247
|
+
if (detected) return detected;
|
|
248
|
+
}
|
|
249
|
+
const env = options.env ?? (options.detectEnv ? getProcessEnv() : void 0);
|
|
250
|
+
if (env) return detectAppTypeFromEnv(env, {
|
|
251
|
+
cwd: options.cwd ?? options.root ?? (options.detectEnv ? node_process.default.cwd() : void 0),
|
|
252
|
+
hbuilderxVite: options.hbuilderxVite
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
//#endregion
|
|
256
|
+
exports.detectAppType = detectAppType;
|
|
257
|
+
exports.detectAppTypeFromEnv = detectAppTypeFromEnv;
|
|
258
|
+
exports.detectAppTypeFromPackageJson = detectAppTypeFromPackageJson;
|
|
259
|
+
exports.isMpxPackage = isMpxPackage;
|
|
260
|
+
exports.isRunningInHBuilderX = isRunningInHBuilderX;
|
|
261
|
+
exports.isTaroPackage = isTaroPackage;
|
|
262
|
+
exports.isUniAppPackage = isUniAppPackage;
|
|
263
|
+
exports.isUniAppVitePackage = isUniAppVitePackage;
|
|
264
|
+
exports.isUniAppXManifest = isUniAppXManifest;
|
|
265
|
+
exports.isUniAppXPackage = isUniAppXPackage;
|
|
266
|
+
exports.isWeappVitePackage = isWeappVitePackage;
|
|
267
|
+
exports.normalizeFrameworkStylePlatform = normalizeFrameworkStylePlatform;
|
|
268
|
+
exports.resolveMpxPlatform = resolveMpxPlatform;
|
|
269
|
+
exports.resolvePlatform = resolvePlatform;
|
|
270
|
+
exports.resolveTaroPlatform = resolveTaroPlatform;
|
|
271
|
+
exports.resolveUniAppXPlatform = resolveUniAppXPlatform;
|
|
272
|
+
exports.resolveUniPlatform = resolveUniPlatform;
|
|
273
|
+
exports.resolveUniPlatformsFromEnv = resolveUniPlatformsFromEnv;
|
|
274
|
+
exports.resolveUniUtsPlatform = resolveUniUtsPlatform;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
type DetectableAppType = 'mpx' | 'taro' | 'uni-app' | 'uni-app-vite' | 'uni-app-x' | 'weapp-vite';
|
|
3
|
+
type FrameworkEnv = Record<string, string | undefined> & Partial<Record<'MPX_CLI_MODE' | 'MPX_CURRENT_TARGET_MODE' | 'NODE_PATH' | 'TARO_ENV' | 'UNI_PLATFORM' | 'UNI_UTS_PLATFORM' | 'WEAPP_TAILWINDCSS_TARGET' | 'WEAPP_TW_TARGET', string | undefined>>;
|
|
4
|
+
interface UniPlatformInfo {
|
|
5
|
+
raw: string | undefined;
|
|
6
|
+
normalized: string | undefined;
|
|
7
|
+
isApp: boolean;
|
|
8
|
+
isAppAndroid: boolean;
|
|
9
|
+
isAppHarmony: boolean;
|
|
10
|
+
isAppIos: boolean;
|
|
11
|
+
isMp: boolean;
|
|
12
|
+
isWeb: boolean;
|
|
13
|
+
}
|
|
14
|
+
interface HBuilderXRuntimeHints {
|
|
15
|
+
cwd?: string | undefined;
|
|
16
|
+
env?: FrameworkEnv | undefined;
|
|
17
|
+
nodePath?: string | null | undefined;
|
|
18
|
+
}
|
|
19
|
+
interface PackageJsonLike {
|
|
20
|
+
'dependencies'?: Record<string, string> | undefined;
|
|
21
|
+
'devDependencies'?: Record<string, string> | undefined;
|
|
22
|
+
'peerDependencies'?: Record<string, string> | undefined;
|
|
23
|
+
'optionalDependencies'?: Record<string, string> | undefined;
|
|
24
|
+
'scripts'?: Record<string, string> | undefined;
|
|
25
|
+
'uni-app'?: unknown;
|
|
26
|
+
}
|
|
27
|
+
interface UniAppManifestLike {
|
|
28
|
+
'uni-app-x'?: unknown;
|
|
29
|
+
}
|
|
30
|
+
interface DetectAppTypeOptions {
|
|
31
|
+
/**
|
|
32
|
+
* 项目根目录。传入后会读取 marker、manifest.json 与 package.json。
|
|
33
|
+
*/
|
|
34
|
+
root?: string | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* 已解析的 package.json 内容。适合在调用方已经读取配置时复用。
|
|
37
|
+
*/
|
|
38
|
+
packageJson?: PackageJsonLike | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* 已解析的 uni-app manifest.json 内容,用于识别 uni-app x。
|
|
41
|
+
*/
|
|
42
|
+
manifest?: UniAppManifestLike | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* 是否从 root 向上查找 package.json 与 manifest.json。
|
|
45
|
+
*
|
|
46
|
+
* @default `true`
|
|
47
|
+
*/
|
|
48
|
+
searchUp?: boolean | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* 环境变量快照。传入后可用于识别 HBuilderX、Taro、MPX 与 uni-app 运行环境。
|
|
51
|
+
* 缺失字段不会回退到进程环境。
|
|
52
|
+
*/
|
|
53
|
+
env?: FrameworkEnv | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* 是否读取当前进程环境变量参与推断。
|
|
56
|
+
*
|
|
57
|
+
* @default `false`
|
|
58
|
+
*/
|
|
59
|
+
detectEnv?: boolean | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* HBuilderX 插件运行时的工作目录。默认使用 `root`,启用 `detectEnv` 时会回退到 `process.cwd()`。
|
|
62
|
+
*/
|
|
63
|
+
cwd?: string | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* 是否在 HBuilderX 插件运行时把 uni-app 识别为 uni-app Vite。
|
|
66
|
+
*
|
|
67
|
+
* @default `true`
|
|
68
|
+
*/
|
|
69
|
+
hbuilderxVite?: boolean | undefined;
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/detect-env.d.ts
|
|
73
|
+
export declare function detectAppTypeFromEnv(env?: FrameworkEnv, options?: {
|
|
74
|
+
cwd?: string | undefined;
|
|
75
|
+
hbuilderxVite?: boolean | undefined;
|
|
76
|
+
}): DetectableAppType | undefined;
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/hbuilderx.d.ts
|
|
79
|
+
export declare function isRunningInHBuilderX(options?: HBuilderXRuntimeHints): boolean;
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/package.d.ts
|
|
82
|
+
export declare function isWeappVitePackage(pkg: PackageJsonLike): boolean;
|
|
83
|
+
export declare function isMpxPackage(pkg: PackageJsonLike): boolean;
|
|
84
|
+
export declare function isTaroPackage(pkg: PackageJsonLike): boolean;
|
|
85
|
+
export declare function isUniAppVitePackage(pkg: PackageJsonLike): boolean;
|
|
86
|
+
export declare function isUniAppXPackage(pkg: PackageJsonLike): boolean;
|
|
87
|
+
export declare function isUniAppPackage(pkg: PackageJsonLike): boolean;
|
|
88
|
+
export declare function isUniAppXManifest(manifest: UniAppManifestLike | undefined): boolean;
|
|
89
|
+
export declare function detectAppTypeFromPackageJson(pkg: PackageJsonLike): DetectableAppType | undefined;
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/platform.d.ts
|
|
92
|
+
export declare function resolvePlatform(value: string | undefined): UniPlatformInfo;
|
|
93
|
+
export declare function resolveTaroPlatform(value?: string | undefined): UniPlatformInfo;
|
|
94
|
+
export declare function resolveMpxPlatform(value?: string | undefined): UniPlatformInfo;
|
|
95
|
+
export declare function resolveUniPlatform(value?: string | undefined): UniPlatformInfo;
|
|
96
|
+
export declare function resolveUniUtsPlatform(value?: string | undefined): UniPlatformInfo;
|
|
97
|
+
export declare function resolveUniAppXPlatform(value?: string | undefined): UniPlatformInfo;
|
|
98
|
+
export declare function resolveUniPlatformsFromEnv(env?: FrameworkEnv): {
|
|
99
|
+
uniPlatform: UniPlatformInfo;
|
|
100
|
+
uniAppXPlatform: UniPlatformInfo;
|
|
101
|
+
uniUtsPlatform: UniPlatformInfo;
|
|
102
|
+
};
|
|
103
|
+
export declare function normalizeFrameworkStylePlatform(platform: string | undefined, appType: string | undefined): string | undefined;
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/project.d.ts
|
|
106
|
+
export declare function detectAppType(options?: DetectAppTypeOptions): DetectableAppType | undefined;
|
|
107
|
+
//#endregion
|
|
108
|
+
export type { DetectAppTypeOptions, DetectableAppType, FrameworkEnv, HBuilderXRuntimeHints, PackageJsonLike, UniAppManifestLike, UniPlatformInfo };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
type DetectableAppType = 'mpx' | 'taro' | 'uni-app' | 'uni-app-vite' | 'uni-app-x' | 'weapp-vite';
|
|
3
|
+
type FrameworkEnv = Record<string, string | undefined> & Partial<Record<'MPX_CLI_MODE' | 'MPX_CURRENT_TARGET_MODE' | 'NODE_PATH' | 'TARO_ENV' | 'UNI_PLATFORM' | 'UNI_UTS_PLATFORM' | 'WEAPP_TAILWINDCSS_TARGET' | 'WEAPP_TW_TARGET', string | undefined>>;
|
|
4
|
+
interface UniPlatformInfo {
|
|
5
|
+
raw: string | undefined;
|
|
6
|
+
normalized: string | undefined;
|
|
7
|
+
isApp: boolean;
|
|
8
|
+
isAppAndroid: boolean;
|
|
9
|
+
isAppHarmony: boolean;
|
|
10
|
+
isAppIos: boolean;
|
|
11
|
+
isMp: boolean;
|
|
12
|
+
isWeb: boolean;
|
|
13
|
+
}
|
|
14
|
+
interface HBuilderXRuntimeHints {
|
|
15
|
+
cwd?: string | undefined;
|
|
16
|
+
env?: FrameworkEnv | undefined;
|
|
17
|
+
nodePath?: string | null | undefined;
|
|
18
|
+
}
|
|
19
|
+
interface PackageJsonLike {
|
|
20
|
+
'dependencies'?: Record<string, string> | undefined;
|
|
21
|
+
'devDependencies'?: Record<string, string> | undefined;
|
|
22
|
+
'peerDependencies'?: Record<string, string> | undefined;
|
|
23
|
+
'optionalDependencies'?: Record<string, string> | undefined;
|
|
24
|
+
'scripts'?: Record<string, string> | undefined;
|
|
25
|
+
'uni-app'?: unknown;
|
|
26
|
+
}
|
|
27
|
+
interface UniAppManifestLike {
|
|
28
|
+
'uni-app-x'?: unknown;
|
|
29
|
+
}
|
|
30
|
+
interface DetectAppTypeOptions {
|
|
31
|
+
/**
|
|
32
|
+
* 项目根目录。传入后会读取 marker、manifest.json 与 package.json。
|
|
33
|
+
*/
|
|
34
|
+
root?: string | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* 已解析的 package.json 内容。适合在调用方已经读取配置时复用。
|
|
37
|
+
*/
|
|
38
|
+
packageJson?: PackageJsonLike | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* 已解析的 uni-app manifest.json 内容,用于识别 uni-app x。
|
|
41
|
+
*/
|
|
42
|
+
manifest?: UniAppManifestLike | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* 是否从 root 向上查找 package.json 与 manifest.json。
|
|
45
|
+
*
|
|
46
|
+
* @default `true`
|
|
47
|
+
*/
|
|
48
|
+
searchUp?: boolean | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* 环境变量快照。传入后可用于识别 HBuilderX、Taro、MPX 与 uni-app 运行环境。
|
|
51
|
+
* 缺失字段不会回退到进程环境。
|
|
52
|
+
*/
|
|
53
|
+
env?: FrameworkEnv | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* 是否读取当前进程环境变量参与推断。
|
|
56
|
+
*
|
|
57
|
+
* @default `false`
|
|
58
|
+
*/
|
|
59
|
+
detectEnv?: boolean | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* HBuilderX 插件运行时的工作目录。默认使用 `root`,启用 `detectEnv` 时会回退到 `process.cwd()`。
|
|
62
|
+
*/
|
|
63
|
+
cwd?: string | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* 是否在 HBuilderX 插件运行时把 uni-app 识别为 uni-app Vite。
|
|
66
|
+
*
|
|
67
|
+
* @default `true`
|
|
68
|
+
*/
|
|
69
|
+
hbuilderxVite?: boolean | undefined;
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/detect-env.d.ts
|
|
73
|
+
export declare function detectAppTypeFromEnv(env?: FrameworkEnv, options?: {
|
|
74
|
+
cwd?: string | undefined;
|
|
75
|
+
hbuilderxVite?: boolean | undefined;
|
|
76
|
+
}): DetectableAppType | undefined;
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/hbuilderx.d.ts
|
|
79
|
+
export declare function isRunningInHBuilderX(options?: HBuilderXRuntimeHints): boolean;
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/package.d.ts
|
|
82
|
+
export declare function isWeappVitePackage(pkg: PackageJsonLike): boolean;
|
|
83
|
+
export declare function isMpxPackage(pkg: PackageJsonLike): boolean;
|
|
84
|
+
export declare function isTaroPackage(pkg: PackageJsonLike): boolean;
|
|
85
|
+
export declare function isUniAppVitePackage(pkg: PackageJsonLike): boolean;
|
|
86
|
+
export declare function isUniAppXPackage(pkg: PackageJsonLike): boolean;
|
|
87
|
+
export declare function isUniAppPackage(pkg: PackageJsonLike): boolean;
|
|
88
|
+
export declare function isUniAppXManifest(manifest: UniAppManifestLike | undefined): boolean;
|
|
89
|
+
export declare function detectAppTypeFromPackageJson(pkg: PackageJsonLike): DetectableAppType | undefined;
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/platform.d.ts
|
|
92
|
+
export declare function resolvePlatform(value: string | undefined): UniPlatformInfo;
|
|
93
|
+
export declare function resolveTaroPlatform(value?: string | undefined): UniPlatformInfo;
|
|
94
|
+
export declare function resolveMpxPlatform(value?: string | undefined): UniPlatformInfo;
|
|
95
|
+
export declare function resolveUniPlatform(value?: string | undefined): UniPlatformInfo;
|
|
96
|
+
export declare function resolveUniUtsPlatform(value?: string | undefined): UniPlatformInfo;
|
|
97
|
+
export declare function resolveUniAppXPlatform(value?: string | undefined): UniPlatformInfo;
|
|
98
|
+
export declare function resolveUniPlatformsFromEnv(env?: FrameworkEnv): {
|
|
99
|
+
uniPlatform: UniPlatformInfo;
|
|
100
|
+
uniAppXPlatform: UniPlatformInfo;
|
|
101
|
+
uniUtsPlatform: UniPlatformInfo;
|
|
102
|
+
};
|
|
103
|
+
export declare function normalizeFrameworkStylePlatform(platform: string | undefined, appType: string | undefined): string | undefined;
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/project.d.ts
|
|
106
|
+
export declare function detectAppType(options?: DetectAppTypeOptions): DetectableAppType | undefined;
|
|
107
|
+
//#endregion
|
|
108
|
+
export type { DetectAppTypeOptions, DetectableAppType, FrameworkEnv, HBuilderXRuntimeHints, PackageJsonLike, UniAppManifestLike, UniPlatformInfo };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import process from "node:process";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
//#region src/environment.ts
|
|
5
|
+
function getProcessEnv() {
|
|
6
|
+
return {
|
|
7
|
+
MPX_CLI_MODE: process.env["MPX_CLI_MODE"],
|
|
8
|
+
MPX_CURRENT_TARGET_MODE: process.env["MPX_CURRENT_TARGET_MODE"],
|
|
9
|
+
NODE_PATH: process.env["NODE_PATH"],
|
|
10
|
+
TARO_ENV: process.env["TARO_ENV"],
|
|
11
|
+
UNI_PLATFORM: process.env["UNI_PLATFORM"],
|
|
12
|
+
UNI_UTS_PLATFORM: process.env["UNI_UTS_PLATFORM"],
|
|
13
|
+
WEAPP_TAILWINDCSS_TARGET: process.env["WEAPP_TAILWINDCSS_TARGET"],
|
|
14
|
+
WEAPP_TW_TARGET: process.env["WEAPP_TW_TARGET"]
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/hbuilderx.ts
|
|
19
|
+
const KNOWN_MAC_HBUILDERX_PLUGIN_DIRS = ["/Applications/HBuilderX.app/Contents/HBuilderX/plugins/uniapp-cli-vite", "/Applications/HBuilderX.app/Contents/HBuilderX/plugins/uniapp-cli"];
|
|
20
|
+
const HBUILDERX_PLUGIN_CWD_RE = /[\\/]HBuilderX(?:\.[^\\/]*)?(?:[\\/]Contents[\\/]HBuilderX)?[\\/]plugins[\\/]uniapp-cli(?:-vite)?(?:[\\/]|$)/i;
|
|
21
|
+
function hasKnownHBuilderXPluginPrefix(normalizedCwd) {
|
|
22
|
+
for (const dir of KNOWN_MAC_HBUILDERX_PLUGIN_DIRS) {
|
|
23
|
+
const normalizedDir = path.normalize(dir);
|
|
24
|
+
if (normalizedCwd === normalizedDir || normalizedCwd.startsWith(`${normalizedDir}${path.sep}`)) return true;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
function matchesHBuilderXPluginCwd(cwd) {
|
|
29
|
+
const normalized = path.normalize(cwd);
|
|
30
|
+
if (hasKnownHBuilderXPluginPrefix(normalized)) return true;
|
|
31
|
+
return HBUILDERX_PLUGIN_CWD_RE.test(normalized);
|
|
32
|
+
}
|
|
33
|
+
function isRunningInHBuilderX(options = {}) {
|
|
34
|
+
const env = options.env ?? getProcessEnv();
|
|
35
|
+
const nodePath = "nodePath" in options ? options.nodePath : env.NODE_PATH;
|
|
36
|
+
if (!(nodePath == null || nodePath.trim().length === 0)) return false;
|
|
37
|
+
return matchesHBuilderXPluginCwd(options.cwd ?? process.cwd());
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/platform.ts
|
|
41
|
+
const MINI_PROGRAM_PLATFORMS = /* @__PURE__ */ new Set([
|
|
42
|
+
"alipay",
|
|
43
|
+
"baidu",
|
|
44
|
+
"jd",
|
|
45
|
+
"qq",
|
|
46
|
+
"swan",
|
|
47
|
+
"tt",
|
|
48
|
+
"weapp",
|
|
49
|
+
"wechat",
|
|
50
|
+
"wx"
|
|
51
|
+
]);
|
|
52
|
+
function normalizePlatform(value) {
|
|
53
|
+
return value?.trim().toLowerCase() || void 0;
|
|
54
|
+
}
|
|
55
|
+
function resolvePlatformInfo(value) {
|
|
56
|
+
const normalized = normalizePlatform(value);
|
|
57
|
+
const isAppAndroid = normalized === "app-android";
|
|
58
|
+
const isAppIos = normalized === "app-ios";
|
|
59
|
+
const isAppHarmony = normalized === "app-harmony";
|
|
60
|
+
return {
|
|
61
|
+
raw: value,
|
|
62
|
+
normalized,
|
|
63
|
+
isApp: normalized?.startsWith("app-") === true || normalized === "app" || normalized === "app-plus",
|
|
64
|
+
isAppAndroid,
|
|
65
|
+
isAppHarmony,
|
|
66
|
+
isAppIos,
|
|
67
|
+
isMp: normalized?.startsWith("mp-") === true || (normalized ? MINI_PROGRAM_PLATFORMS.has(normalized) : false),
|
|
68
|
+
isWeb: normalized?.startsWith("web") === true || normalized === "h5"
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function resolvePlatform(value) {
|
|
72
|
+
return resolvePlatformInfo(value);
|
|
73
|
+
}
|
|
74
|
+
function resolveTaroPlatform(value = getProcessEnv().TARO_ENV) {
|
|
75
|
+
return resolvePlatform(value);
|
|
76
|
+
}
|
|
77
|
+
function resolveMpxPlatformValue(env = getProcessEnv()) {
|
|
78
|
+
return env.MPX_CURRENT_TARGET_MODE ?? env.MPX_CLI_MODE;
|
|
79
|
+
}
|
|
80
|
+
function resolveMpxPlatform(value = resolveMpxPlatformValue()) {
|
|
81
|
+
return resolvePlatform(value);
|
|
82
|
+
}
|
|
83
|
+
function resolveUniPlatform(value = getProcessEnv().UNI_PLATFORM) {
|
|
84
|
+
return resolvePlatform(value);
|
|
85
|
+
}
|
|
86
|
+
function resolveUniUtsPlatform(value = getProcessEnv().UNI_UTS_PLATFORM) {
|
|
87
|
+
return resolvePlatform(value);
|
|
88
|
+
}
|
|
89
|
+
function resolveUniAppXPlatform(value = getProcessEnv().UNI_UTS_PLATFORM) {
|
|
90
|
+
return resolvePlatform(value);
|
|
91
|
+
}
|
|
92
|
+
function resolveUniPlatformsFromEnv(env = getProcessEnv()) {
|
|
93
|
+
return {
|
|
94
|
+
uniPlatform: resolvePlatform(env.UNI_PLATFORM),
|
|
95
|
+
uniAppXPlatform: resolvePlatform(env.UNI_UTS_PLATFORM),
|
|
96
|
+
uniUtsPlatform: resolvePlatform(env.UNI_UTS_PLATFORM)
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const MPX_TO_UNI_PLATFORM = {
|
|
100
|
+
ali: "mp-alipay",
|
|
101
|
+
dd: "mp-dingtalk",
|
|
102
|
+
jd: "mp-jd",
|
|
103
|
+
qq: "mp-qq",
|
|
104
|
+
swan: "mp-baidu",
|
|
105
|
+
tt: "mp-toutiao",
|
|
106
|
+
wx: "mp-weixin"
|
|
107
|
+
};
|
|
108
|
+
function normalizeFrameworkStylePlatform(platform, appType) {
|
|
109
|
+
const normalized = platform?.trim().toLowerCase();
|
|
110
|
+
if (!normalized) return;
|
|
111
|
+
if (appType === "mpx") return MPX_TO_UNI_PLATFORM[normalized] ?? normalized;
|
|
112
|
+
return normalized;
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/detect-env.ts
|
|
116
|
+
function detectAppTypeFromEnv(env = getProcessEnv(), options = {}) {
|
|
117
|
+
if (env.WEAPP_TW_TARGET === "weapp-vite" || env.WEAPP_TAILWINDCSS_TARGET === "weapp-vite") return "weapp-vite";
|
|
118
|
+
if (env.TARO_ENV) return "taro";
|
|
119
|
+
if (env.MPX_CLI_MODE || env.MPX_CURRENT_TARGET_MODE) return "mpx";
|
|
120
|
+
const { uniPlatform, uniUtsPlatform } = resolveUniPlatformsFromEnv(env);
|
|
121
|
+
if (uniUtsPlatform.normalized) return "uni-app-x";
|
|
122
|
+
if (uniPlatform.normalized) return "uni-app-vite";
|
|
123
|
+
if (isRunningInHBuilderX({
|
|
124
|
+
cwd: options.cwd,
|
|
125
|
+
env
|
|
126
|
+
})) return options.hbuilderxVite === false ? "uni-app" : "uni-app-vite";
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/package.ts
|
|
130
|
+
const MPX_SCRIPT_RE = /\bmpx(?:-cli-service)?\b/u;
|
|
131
|
+
const TARO_SCRIPT_RE = /\btaro\b/u;
|
|
132
|
+
const WEAPP_VITE_SCRIPT_RE = /\bweapp-vite\b/u;
|
|
133
|
+
const UNI_APP_SCRIPT_RE = /\buni(?:\s|$)/u;
|
|
134
|
+
const UNI_APP_VITE_SCRIPT_RE = /\buni(?:\s|$)/u;
|
|
135
|
+
function resolveDependencyNames(pkg) {
|
|
136
|
+
return /* @__PURE__ */ new Set([
|
|
137
|
+
...Object.keys(pkg.dependencies ?? {}),
|
|
138
|
+
...Object.keys(pkg.devDependencies ?? {}),
|
|
139
|
+
...Object.keys(pkg.peerDependencies ?? {}),
|
|
140
|
+
...Object.keys(pkg.optionalDependencies ?? {})
|
|
141
|
+
]);
|
|
142
|
+
}
|
|
143
|
+
function hasDependencyPrefix(dependencyNames, prefix) {
|
|
144
|
+
return [...dependencyNames].some((name) => name.startsWith(prefix));
|
|
145
|
+
}
|
|
146
|
+
function hasScriptMatch(pkg, pattern) {
|
|
147
|
+
return Object.values(pkg.scripts ?? {}).some((script) => pattern.test(script));
|
|
148
|
+
}
|
|
149
|
+
function isWeappVitePackage(pkg) {
|
|
150
|
+
const dependencyNames = resolveDependencyNames(pkg);
|
|
151
|
+
return dependencyNames.has("weapp-vite") || hasDependencyPrefix(dependencyNames, "@weapp-vite/") || hasScriptMatch(pkg, WEAPP_VITE_SCRIPT_RE);
|
|
152
|
+
}
|
|
153
|
+
function isMpxPackage(pkg) {
|
|
154
|
+
return hasDependencyPrefix(resolveDependencyNames(pkg), "@mpxjs/") || hasScriptMatch(pkg, MPX_SCRIPT_RE);
|
|
155
|
+
}
|
|
156
|
+
function isTaroPackage(pkg) {
|
|
157
|
+
return hasDependencyPrefix(resolveDependencyNames(pkg), "@tarojs/") || hasScriptMatch(pkg, TARO_SCRIPT_RE);
|
|
158
|
+
}
|
|
159
|
+
function isUniAppVitePackage(pkg) {
|
|
160
|
+
return resolveDependencyNames(pkg).has("@dcloudio/vite-plugin-uni") || hasScriptMatch(pkg, UNI_APP_VITE_SCRIPT_RE);
|
|
161
|
+
}
|
|
162
|
+
function isUniAppXPackage(pkg) {
|
|
163
|
+
const dependencyNames = resolveDependencyNames(pkg);
|
|
164
|
+
return dependencyNames.has("@dcloudio/uni-uts-v1") || dependencyNames.has("@weapp-tailwindcss/debug-uni-app-x");
|
|
165
|
+
}
|
|
166
|
+
function isUniAppPackage(pkg) {
|
|
167
|
+
const dependencyNames = resolveDependencyNames(pkg);
|
|
168
|
+
return dependencyNames.has("@dcloudio/vue-cli-plugin-uni") || dependencyNames.has("@dcloudio/uni-app") || Object.hasOwn(pkg, "uni-app") || hasScriptMatch(pkg, UNI_APP_SCRIPT_RE);
|
|
169
|
+
}
|
|
170
|
+
function isUniAppXManifest(manifest) {
|
|
171
|
+
return Boolean(manifest && Object.hasOwn(manifest, "uni-app-x"));
|
|
172
|
+
}
|
|
173
|
+
function detectAppTypeFromPackageJson(pkg) {
|
|
174
|
+
if (isWeappVitePackage(pkg)) return "weapp-vite";
|
|
175
|
+
if (isMpxPackage(pkg)) return "mpx";
|
|
176
|
+
if (isTaroPackage(pkg)) return "taro";
|
|
177
|
+
if (isUniAppXPackage(pkg)) return "uni-app-x";
|
|
178
|
+
if (isUniAppVitePackage(pkg)) return "uni-app-vite";
|
|
179
|
+
if (isUniAppPackage(pkg)) return "uni-app";
|
|
180
|
+
}
|
|
181
|
+
//#endregion
|
|
182
|
+
//#region src/project.ts
|
|
183
|
+
const PACKAGE_JSON_FILE = "package.json";
|
|
184
|
+
const MANIFEST_JSON_FILE = "manifest.json";
|
|
185
|
+
const MPX_MARKERS = [["src", "app.mpx"], ["app.mpx"]];
|
|
186
|
+
function tryReadJson(file) {
|
|
187
|
+
if (!existsSync(file)) return;
|
|
188
|
+
try {
|
|
189
|
+
return JSON.parse(readFileSync(file, "utf8"));
|
|
190
|
+
} catch {}
|
|
191
|
+
}
|
|
192
|
+
function detectAppTypeFromMarkers(root) {
|
|
193
|
+
for (const segments of MPX_MARKERS) if (existsSync(path.join(root, ...segments))) return "mpx";
|
|
194
|
+
}
|
|
195
|
+
function detectAppTypeFromRoot(root, searchUp = true) {
|
|
196
|
+
const resolvedRoot = path.resolve(root);
|
|
197
|
+
if (!existsSync(resolvedRoot)) return;
|
|
198
|
+
const markerDetected = detectAppTypeFromMarkers(resolvedRoot);
|
|
199
|
+
if (markerDetected) return markerDetected;
|
|
200
|
+
let current = resolvedRoot;
|
|
201
|
+
while (true) {
|
|
202
|
+
if (isUniAppXManifest(tryReadJson(path.join(current, MANIFEST_JSON_FILE)))) return "uni-app-x";
|
|
203
|
+
const pkg = tryReadJson(path.join(current, PACKAGE_JSON_FILE));
|
|
204
|
+
if (pkg) {
|
|
205
|
+
const detected = detectAppTypeFromPackageJson(pkg);
|
|
206
|
+
if (detected) return detected;
|
|
207
|
+
}
|
|
208
|
+
if (!searchUp) break;
|
|
209
|
+
const parent = path.dirname(current);
|
|
210
|
+
if (parent === current) break;
|
|
211
|
+
current = parent;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function detectAppType(options = {}) {
|
|
215
|
+
if (isUniAppXManifest(options.manifest)) return "uni-app-x";
|
|
216
|
+
if (options.packageJson) {
|
|
217
|
+
const detected = detectAppTypeFromPackageJson(options.packageJson);
|
|
218
|
+
if (detected) return detected;
|
|
219
|
+
}
|
|
220
|
+
if (options.root) {
|
|
221
|
+
const detected = detectAppTypeFromRoot(options.root, options.searchUp ?? true);
|
|
222
|
+
if (detected) return detected;
|
|
223
|
+
}
|
|
224
|
+
const env = options.env ?? (options.detectEnv ? getProcessEnv() : void 0);
|
|
225
|
+
if (env) return detectAppTypeFromEnv(env, {
|
|
226
|
+
cwd: options.cwd ?? options.root ?? (options.detectEnv ? process.cwd() : void 0),
|
|
227
|
+
hbuilderxVite: options.hbuilderxVite
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
//#endregion
|
|
231
|
+
export { detectAppType, detectAppTypeFromEnv, detectAppTypeFromPackageJson, isMpxPackage, isRunningInHBuilderX, isTaroPackage, isUniAppPackage, isUniAppVitePackage, isUniAppXManifest, isUniAppXPackage, isWeappVitePackage, normalizeFrameworkStylePlatform, resolveMpxPlatform, resolvePlatform, resolveTaroPlatform, resolveUniAppXPlatform, resolveUniPlatform, resolveUniPlatformsFromEnv, resolveUniUtsPlatform };
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weapp-tailwindcss/env",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "构建期的环境、框架与平台识别工具。",
|
|
6
|
+
"author": "ice breaker <1324318532@qq.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://tw.weapp.dev",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/weapp-tailwindcss/weapp-tailwindcss.git",
|
|
12
|
+
"directory": "packages/env"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/weapp-tailwindcss/weapp-tailwindcss/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"weapp",
|
|
19
|
+
"framework",
|
|
20
|
+
"environment",
|
|
21
|
+
"taro",
|
|
22
|
+
"uni-app",
|
|
23
|
+
"mpx"
|
|
24
|
+
],
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./dist/index.d.cts",
|
|
34
|
+
"default": "./dist/index.cjs"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"main": "./dist/index.cjs",
|
|
39
|
+
"module": "./dist/index.js",
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"files": [
|
|
42
|
+
"dist"
|
|
43
|
+
],
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": "^22.18.0 || >=24.11.0"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public",
|
|
49
|
+
"registry": "https://registry.npmjs.org/"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"dev": "tsdown --watch --sourcemap",
|
|
53
|
+
"build": "tsdown",
|
|
54
|
+
"test": "vitest run",
|
|
55
|
+
"test:dev": "vitest",
|
|
56
|
+
"typecheck": "tsc --noEmit",
|
|
57
|
+
"lint": "eslint .",
|
|
58
|
+
"test:package": "node scripts/verify-package.mjs"
|
|
59
|
+
}
|
|
6
60
|
}
|