UNPKG

weapp-tailwindcss

Version:

把 tailwindcss 原子化样式思想,带给小程序开发者们! bring tailwindcss to miniprogram developers!

208 lines (207 loc) 8.67 kB
import { existsSync, readFileSync } from "node:fs"; import path from "node:path"; import process from "node:process"; //#region src/framework/index.ts const KNOWN_MAC_HBUILDERX_PLUGIN_DIRS = ["/Applications/HBuilderX.app/Contents/HBuilderX/plugins/uniapp-cli-vite", "/Applications/HBuilderX.app/Contents/HBuilderX/plugins/uniapp-cli"]; const HBUILDERX_PLUGIN_CWD_RE = /[\\/]HBuilderX(?:\.[^\\/]*)?(?:[\\/]Contents[\\/]HBuilderX)?[\\/]plugins[\\/]uniapp-cli(?:-vite)?(?:[\\/]|$)/i; const PACKAGE_JSON_FILE = "package.json"; const MANIFEST_JSON_FILE = "manifest.json"; const MPX_SCRIPT_RE = /\bmpx(?:-cli-service)?\b/u; const TARO_SCRIPT_RE = /\btaro\b/u; const WEAPP_VITE_SCRIPT_RE = /\bweapp-vite\b/u; const UNI_APP_SCRIPT_RE = /\buni(?:\s|$)/u; const UNI_APP_VITE_SCRIPT_RE = /\buni(?:\s|$)/u; const MINI_PROGRAM_PLATFORMS = /* @__PURE__ */ new Set([ "alipay", "baidu", "jd", "qq", "swan", "tt", "weapp", "wechat", "wx" ]); const MPX_MARKERS = [["src/app.mpx", "mpx"], ["app.mpx", "mpx"]]; function getProcessEnv() { return { MPX_CLI_MODE: process.env["MPX_CLI_MODE"], MPX_CURRENT_TARGET_MODE: process.env["MPX_CURRENT_TARGET_MODE"], NODE_PATH: process.env["NODE_PATH"], TARO_ENV: process.env["TARO_ENV"], UNI_PLATFORM: process.env["UNI_PLATFORM"], UNI_UTS_PLATFORM: process.env["UNI_UTS_PLATFORM"], WEAPP_TAILWINDCSS_TARGET: process.env["WEAPP_TAILWINDCSS_TARGET"], WEAPP_TW_TARGET: process.env["WEAPP_TW_TARGET"] }; } function resolveDependencyNames(pkg) { return /* @__PURE__ */ new Set([ ...Object.keys(pkg.dependencies ?? {}), ...Object.keys(pkg.devDependencies ?? {}), ...Object.keys(pkg.peerDependencies ?? {}), ...Object.keys(pkg.optionalDependencies ?? {}) ]); } function hasDependencyPrefix(dependencyNames, prefix) { return [...dependencyNames].some((name) => name.startsWith(prefix)); } function hasScriptMatch(pkg, pattern) { return Object.values(pkg.scripts ?? {}).some((script) => pattern.test(script)); } function normalizePlatform(value) { return value?.trim().toLowerCase() || void 0; } function resolvePlatformInfo(value) { const normalized = normalizePlatform(value); const isAppAndroid = normalized === "app-android"; const isAppIos = normalized === "app-ios"; const isAppHarmony = normalized === "app-harmony"; return { raw: value, normalized, isApp: normalized?.startsWith("app-") === true || normalized === "app" || normalized === "app-plus", isAppAndroid, isAppHarmony, isAppIos, isMp: normalized?.startsWith("mp-") === true || (normalized ? MINI_PROGRAM_PLATFORMS.has(normalized) : false), isWeb: normalized?.startsWith("web") === true || normalized === "h5" }; } function hasKnownHBuilderXPluginPrefix(normalizedCwd) { for (const dir of KNOWN_MAC_HBUILDERX_PLUGIN_DIRS) { const normalizedDir = path.normalize(dir); if (normalizedCwd === normalizedDir || normalizedCwd.startsWith(`${normalizedDir}${path.sep}`)) return true; } return false; } function matchesHBuilderXPluginCwd(cwd) { const normalized = path.normalize(cwd); if (hasKnownHBuilderXPluginPrefix(normalized)) return true; return HBUILDERX_PLUGIN_CWD_RE.test(normalized); } function isRunningInHBuilderX(options = {}) { const env = options.env ?? getProcessEnv(); const nodePath = "nodePath" in options ? options.nodePath : env.NODE_PATH; if (!(nodePath == null || nodePath.trim().length === 0)) return false; return matchesHBuilderXPluginCwd(options.cwd ?? process.cwd()); } function resolvePlatform(value) { return resolvePlatformInfo(value); } function resolveTaroPlatform(value = getProcessEnv().TARO_ENV) { return resolvePlatform(value); } function resolveMpxPlatformValue(env = getProcessEnv()) { return env.MPX_CURRENT_TARGET_MODE ?? env.MPX_CLI_MODE; } function resolveMpxPlatform(value = resolveMpxPlatformValue()) { return resolvePlatform(value); } function resolveUniPlatform(value = getProcessEnv().UNI_PLATFORM) { return resolvePlatform(value); } function resolveUniUtsPlatform(value = getProcessEnv().UNI_UTS_PLATFORM) { return resolvePlatform(value); } function resolveUniAppXPlatform(value = getProcessEnv().UNI_UTS_PLATFORM) { return resolvePlatform(value); } function resolveUniPlatformsFromEnv(env = getProcessEnv()) { return { uniPlatform: resolveUniPlatform(env.UNI_PLATFORM), uniAppXPlatform: resolveUniAppXPlatform(env.UNI_UTS_PLATFORM), uniUtsPlatform: resolveUniUtsPlatform(env.UNI_UTS_PLATFORM) }; } function detectAppTypeFromEnv(env = getProcessEnv(), options = {}) { if (env.WEAPP_TW_TARGET === "weapp-vite" || env.WEAPP_TAILWINDCSS_TARGET === "weapp-vite") return "weapp-vite"; if (env.TARO_ENV) return "taro"; if (env.MPX_CLI_MODE || env.MPX_CURRENT_TARGET_MODE) return "mpx"; const { uniPlatform, uniUtsPlatform } = resolveUniPlatformsFromEnv(env); if (uniUtsPlatform.normalized) return "uni-app-x"; if (uniPlatform.normalized) return "uni-app-vite"; if (isRunningInHBuilderX({ cwd: options.cwd, env })) return options.hbuilderxVite === false ? "uni-app" : "uni-app-vite"; } function isWeappVitePackage(pkg) { const dependencyNames = resolveDependencyNames(pkg); return dependencyNames.has("weapp-vite") || hasDependencyPrefix(dependencyNames, "@weapp-vite/") || hasScriptMatch(pkg, WEAPP_VITE_SCRIPT_RE); } function isMpxPackage(pkg) { return hasDependencyPrefix(resolveDependencyNames(pkg), "@mpxjs/") || hasScriptMatch(pkg, MPX_SCRIPT_RE); } function isTaroPackage(pkg) { return hasDependencyPrefix(resolveDependencyNames(pkg), "@tarojs/") || hasScriptMatch(pkg, TARO_SCRIPT_RE); } function isUniAppVitePackage(pkg) { return resolveDependencyNames(pkg).has("@dcloudio/vite-plugin-uni") || hasScriptMatch(pkg, UNI_APP_VITE_SCRIPT_RE); } function isUniAppXPackage(pkg) { const dependencyNames = resolveDependencyNames(pkg); return dependencyNames.has("@dcloudio/uni-uts-v1") || dependencyNames.has("@weapp-tailwindcss/debug-uni-app-x"); } function isUniAppPackage(pkg) { const dependencyNames = resolveDependencyNames(pkg); return dependencyNames.has("@dcloudio/vue-cli-plugin-uni") || dependencyNames.has("@dcloudio/uni-app") || Object.hasOwn(pkg, "uni-app") || hasScriptMatch(pkg, UNI_APP_SCRIPT_RE); } function isUniAppXManifest(manifest) { return Boolean(manifest && Object.hasOwn(manifest, "uni-app-x")); } function detectAppTypeFromPackageJson(pkg) { if (isWeappVitePackage(pkg)) return "weapp-vite"; if (isMpxPackage(pkg)) return "mpx"; if (isTaroPackage(pkg)) return "taro"; if (isUniAppXPackage(pkg)) return "uni-app-x"; if (isUniAppVitePackage(pkg)) return "uni-app-vite"; if (isUniAppPackage(pkg)) return "uni-app"; } function tryReadJson(file) { if (!existsSync(file)) return; try { return JSON.parse(readFileSync(file, "utf8")); } catch {} } function detectAppTypeFromMarkers(root) { for (const [relativePath, appType] of MPX_MARKERS) if (existsSync(path.join(root, relativePath))) return appType; } function detectAppTypeFromRoot(root, searchUp = true) { const resolvedRoot = path.resolve(root); if (!existsSync(resolvedRoot)) return; const markerDetected = detectAppTypeFromMarkers(resolvedRoot); if (markerDetected) return markerDetected; let current = resolvedRoot; while (true) { if (isUniAppXManifest(tryReadJson(path.join(current, MANIFEST_JSON_FILE)))) return "uni-app-x"; const pkg = tryReadJson(path.join(current, PACKAGE_JSON_FILE)); if (pkg) { const detected = detectAppTypeFromPackageJson(pkg); if (detected) return detected; } if (!searchUp) break; const parent = path.dirname(current); if (parent === current) break; current = parent; } } function detectAppType(options = {}) { if (isUniAppXManifest(options.manifest)) return "uni-app-x"; if (options.packageJson) { const detected = detectAppTypeFromPackageJson(options.packageJson); if (detected) return detected; } if (options.root) { const detected = detectAppTypeFromRoot(options.root, options.searchUp ?? true); if (detected) return detected; } const env = options.env ?? (options.detectEnv ? getProcessEnv() : void 0); if (env) return detectAppTypeFromEnv(env, { cwd: options.cwd ?? options.root ?? (options.detectEnv ? process.cwd() : void 0), hbuilderxVite: options.hbuilderxVite }); } const resolveImplicitAppTypeFromViteRoot = (root) => detectAppType({ root }); //#endregion export { detectAppType, detectAppTypeFromEnv, detectAppTypeFromPackageJson, isMpxPackage, isRunningInHBuilderX, isTaroPackage, isUniAppPackage, isUniAppVitePackage, isUniAppXManifest, isUniAppXPackage, isWeappVitePackage, resolveImplicitAppTypeFromViteRoot, resolveMpxPlatform, resolvePlatform, resolveTaroPlatform, resolveUniAppXPlatform, resolveUniPlatform, resolveUniPlatformsFromEnv, resolveUniUtsPlatform };