@weapp-tailwindcss/react-native 0.2.23 → 0.3.1
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/README.md +4 -0
- package/README.zh-CN.md +4 -0
- package/dist/babel.cjs +5 -2
- package/dist/babel.d.ts +1 -1
- package/dist/babel.js +5 -2
- package/dist/compiler.d.ts +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/metro-BZbdIyTH.js +353 -0
- package/dist/metro-Ds2ZUc6_.cjs +429 -0
- package/dist/metro-transformer.cjs +14 -22
- package/dist/metro-transformer.js +12 -20
- package/dist/metro.cjs +10 -247
- package/dist/metro.d.ts +4 -2
- package/dist/metro.js +1 -233
- package/dist/runtime-B3hAisFx.d.ts +34 -0
- package/dist/runtime.cjs +1 -1
- package/dist/runtime.d.ts +3 -17
- package/dist/runtime.js +1 -1
- package/dist/tailwind.cjs +4 -2
- package/dist/tailwind.d.ts +2 -2
- package/dist/tailwind.js +4 -2
- package/dist/types-CKAFTXX9.d.ts +17 -0
- package/package.json +6 -5
- package/dist/types-BKoPIarb.d.ts +0 -31
package/dist/metro.cjs
CHANGED
|
@@ -1,248 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
let node_process = require("node:process");
|
|
13
|
-
node_process = require_rolldown_runtime.__toESM(node_process, 1);
|
|
14
|
-
let node_url = require("node:url");
|
|
15
|
-
//#region src/metro.ts
|
|
16
|
-
const VIRTUAL_MANIFEST_MODULE = "@weapp-tailwindcss/react-native/virtual";
|
|
17
|
-
const registry = /* @__PURE__ */ new Map();
|
|
18
|
-
const appRootSingletonModules = /* @__PURE__ */ new Set(["react"]);
|
|
19
|
-
let nextId = 0;
|
|
20
|
-
function packageNameFromModuleId(moduleName) {
|
|
21
|
-
const segments = moduleName.split("/");
|
|
22
|
-
return moduleName.startsWith("@") ? segments.slice(0, 2).join("/") : segments[0];
|
|
23
|
-
}
|
|
24
|
-
function shouldResolveFromAppRoot(moduleName, platform) {
|
|
25
|
-
const packageName = packageNameFromModuleId(moduleName);
|
|
26
|
-
if (appRootSingletonModules.has(packageName)) return true;
|
|
27
|
-
return platform !== "web" && packageName === "react-native";
|
|
28
|
-
}
|
|
29
|
-
function resolveAppRootSingleton(projectRoot, moduleName, platform) {
|
|
30
|
-
if (platform !== "web" || ![
|
|
31
|
-
"react",
|
|
32
|
-
"react-dom",
|
|
33
|
-
"react-native-web"
|
|
34
|
-
].includes(packageNameFromModuleId(moduleName))) return;
|
|
35
|
-
try {
|
|
36
|
-
return {
|
|
37
|
-
type: "sourceFile",
|
|
38
|
-
filePath: (0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json")).resolve(moduleName)
|
|
39
|
-
};
|
|
40
|
-
} catch {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
/** 为 Metro worker 提供不依赖进程内 registry 的 manifest 入口。 */
|
|
45
|
-
function getManifestPathsForProjectRoot(projectRoot) {
|
|
46
|
-
const projectKey = (0, node_crypto.createHash)("sha256").update(node_path.default.resolve(projectRoot)).digest("hex").slice(0, 24);
|
|
47
|
-
const base = node_path.default.join(node_os.default.tmpdir(), "weapp-tailwindcss-native", `project-${projectKey}`);
|
|
48
|
-
return {
|
|
49
|
-
manifestPath: `${base}.manifest.json`,
|
|
50
|
-
manifestReadyPath: `${base}.manifest.ready`
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
function resolveInput(input, projectRoot) {
|
|
54
|
-
return input ? node_path.default.resolve(projectRoot, input) : void 0;
|
|
55
|
-
}
|
|
56
|
-
function emptyManifest() {
|
|
57
|
-
return {
|
|
58
|
-
version: 1,
|
|
59
|
-
classSet: [],
|
|
60
|
-
rules: {},
|
|
61
|
-
variables: {},
|
|
62
|
-
warnings: []
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
function virtualModuleCode(manifest) {
|
|
66
|
-
return `import { setEnvironment, setManifest, setStyleSheetFactory } from ${JSON.stringify("@weapp-tailwindcss/react-native/runtime")};\nimport { Appearance, Platform, StyleSheet } from 'react-native';\nsetStyleSheetFactory(StyleSheet.create);\nconst syncEnvironment = () => setEnvironment({ platform: Platform.OS, colorScheme: Appearance.getColorScheme() ?? 'light' });\nsetManifest(${JSON.stringify(manifest)});\nsyncEnvironment();\nAppearance.addChangeListener?.(({ colorScheme }) => setEnvironment({ platform: Platform.OS, colorScheme: colorScheme ?? 'light' }));\nexport default undefined;`;
|
|
67
|
-
}
|
|
68
|
-
function writeVirtualModule(entry) {
|
|
69
|
-
node_fs.default.mkdirSync(node_path.default.dirname(entry.virtualPath), { recursive: true });
|
|
70
|
-
node_fs.default.mkdirSync(node_path.default.dirname(entry.manifestPath), { recursive: true });
|
|
71
|
-
node_fs.default.writeFileSync(entry.virtualPath, virtualModuleCode(entry.manifest), "utf8");
|
|
72
|
-
node_fs.default.writeFileSync(entry.manifestPath, JSON.stringify(entry.manifest), "utf8");
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Metro worker 会把文件路径先相对化再恢复为绝对路径。虚拟模块必须位于
|
|
76
|
-
* 项目根目录内,否则 Windows 跨盘符路径会被错误拼接成 `D:\\C:\\...`。
|
|
77
|
-
*/
|
|
78
|
-
function getVirtualModulePath(projectRoot, id) {
|
|
79
|
-
return node_path.default.join(projectRoot, "node_modules", ".cache", "weapp-tailwindcss-native", `${id}.js`);
|
|
80
|
-
}
|
|
81
|
-
function markManifestPending(entry) {
|
|
82
|
-
node_fs.default.rmSync(entry.manifestReadyPath, { force: true });
|
|
83
|
-
}
|
|
84
|
-
function markManifestReady(entry) {
|
|
85
|
-
node_fs.default.writeFileSync(entry.manifestReadyPath, `${entry.version}\n`, "utf8");
|
|
86
|
-
}
|
|
87
|
-
async function compileOptions(options, projectRoot) {
|
|
88
|
-
if (options.manifest) return options.manifest;
|
|
89
|
-
if (options.input) {
|
|
90
|
-
const { generateNativeStylesheet } = await (require("url").pathToFileURL(__filename).href.endsWith(".ts") ? Promise.resolve().then(() => require("./tailwind.cjs")) : Promise.resolve().then(() => require("./tailwind.cjs")));
|
|
91
|
-
return generateNativeStylesheet({
|
|
92
|
-
projectRoot,
|
|
93
|
-
cssEntries: [resolveInput(options.input, projectRoot)],
|
|
94
|
-
candidates: options.classSet,
|
|
95
|
-
sourceGlobs: options.sourceGlobs
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
return require_compiler.compileNativeStylesheet(options.css ?? "", { classSet: options.classSet });
|
|
99
|
-
}
|
|
100
|
-
function register(options) {
|
|
101
|
-
const id = `weapp-tailwindcss-native-${nextId++}`;
|
|
102
|
-
const projectRoot = node_path.default.resolve(options.projectRoot ?? node_process.default.cwd());
|
|
103
|
-
const manifestPaths = getManifestPathsForProjectRoot(projectRoot);
|
|
104
|
-
const entry = {
|
|
105
|
-
version: 0,
|
|
106
|
-
manifest: options.manifest ?? (options.css ? require_compiler.compileNativeStylesheet(options.css, { classSet: options.classSet }) : emptyManifest()),
|
|
107
|
-
projectRoot,
|
|
108
|
-
virtualPath: getVirtualModulePath(projectRoot, id),
|
|
109
|
-
manifestPath: manifestPaths.manifestPath,
|
|
110
|
-
manifestReadyPath: manifestPaths.manifestReadyPath,
|
|
111
|
-
ready: Promise.resolve(),
|
|
112
|
-
refresh: async () => {}
|
|
113
|
-
};
|
|
114
|
-
registry.set(id, entry);
|
|
115
|
-
markManifestPending(entry);
|
|
116
|
-
writeVirtualModule(entry);
|
|
117
|
-
let generation = 0;
|
|
118
|
-
entry.refresh = async () => {
|
|
119
|
-
const currentGeneration = ++generation;
|
|
120
|
-
markManifestPending(entry);
|
|
121
|
-
const manifest = await compileOptions(options, projectRoot);
|
|
122
|
-
if (currentGeneration === generation) {
|
|
123
|
-
entry.manifest = manifest;
|
|
124
|
-
entry.version++;
|
|
125
|
-
writeVirtualModule(entry);
|
|
126
|
-
markManifestReady(entry);
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
entry.ready = entry.refresh().catch((error) => {
|
|
130
|
-
const message = `生成 React Native manifest 失败:${error instanceof Error ? error.message : String(error)}`;
|
|
131
|
-
entry.manifest.warnings.push({ message });
|
|
132
|
-
if (node_process.default.env.WEAPP_TW_RN_DEBUG === "1") console.error(`[react-native-debug] ${message}`);
|
|
133
|
-
writeVirtualModule(entry);
|
|
134
|
-
markManifestReady(entry);
|
|
135
|
-
});
|
|
136
|
-
const sourceRoots = (options.sourceGlobs ?? []).map((pattern) => pattern.split(/[*{[]/, 1)[0]?.replace(/[/\\]$/, "")).filter(Boolean);
|
|
137
|
-
[
|
|
138
|
-
...options.watchFiles ?? [],
|
|
139
|
-
...options.input ? [options.input] : [],
|
|
140
|
-
...sourceRoots
|
|
141
|
-
].map((file) => {
|
|
142
|
-
const target = node_path.default.resolve(projectRoot, file);
|
|
143
|
-
try {
|
|
144
|
-
const recursive = node_fs.default.statSync(target).isDirectory();
|
|
145
|
-
return node_fs.default.watch(target, {
|
|
146
|
-
persistent: false,
|
|
147
|
-
recursive
|
|
148
|
-
}, () => {
|
|
149
|
-
entry.refresh();
|
|
150
|
-
});
|
|
151
|
-
} catch {
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
return {
|
|
156
|
-
id,
|
|
157
|
-
entry
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
function getRegisteredVirtualModule(filename) {
|
|
161
|
-
for (const entry of registry.values()) if (entry.virtualPath === filename) return entry;
|
|
162
|
-
}
|
|
163
|
-
async function getRegisteredManifest(id) {
|
|
164
|
-
const registered = registry.get(id);
|
|
165
|
-
if (!registered) return;
|
|
166
|
-
await registered.ready;
|
|
167
|
-
return registered.manifest;
|
|
168
|
-
}
|
|
169
|
-
/** 等待并读取由 Metro 配置注册的 manifest 文件,兼容未透传自定义 Metro id 的 transformer。 */
|
|
170
|
-
async function getRegisteredManifestByPath(filename) {
|
|
171
|
-
for (const entry of registry.values()) if (entry.manifestPath === filename) {
|
|
172
|
-
await entry.ready;
|
|
173
|
-
return entry.manifest;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
/** 按 Metro 传入的项目根目录读取当前注册项,兼容 transformer 未透传自定义字段的实现。 */
|
|
177
|
-
async function getRegisteredManifestByProjectRoot(projectRoot) {
|
|
178
|
-
const resolvedRoot = node_path.default.resolve(projectRoot);
|
|
179
|
-
const entry = [...registry.values()].filter((entry) => entry.projectRoot === resolvedRoot).at(-1);
|
|
180
|
-
if (!entry) return;
|
|
181
|
-
await entry.ready;
|
|
182
|
-
return entry.manifest;
|
|
183
|
-
}
|
|
184
|
-
function getVirtualModuleCode(filename) {
|
|
185
|
-
const registered = getRegisteredVirtualModule(filename);
|
|
186
|
-
if (!registered) return;
|
|
187
|
-
return virtualModuleCode(registered.manifest);
|
|
188
|
-
}
|
|
189
|
-
async function getVirtualModuleCodeAsync(filename) {
|
|
190
|
-
const registered = getRegisteredVirtualModule(filename);
|
|
191
|
-
if (!registered) return;
|
|
192
|
-
await registered.ready;
|
|
193
|
-
return getVirtualModuleCode(filename);
|
|
194
|
-
}
|
|
195
|
-
function withWeappTailwindcss(config, options = {}) {
|
|
196
|
-
if (typeof config === "function" || config && typeof config.then === "function") return Promise.resolve(typeof config === "function" ? config() : config).then((resolved) => withWeappTailwindcss(resolved, options));
|
|
197
|
-
const resolvedConfig = config;
|
|
198
|
-
const { id, entry } = register(options);
|
|
199
|
-
const originalResolver = resolvedConfig.resolver?.resolveRequest;
|
|
200
|
-
const sourceExts = resolvedConfig.resolver?.sourceExts ?? [];
|
|
201
|
-
const virtualResolver = (context, moduleName, platform) => {
|
|
202
|
-
if (moduleName === "@weapp-tailwindcss/react-native/virtual") return {
|
|
203
|
-
type: "sourceFile",
|
|
204
|
-
filePath: entry.virtualPath
|
|
205
|
-
};
|
|
206
|
-
const appRootSingleton = resolveAppRootSingleton(entry.projectRoot, moduleName, platform);
|
|
207
|
-
if (appRootSingleton) return appRootSingleton;
|
|
208
|
-
const resolverContext = context;
|
|
209
|
-
const anchoredContext = resolverContext.originModulePath === entry.virtualPath || shouldResolveFromAppRoot(moduleName, platform) ? {
|
|
210
|
-
...resolverContext,
|
|
211
|
-
originModulePath: node_path.default.join(entry.projectRoot, "package.json")
|
|
212
|
-
} : resolverContext;
|
|
213
|
-
return originalResolver?.(anchoredContext, moduleName, platform) ?? resolverContext.resolveRequest?.(anchoredContext, moduleName, platform) ?? resolveAppRootSingleton(entry.projectRoot, moduleName, platform);
|
|
214
|
-
};
|
|
215
|
-
return {
|
|
216
|
-
...resolvedConfig,
|
|
217
|
-
watchFolders: [.../* @__PURE__ */ new Set([...resolvedConfig.watchFolders ?? [], node_path.default.dirname(entry.virtualPath)])],
|
|
218
|
-
transformerPath: (0, node_url.fileURLToPath)(new URL(require("url").pathToFileURL(__filename).href.includes(".ts") ? "./metro-transformer.ts" : "./metro-transformer.js", require("url").pathToFileURL(__filename).href)),
|
|
219
|
-
transformer: {
|
|
220
|
-
...resolvedConfig.transformer,
|
|
221
|
-
weappTailwindcssMetroId: id,
|
|
222
|
-
weappTailwindcssOriginalTransformerPath: resolvedConfig.transformerPath,
|
|
223
|
-
weappTailwindcssManifestPath: entry.manifestPath,
|
|
224
|
-
weappTailwindcssManifestReadyPath: entry.manifestReadyPath,
|
|
225
|
-
weappTailwindcssVirtualModulePath: entry.virtualPath
|
|
226
|
-
},
|
|
227
|
-
resolver: {
|
|
228
|
-
...resolvedConfig.resolver,
|
|
229
|
-
extraNodeModules: {
|
|
230
|
-
...resolvedConfig.resolver?.extraNodeModules,
|
|
231
|
-
"react": node_path.default.resolve(entry.projectRoot, "node_modules/react"),
|
|
232
|
-
"react-dom": node_path.default.resolve(entry.projectRoot, "node_modules/react-dom")
|
|
233
|
-
},
|
|
234
|
-
sourceExts: sourceExts.includes("css") ? sourceExts : [...sourceExts, "css"],
|
|
235
|
-
resolveRequest: virtualResolver
|
|
236
|
-
}
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
//#endregion
|
|
240
|
-
exports.VIRTUAL_MANIFEST_MODULE = VIRTUAL_MANIFEST_MODULE;
|
|
241
|
-
exports.getManifestPathsForProjectRoot = getManifestPathsForProjectRoot;
|
|
242
|
-
exports.getRegisteredManifest = getRegisteredManifest;
|
|
243
|
-
exports.getRegisteredManifestByPath = getRegisteredManifestByPath;
|
|
244
|
-
exports.getRegisteredManifestByProjectRoot = getRegisteredManifestByProjectRoot;
|
|
245
|
-
exports.getRegisteredVirtualModule = getRegisteredVirtualModule;
|
|
246
|
-
exports.getVirtualModuleCode = getVirtualModuleCode;
|
|
247
|
-
exports.getVirtualModuleCodeAsync = getVirtualModuleCodeAsync;
|
|
248
|
-
exports.withWeappTailwindcss = withWeappTailwindcss;
|
|
2
|
+
const require_metro = require("./metro-Ds2ZUc6_.cjs");
|
|
3
|
+
exports.VIRTUAL_MANIFEST_MODULE = require_metro.VIRTUAL_MANIFEST_MODULE;
|
|
4
|
+
exports.getManifestPathsForProjectRoot = require_metro.getManifestPathsForProjectRoot;
|
|
5
|
+
exports.getRegisteredManifest = require_metro.getRegisteredManifest;
|
|
6
|
+
exports.getRegisteredManifestByPath = require_metro.getRegisteredManifestByPath;
|
|
7
|
+
exports.getRegisteredManifestByProjectRoot = require_metro.getRegisteredManifestByProjectRoot;
|
|
8
|
+
exports.getRegisteredVirtualModule = require_metro.getRegisteredVirtualModule;
|
|
9
|
+
exports.getVirtualModuleCode = require_metro.getVirtualModuleCode;
|
|
10
|
+
exports.getVirtualModuleCodeAsync = require_metro.getVirtualModuleCodeAsync;
|
|
11
|
+
exports.withWeappTailwindcss = require_metro.withWeappTailwindcss;
|
package/dist/metro.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as NativeStyleManifest } from "./types-CKAFTXX9.js";
|
|
2
2
|
//#region src/metro.d.ts
|
|
3
3
|
export declare const VIRTUAL_MANIFEST_MODULE = "@weapp-tailwindcss/react-native/virtual";
|
|
4
4
|
export interface MetroConfigLike {
|
|
@@ -47,5 +47,7 @@ export declare function getRegisteredManifestByPath(filename: string): Promise<N
|
|
|
47
47
|
export declare function getRegisteredManifestByProjectRoot(projectRoot: string): Promise<NativeStyleManifest | undefined>;
|
|
48
48
|
export declare function getVirtualModuleCode(filename: string): string | undefined;
|
|
49
49
|
export declare function getVirtualModuleCodeAsync(filename: string): Promise<string | undefined>;
|
|
50
|
-
export declare function withWeappTailwindcss<T extends MetroConfigLike>(config: T
|
|
50
|
+
export declare function withWeappTailwindcss<T extends MetroConfigLike>(config: T, options?: WeappReactNativeMetroOptions): T & MetroConfigLike;
|
|
51
|
+
export declare function withWeappTailwindcss<T extends MetroConfigLike>(config: Promise<T> | (() => T | Promise<T>), options?: WeappReactNativeMetroOptions): Promise<T & MetroConfigLike>;
|
|
52
|
+
export declare function withWeappTailwindcss<T extends MetroConfigLike>(config: T | Promise<T> | (() => T | Promise<T>), options?: WeappReactNativeMetroOptions): (T & MetroConfigLike) | Promise<T & MetroConfigLike>;
|
|
51
53
|
//#endregion
|
package/dist/metro.js
CHANGED
|
@@ -1,234 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createRequire } from "node:module";
|
|
3
|
-
import { createHash } from "node:crypto";
|
|
4
|
-
import fs from "node:fs";
|
|
5
|
-
import os from "node:os";
|
|
6
|
-
import path from "node:path";
|
|
7
|
-
import process from "node:process";
|
|
8
|
-
import { fileURLToPath } from "node:url";
|
|
9
|
-
//#region src/metro.ts
|
|
10
|
-
const VIRTUAL_MANIFEST_MODULE = "@weapp-tailwindcss/react-native/virtual";
|
|
11
|
-
const registry = /* @__PURE__ */ new Map();
|
|
12
|
-
const appRootSingletonModules = /* @__PURE__ */ new Set(["react"]);
|
|
13
|
-
let nextId = 0;
|
|
14
|
-
function packageNameFromModuleId(moduleName) {
|
|
15
|
-
const segments = moduleName.split("/");
|
|
16
|
-
return moduleName.startsWith("@") ? segments.slice(0, 2).join("/") : segments[0];
|
|
17
|
-
}
|
|
18
|
-
function shouldResolveFromAppRoot(moduleName, platform) {
|
|
19
|
-
const packageName = packageNameFromModuleId(moduleName);
|
|
20
|
-
if (appRootSingletonModules.has(packageName)) return true;
|
|
21
|
-
return platform !== "web" && packageName === "react-native";
|
|
22
|
-
}
|
|
23
|
-
function resolveAppRootSingleton(projectRoot, moduleName, platform) {
|
|
24
|
-
if (platform !== "web" || ![
|
|
25
|
-
"react",
|
|
26
|
-
"react-dom",
|
|
27
|
-
"react-native-web"
|
|
28
|
-
].includes(packageNameFromModuleId(moduleName))) return;
|
|
29
|
-
try {
|
|
30
|
-
return {
|
|
31
|
-
type: "sourceFile",
|
|
32
|
-
filePath: createRequire(path.join(projectRoot, "package.json")).resolve(moduleName)
|
|
33
|
-
};
|
|
34
|
-
} catch {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
/** 为 Metro worker 提供不依赖进程内 registry 的 manifest 入口。 */
|
|
39
|
-
function getManifestPathsForProjectRoot(projectRoot) {
|
|
40
|
-
const projectKey = createHash("sha256").update(path.resolve(projectRoot)).digest("hex").slice(0, 24);
|
|
41
|
-
const base = path.join(os.tmpdir(), "weapp-tailwindcss-native", `project-${projectKey}`);
|
|
42
|
-
return {
|
|
43
|
-
manifestPath: `${base}.manifest.json`,
|
|
44
|
-
manifestReadyPath: `${base}.manifest.ready`
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
function resolveInput(input, projectRoot) {
|
|
48
|
-
return input ? path.resolve(projectRoot, input) : void 0;
|
|
49
|
-
}
|
|
50
|
-
function emptyManifest() {
|
|
51
|
-
return {
|
|
52
|
-
version: 1,
|
|
53
|
-
classSet: [],
|
|
54
|
-
rules: {},
|
|
55
|
-
variables: {},
|
|
56
|
-
warnings: []
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
function virtualModuleCode(manifest) {
|
|
60
|
-
return `import { setEnvironment, setManifest, setStyleSheetFactory } from ${JSON.stringify("@weapp-tailwindcss/react-native/runtime")};\nimport { Appearance, Platform, StyleSheet } from 'react-native';\nsetStyleSheetFactory(StyleSheet.create);\nconst syncEnvironment = () => setEnvironment({ platform: Platform.OS, colorScheme: Appearance.getColorScheme() ?? 'light' });\nsetManifest(${JSON.stringify(manifest)});\nsyncEnvironment();\nAppearance.addChangeListener?.(({ colorScheme }) => setEnvironment({ platform: Platform.OS, colorScheme: colorScheme ?? 'light' }));\nexport default undefined;`;
|
|
61
|
-
}
|
|
62
|
-
function writeVirtualModule(entry) {
|
|
63
|
-
fs.mkdirSync(path.dirname(entry.virtualPath), { recursive: true });
|
|
64
|
-
fs.mkdirSync(path.dirname(entry.manifestPath), { recursive: true });
|
|
65
|
-
fs.writeFileSync(entry.virtualPath, virtualModuleCode(entry.manifest), "utf8");
|
|
66
|
-
fs.writeFileSync(entry.manifestPath, JSON.stringify(entry.manifest), "utf8");
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Metro worker 会把文件路径先相对化再恢复为绝对路径。虚拟模块必须位于
|
|
70
|
-
* 项目根目录内,否则 Windows 跨盘符路径会被错误拼接成 `D:\\C:\\...`。
|
|
71
|
-
*/
|
|
72
|
-
function getVirtualModulePath(projectRoot, id) {
|
|
73
|
-
return path.join(projectRoot, "node_modules", ".cache", "weapp-tailwindcss-native", `${id}.js`);
|
|
74
|
-
}
|
|
75
|
-
function markManifestPending(entry) {
|
|
76
|
-
fs.rmSync(entry.manifestReadyPath, { force: true });
|
|
77
|
-
}
|
|
78
|
-
function markManifestReady(entry) {
|
|
79
|
-
fs.writeFileSync(entry.manifestReadyPath, `${entry.version}\n`, "utf8");
|
|
80
|
-
}
|
|
81
|
-
async function compileOptions(options, projectRoot) {
|
|
82
|
-
if (options.manifest) return options.manifest;
|
|
83
|
-
if (options.input) {
|
|
84
|
-
const { generateNativeStylesheet } = await (import.meta.url.endsWith(".ts") ? import("./tailwind.js") : import("./tailwind.js"));
|
|
85
|
-
return generateNativeStylesheet({
|
|
86
|
-
projectRoot,
|
|
87
|
-
cssEntries: [resolveInput(options.input, projectRoot)],
|
|
88
|
-
candidates: options.classSet,
|
|
89
|
-
sourceGlobs: options.sourceGlobs
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
return compileNativeStylesheet(options.css ?? "", { classSet: options.classSet });
|
|
93
|
-
}
|
|
94
|
-
function register(options) {
|
|
95
|
-
const id = `weapp-tailwindcss-native-${nextId++}`;
|
|
96
|
-
const projectRoot = path.resolve(options.projectRoot ?? process.cwd());
|
|
97
|
-
const manifestPaths = getManifestPathsForProjectRoot(projectRoot);
|
|
98
|
-
const entry = {
|
|
99
|
-
version: 0,
|
|
100
|
-
manifest: options.manifest ?? (options.css ? compileNativeStylesheet(options.css, { classSet: options.classSet }) : emptyManifest()),
|
|
101
|
-
projectRoot,
|
|
102
|
-
virtualPath: getVirtualModulePath(projectRoot, id),
|
|
103
|
-
manifestPath: manifestPaths.manifestPath,
|
|
104
|
-
manifestReadyPath: manifestPaths.manifestReadyPath,
|
|
105
|
-
ready: Promise.resolve(),
|
|
106
|
-
refresh: async () => {}
|
|
107
|
-
};
|
|
108
|
-
registry.set(id, entry);
|
|
109
|
-
markManifestPending(entry);
|
|
110
|
-
writeVirtualModule(entry);
|
|
111
|
-
let generation = 0;
|
|
112
|
-
entry.refresh = async () => {
|
|
113
|
-
const currentGeneration = ++generation;
|
|
114
|
-
markManifestPending(entry);
|
|
115
|
-
const manifest = await compileOptions(options, projectRoot);
|
|
116
|
-
if (currentGeneration === generation) {
|
|
117
|
-
entry.manifest = manifest;
|
|
118
|
-
entry.version++;
|
|
119
|
-
writeVirtualModule(entry);
|
|
120
|
-
markManifestReady(entry);
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
entry.ready = entry.refresh().catch((error) => {
|
|
124
|
-
const message = `生成 React Native manifest 失败:${error instanceof Error ? error.message : String(error)}`;
|
|
125
|
-
entry.manifest.warnings.push({ message });
|
|
126
|
-
if (process.env.WEAPP_TW_RN_DEBUG === "1") console.error(`[react-native-debug] ${message}`);
|
|
127
|
-
writeVirtualModule(entry);
|
|
128
|
-
markManifestReady(entry);
|
|
129
|
-
});
|
|
130
|
-
const sourceRoots = (options.sourceGlobs ?? []).map((pattern) => pattern.split(/[*{[]/, 1)[0]?.replace(/[/\\]$/, "")).filter(Boolean);
|
|
131
|
-
[
|
|
132
|
-
...options.watchFiles ?? [],
|
|
133
|
-
...options.input ? [options.input] : [],
|
|
134
|
-
...sourceRoots
|
|
135
|
-
].map((file) => {
|
|
136
|
-
const target = path.resolve(projectRoot, file);
|
|
137
|
-
try {
|
|
138
|
-
const recursive = fs.statSync(target).isDirectory();
|
|
139
|
-
return fs.watch(target, {
|
|
140
|
-
persistent: false,
|
|
141
|
-
recursive
|
|
142
|
-
}, () => {
|
|
143
|
-
entry.refresh();
|
|
144
|
-
});
|
|
145
|
-
} catch {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
return {
|
|
150
|
-
id,
|
|
151
|
-
entry
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
function getRegisteredVirtualModule(filename) {
|
|
155
|
-
for (const entry of registry.values()) if (entry.virtualPath === filename) return entry;
|
|
156
|
-
}
|
|
157
|
-
async function getRegisteredManifest(id) {
|
|
158
|
-
const registered = registry.get(id);
|
|
159
|
-
if (!registered) return;
|
|
160
|
-
await registered.ready;
|
|
161
|
-
return registered.manifest;
|
|
162
|
-
}
|
|
163
|
-
/** 等待并读取由 Metro 配置注册的 manifest 文件,兼容未透传自定义 Metro id 的 transformer。 */
|
|
164
|
-
async function getRegisteredManifestByPath(filename) {
|
|
165
|
-
for (const entry of registry.values()) if (entry.manifestPath === filename) {
|
|
166
|
-
await entry.ready;
|
|
167
|
-
return entry.manifest;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
/** 按 Metro 传入的项目根目录读取当前注册项,兼容 transformer 未透传自定义字段的实现。 */
|
|
171
|
-
async function getRegisteredManifestByProjectRoot(projectRoot) {
|
|
172
|
-
const resolvedRoot = path.resolve(projectRoot);
|
|
173
|
-
const entry = [...registry.values()].filter((entry) => entry.projectRoot === resolvedRoot).at(-1);
|
|
174
|
-
if (!entry) return;
|
|
175
|
-
await entry.ready;
|
|
176
|
-
return entry.manifest;
|
|
177
|
-
}
|
|
178
|
-
function getVirtualModuleCode(filename) {
|
|
179
|
-
const registered = getRegisteredVirtualModule(filename);
|
|
180
|
-
if (!registered) return;
|
|
181
|
-
return virtualModuleCode(registered.manifest);
|
|
182
|
-
}
|
|
183
|
-
async function getVirtualModuleCodeAsync(filename) {
|
|
184
|
-
const registered = getRegisteredVirtualModule(filename);
|
|
185
|
-
if (!registered) return;
|
|
186
|
-
await registered.ready;
|
|
187
|
-
return getVirtualModuleCode(filename);
|
|
188
|
-
}
|
|
189
|
-
function withWeappTailwindcss(config, options = {}) {
|
|
190
|
-
if (typeof config === "function" || config && typeof config.then === "function") return Promise.resolve(typeof config === "function" ? config() : config).then((resolved) => withWeappTailwindcss(resolved, options));
|
|
191
|
-
const resolvedConfig = config;
|
|
192
|
-
const { id, entry } = register(options);
|
|
193
|
-
const originalResolver = resolvedConfig.resolver?.resolveRequest;
|
|
194
|
-
const sourceExts = resolvedConfig.resolver?.sourceExts ?? [];
|
|
195
|
-
const virtualResolver = (context, moduleName, platform) => {
|
|
196
|
-
if (moduleName === "@weapp-tailwindcss/react-native/virtual") return {
|
|
197
|
-
type: "sourceFile",
|
|
198
|
-
filePath: entry.virtualPath
|
|
199
|
-
};
|
|
200
|
-
const appRootSingleton = resolveAppRootSingleton(entry.projectRoot, moduleName, platform);
|
|
201
|
-
if (appRootSingleton) return appRootSingleton;
|
|
202
|
-
const resolverContext = context;
|
|
203
|
-
const anchoredContext = resolverContext.originModulePath === entry.virtualPath || shouldResolveFromAppRoot(moduleName, platform) ? {
|
|
204
|
-
...resolverContext,
|
|
205
|
-
originModulePath: path.join(entry.projectRoot, "package.json")
|
|
206
|
-
} : resolverContext;
|
|
207
|
-
return originalResolver?.(anchoredContext, moduleName, platform) ?? resolverContext.resolveRequest?.(anchoredContext, moduleName, platform) ?? resolveAppRootSingleton(entry.projectRoot, moduleName, platform);
|
|
208
|
-
};
|
|
209
|
-
return {
|
|
210
|
-
...resolvedConfig,
|
|
211
|
-
watchFolders: [.../* @__PURE__ */ new Set([...resolvedConfig.watchFolders ?? [], path.dirname(entry.virtualPath)])],
|
|
212
|
-
transformerPath: fileURLToPath(new URL(import.meta.url.includes(".ts") ? "./metro-transformer.ts" : "./metro-transformer.js", import.meta.url)),
|
|
213
|
-
transformer: {
|
|
214
|
-
...resolvedConfig.transformer,
|
|
215
|
-
weappTailwindcssMetroId: id,
|
|
216
|
-
weappTailwindcssOriginalTransformerPath: resolvedConfig.transformerPath,
|
|
217
|
-
weappTailwindcssManifestPath: entry.manifestPath,
|
|
218
|
-
weappTailwindcssManifestReadyPath: entry.manifestReadyPath,
|
|
219
|
-
weappTailwindcssVirtualModulePath: entry.virtualPath
|
|
220
|
-
},
|
|
221
|
-
resolver: {
|
|
222
|
-
...resolvedConfig.resolver,
|
|
223
|
-
extraNodeModules: {
|
|
224
|
-
...resolvedConfig.resolver?.extraNodeModules,
|
|
225
|
-
"react": path.resolve(entry.projectRoot, "node_modules/react"),
|
|
226
|
-
"react-dom": path.resolve(entry.projectRoot, "node_modules/react-dom")
|
|
227
|
-
},
|
|
228
|
-
sourceExts: sourceExts.includes("css") ? sourceExts : [...sourceExts, "css"],
|
|
229
|
-
resolveRequest: virtualResolver
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
//#endregion
|
|
1
|
+
import { a as getRegisteredManifestByProjectRoot, c as getVirtualModuleCodeAsync, i as getRegisteredManifestByPath, l as withWeappTailwindcss, n as getManifestPathsForProjectRoot, o as getRegisteredVirtualModule, r as getRegisteredManifest, s as getVirtualModuleCode, t as VIRTUAL_MANIFEST_MODULE } from "./metro-BZbdIyTH.js";
|
|
234
2
|
export { VIRTUAL_MANIFEST_MODULE, getManifestPathsForProjectRoot, getRegisteredManifest, getRegisteredManifestByPath, getRegisteredManifestByProjectRoot, getRegisteredVirtualModule, getVirtualModuleCode, getVirtualModuleCodeAsync, withWeappTailwindcss };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { i as NativeStyleManifest, r as NativePlatform } from "./types-CKAFTXX9.js";
|
|
2
|
+
import { StyleProp } from "react-native";
|
|
3
|
+
//#region src/runtime/types.d.ts
|
|
4
|
+
interface NativeStyleEnvironment {
|
|
5
|
+
colorScheme?: 'light' | 'dark' | undefined;
|
|
6
|
+
platform?: NativePlatform | undefined;
|
|
7
|
+
}
|
|
8
|
+
type NativeClassValue = string | false | null | undefined | NativeClassValue[] | Record<string, boolean>;
|
|
9
|
+
type NativeStyleValue = StyleProp<Record<string, unknown>>;
|
|
10
|
+
interface NativeStyleRuntime {
|
|
11
|
+
tw: (value: NativeClassValue, environment?: NativeStyleEnvironment) => NativeStyleValue;
|
|
12
|
+
getStaticStyle: (ids: readonly string[], environment?: NativeStyleEnvironment) => NativeStyleValue;
|
|
13
|
+
composeStyle: <const InlineStyle>(tailwindStyle: NativeStyleValue, inlineStyle: InlineStyle) => [NativeStyleValue, InlineStyle] | [NativeStyleValue, InlineStyle, NativeStyleValue];
|
|
14
|
+
setManifest: (manifest: NativeStyleManifest) => void;
|
|
15
|
+
setEnvironment: (environment: NativeStyleEnvironment) => void;
|
|
16
|
+
getManifest: () => NativeStyleManifest | undefined;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/runtime.d.ts
|
|
20
|
+
interface StyleValue {
|
|
21
|
+
[key: string]: unknown;
|
|
22
|
+
}
|
|
23
|
+
type StyleSheetFactory = (value: Record<string, StyleValue>) => Record<string, NativeStyleValue>;
|
|
24
|
+
declare function createNativeStyleRuntime(initialManifest?: NativeStyleManifest): NativeStyleRuntime;
|
|
25
|
+
declare function setManifest(manifest: NativeStyleManifest): void;
|
|
26
|
+
/** 由 Expo virtual module 注入,避免 runtime 引入 Node-only 的 require shim。 */
|
|
27
|
+
declare function setStyleSheetFactory(factory: StyleSheetFactory): void;
|
|
28
|
+
declare function setEnvironment(environment: NativeStyleEnvironment): void;
|
|
29
|
+
declare function getManifest(): NativeStyleManifest | undefined;
|
|
30
|
+
declare function tw(value: NativeClassValue, environment?: NativeStyleEnvironment): NativeStyleValue;
|
|
31
|
+
declare function getStaticStyle(ids: readonly string[], environment?: NativeStyleEnvironment): NativeStyleValue;
|
|
32
|
+
declare function composeStyle<const InlineStyle>(tailwindStyle: NativeStyleValue, inlineStyle: InlineStyle): [NativeStyleValue, InlineStyle] | [NativeStyleValue, InlineStyle, NativeStyleValue];
|
|
33
|
+
//#endregion
|
|
34
|
+
export { setEnvironment as a, tw as c, NativeStyleRuntime as d, NativeStyleValue as f, getStaticStyle as i, NativeClassValue as l, createNativeStyleRuntime as n, setManifest as o, getManifest as r, setStyleSheetFactory as s, composeStyle as t, NativeStyleEnvironment as u };
|
package/dist/runtime.cjs
CHANGED
|
@@ -47,7 +47,7 @@ function resolveRules(ids, manifest, environment) {
|
|
|
47
47
|
}
|
|
48
48
|
const fallbackIds = manifest?.staticLookup?.[id] ?? [];
|
|
49
49
|
if (fallbackIds.length) for (const fallbackId of fallbackIds) {
|
|
50
|
-
const fallbackRule = manifest
|
|
50
|
+
const fallbackRule = manifest?.styleEntries?.[fallbackId];
|
|
51
51
|
if (!fallbackRule || !matches(fallbackRule, environment)) continue;
|
|
52
52
|
(fallbackRule.important ? important : normal).push({
|
|
53
53
|
rule: fallbackRule,
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,17 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
[key: string]: unknown;
|
|
5
|
-
}
|
|
6
|
-
type StyleSheetFactory = (value: Record<string, StyleValue>) => Record<string, unknown>;
|
|
7
|
-
export declare function createNativeStyleRuntime(initialManifest?: NativeStyleManifest): NativeStyleRuntime;
|
|
8
|
-
export declare function setManifest(manifest: NativeStyleManifest): void;
|
|
9
|
-
/** 由 Expo virtual module 注入,避免 runtime 引入 Node-only 的 require shim。 */
|
|
10
|
-
export declare function setStyleSheetFactory(factory: StyleSheetFactory): void;
|
|
11
|
-
export declare function setEnvironment(environment: NativeStyleEnvironment): void;
|
|
12
|
-
export declare function getManifest(): NativeStyleManifest | undefined;
|
|
13
|
-
export declare function tw(value: NativeClassValue, environment?: NativeStyleEnvironment): Record<string, unknown>;
|
|
14
|
-
export declare function getStaticStyle(ids: readonly string[], environment?: NativeStyleEnvironment): NativeStyleValue;
|
|
15
|
-
export declare function composeStyle(tailwindStyle: NativeStyleValue, inlineStyle: unknown): NativeStyleValue[];
|
|
16
|
-
//#endregion
|
|
17
|
-
export type { NativeClassValue, NativePlatform, NativeStyleEnvironment, NativeStyleManifest, NativeStyleValue };
|
|
1
|
+
import { i as NativeStyleManifest, r as NativePlatform } from "./types-CKAFTXX9.js";
|
|
2
|
+
import { a as setEnvironment, c as tw, f as NativeStyleValue, i as getStaticStyle, l as NativeClassValue, n as createNativeStyleRuntime, o as setManifest, r as getManifest, s as setStyleSheetFactory, t as composeStyle, u as NativeStyleEnvironment } from "./runtime-B3hAisFx.js";
|
|
3
|
+
export { type NativeClassValue, type NativePlatform, type NativeStyleEnvironment, type NativeStyleManifest, type NativeStyleValue, composeStyle, createNativeStyleRuntime, getManifest, getStaticStyle, setEnvironment, setManifest, setStyleSheetFactory, tw };
|
package/dist/runtime.js
CHANGED
|
@@ -46,7 +46,7 @@ function resolveRules(ids, manifest, environment) {
|
|
|
46
46
|
}
|
|
47
47
|
const fallbackIds = manifest?.staticLookup?.[id] ?? [];
|
|
48
48
|
if (fallbackIds.length) for (const fallbackId of fallbackIds) {
|
|
49
|
-
const fallbackRule = manifest
|
|
49
|
+
const fallbackRule = manifest?.styleEntries?.[fallbackId];
|
|
50
50
|
if (!fallbackRule || !matches(fallbackRule, environment)) continue;
|
|
51
51
|
(fallbackRule.important ? important : normal).push({
|
|
52
52
|
rule: fallbackRule,
|
package/dist/tailwind.cjs
CHANGED
|
@@ -10,9 +10,11 @@ let _weapp_tailwindcss_postcss_native = require("@weapp-tailwindcss/postcss/nati
|
|
|
10
10
|
* 使用 weapp-tailwindcss 的 Tailwind v4 generator 生成原始 CSS,再编译为 RN manifest。
|
|
11
11
|
*/
|
|
12
12
|
async function generateNativeStylesheet(options = {}) {
|
|
13
|
+
const { projectRoot = node_process.default.cwd(), cssEntries, ...sourceOptions } = options;
|
|
13
14
|
const source = await (0, weapp_tailwindcss_generator.resolveTailwindV4Source)({
|
|
14
|
-
...
|
|
15
|
-
projectRoot
|
|
15
|
+
...sourceOptions,
|
|
16
|
+
projectRoot,
|
|
17
|
+
...cssEntries ? { cssEntries } : {}
|
|
16
18
|
});
|
|
17
19
|
const generator = (0, weapp_tailwindcss_generator.createWeappTailwindcssGenerator)(source);
|
|
18
20
|
const sourcePatterns = options.sourceGlobs?.map((pattern) => ({
|
package/dist/tailwind.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as NativeStyleManifest } from "./types-CKAFTXX9.js";
|
|
2
2
|
import { TailwindV4SourceOptions } from "weapp-tailwindcss/generator";
|
|
3
3
|
//#region src/tailwind.d.ts
|
|
4
|
-
export interface GenerateNativeStylesheetOptions extends TailwindV4SourceOptions {
|
|
4
|
+
export interface GenerateNativeStylesheetOptions extends Omit<TailwindV4SourceOptions, 'projectRoot' | 'cssEntries'> {
|
|
5
5
|
/** Tailwind source 扫描根目录;用于 workspace 示例和 Metro 项目。 */
|
|
6
6
|
projectRoot?: string | undefined;
|
|
7
7
|
/** Tailwind CSS 入口文件;相对路径会按 projectRoot 解析。 */
|
package/dist/tailwind.js
CHANGED
|
@@ -6,9 +6,11 @@ import { createWeappTailwindcssGenerator, resolveTailwindV4Source } from "weapp-
|
|
|
6
6
|
* 使用 weapp-tailwindcss 的 Tailwind v4 generator 生成原始 CSS,再编译为 RN manifest。
|
|
7
7
|
*/
|
|
8
8
|
async function generateNativeStylesheet(options = {}) {
|
|
9
|
+
const { projectRoot = process.cwd(), cssEntries, ...sourceOptions } = options;
|
|
9
10
|
const source = await resolveTailwindV4Source({
|
|
10
|
-
...
|
|
11
|
-
projectRoot
|
|
11
|
+
...sourceOptions,
|
|
12
|
+
projectRoot,
|
|
13
|
+
...cssEntries ? { cssEntries } : {}
|
|
12
14
|
});
|
|
13
15
|
const generator = createWeappTailwindcssGenerator(source);
|
|
14
16
|
const sourcePatterns = options.sourceGlobs?.map((pattern) => ({
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CompileNativeStylesheetOptions, NativeCompilerWarning, NativeCompilerWarning as NativeCompilerWarning$1, NativePlatform, NativeStyleRule, NativeStyleRule as NativeStyleRule$1 } from "@weapp-tailwindcss/postcss/native";
|
|
2
|
+
//#region src/types.d.ts
|
|
3
|
+
interface NativeStyleManifest {
|
|
4
|
+
version: 1;
|
|
5
|
+
classSet: string[];
|
|
6
|
+
rules: Record<string, NativeStyleRule[]>;
|
|
7
|
+
/** 可直接交给 StyleSheet.create 的稳定规则表。 */
|
|
8
|
+
styleSheet?: Record<string, Record<string, unknown>>;
|
|
9
|
+
/** styleSheet 规则的条件和优先级元数据。 */
|
|
10
|
+
styleEntries?: Record<string, NativeStyleRule>;
|
|
11
|
+
/** 每个 class token 对应的静态 style ID,Babel 编译结果直接使用它。 */
|
|
12
|
+
staticLookup?: Record<string, string[]>;
|
|
13
|
+
variables: Record<string, string>;
|
|
14
|
+
warnings: NativeCompilerWarning[];
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { NativeStyleRule$1 as a, NativeStyleManifest as i, NativeCompilerWarning$1 as n, NativePlatform as r, CompileNativeStylesheetOptions as t };
|