@weapp-tailwindcss/react-native 0.2.23 → 0.3.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.
@@ -0,0 +1,429 @@
1
+ const require_rolldown_runtime = require("./rolldown-runtime-VH7oDXx4.cjs");
2
+ const require_compiler = require("./compiler.cjs");
3
+ let node_crypto = require("node:crypto");
4
+ let node_fs = require("node:fs");
5
+ node_fs = require_rolldown_runtime.__toESM(node_fs, 1);
6
+ let node_module = require("node:module");
7
+ let node_os = require("node:os");
8
+ node_os = require_rolldown_runtime.__toESM(node_os, 1);
9
+ let node_path = require("node:path");
10
+ node_path = require_rolldown_runtime.__toESM(node_path, 1);
11
+ let node_process = require("node:process");
12
+ node_process = require_rolldown_runtime.__toESM(node_process, 1);
13
+ let node_url = require("node:url");
14
+ let node_timers_promises = require("node:timers/promises");
15
+ //#region src/metro/manifest-store.ts
16
+ /** 同目录替换,避免 Metro 或 worker 读到截断后的虚拟模块与 JSON。 */
17
+ function writeManifestFile(filename, content) {
18
+ const temporary = `${filename}.${(0, node_crypto.randomUUID)()}.tmp`;
19
+ try {
20
+ node_fs.default.writeFileSync(temporary, content, "utf8");
21
+ node_fs.default.renameSync(temporary, filename);
22
+ } finally {
23
+ node_fs.default.rmSync(temporary, { force: true });
24
+ }
25
+ }
26
+ /** ready 前后版本一致才接受产物;失败和超时不能降级成旧 manifest。 */
27
+ async function readPublishedSnapshot(filename, readyPath) {
28
+ if (!readyPath) try {
29
+ return { manifest: JSON.parse(await node_fs.default.promises.readFile(filename, "utf8")) };
30
+ } catch (error) {
31
+ if (error.code === "ENOENT") return;
32
+ throw error;
33
+ }
34
+ const deadline = Date.now() + 12e4;
35
+ while (Date.now() < deadline) {
36
+ try {
37
+ const before = await node_fs.default.promises.readFile(readyPath, "utf8");
38
+ const content = await node_fs.default.promises.readFile(filename, "utf8");
39
+ if (before === await node_fs.default.promises.readFile(readyPath, "utf8")) {
40
+ let virtualPath;
41
+ if (before.startsWith("{")) {
42
+ const publication = JSON.parse(before);
43
+ if (publication.error) throw new Error(publication.error);
44
+ if (!publication.revision || !publication.virtualPath) throw new Error("React Native manifest 缺少发布身份");
45
+ virtualPath = publication.virtualPath;
46
+ } else if (!/^\d+\s*$/.test(before)) throw new Error("React Native manifest 发布标识无效");
47
+ return {
48
+ manifest: JSON.parse(content),
49
+ ...virtualPath ? { virtualPath } : {}
50
+ };
51
+ }
52
+ } catch (error) {
53
+ if (error.code !== "ENOENT") throw error;
54
+ }
55
+ await (0, node_timers_promises.setTimeout)(25);
56
+ }
57
+ throw new Error(`等待 React Native manifest 超时:${filename}`);
58
+ }
59
+ //#endregion
60
+ //#region src/metro/refresh.ts
61
+ function fingerprint(files) {
62
+ const hash = (0, node_crypto.createHash)("sha256");
63
+ for (const file of files) try {
64
+ const stat = node_fs.default.statSync(file, { bigint: true });
65
+ hash.update(JSON.stringify([
66
+ file,
67
+ String(stat.ino),
68
+ String(stat.size),
69
+ String(stat.mtimeNs),
70
+ String(stat.ctimeNs)
71
+ ]));
72
+ if (stat.isFile()) hash.update(node_fs.default.readFileSync(file));
73
+ } catch (error) {
74
+ if (error.code !== "ENOENT") throw error;
75
+ hash.update(JSON.stringify([file, "missing"]));
76
+ }
77
+ return hash.digest("hex");
78
+ }
79
+ /** 发布前验证显式输入的版本;watch 合并事件时也不能发布已过期的编译结果。 */
80
+ async function compileStableInput(files, compile, isCurrent) {
81
+ for (let attempt = 0; attempt < 5 && isCurrent(); attempt++) {
82
+ const before = fingerprint(files);
83
+ if (files.length) {
84
+ await (0, node_timers_promises.setTimeout)(25);
85
+ if (!isCurrent()) return;
86
+ if (before !== fingerprint(files)) continue;
87
+ }
88
+ try {
89
+ const result = await compile();
90
+ if (!isCurrent()) return;
91
+ if (before === fingerprint(files)) return result;
92
+ } catch (error) {
93
+ if (!isCurrent()) return;
94
+ if (before === fingerprint(files)) throw error;
95
+ }
96
+ }
97
+ if (isCurrent()) throw new Error("React Native 样式输入持续变化,5 次稳定性检查后仍未完成保存;请完成保存后重试。");
98
+ }
99
+ /** 等待期间出现新一轮刷新时,继续等待新一轮;过期轮的失败不能遮蔽新结果。 */
100
+ async function waitForCurrentRefresh(entry) {
101
+ for (;;) {
102
+ const ready = entry.ready;
103
+ try {
104
+ await ready;
105
+ } catch (error) {
106
+ if (ready === entry.ready) throw error;
107
+ }
108
+ if (ready === entry.ready) return;
109
+ }
110
+ }
111
+ //#endregion
112
+ //#region src/metro/virtual-module.ts
113
+ function virtualModuleCode(manifest) {
114
+ 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;`;
115
+ }
116
+ //#endregion
117
+ //#region src/metro/watch.ts
118
+ /** 文件监听绑定父目录,避免编辑器原子替换文件后仍监听已移除的 inode。 */
119
+ function watchInput(target, changed) {
120
+ const recursive = node_fs.default.statSync(target).isDirectory();
121
+ const watchRoot = recursive ? target : node_path.default.dirname(target);
122
+ return node_fs.default.watch(watchRoot, {
123
+ persistent: false,
124
+ recursive
125
+ }, (_event, filename) => {
126
+ if (recursive || filename === null || node_path.default.resolve(watchRoot, filename) === target) changed();
127
+ });
128
+ }
129
+ //#endregion
130
+ //#region src/metro.ts
131
+ const VIRTUAL_MANIFEST_MODULE = "@weapp-tailwindcss/react-native/virtual";
132
+ const registry = /* @__PURE__ */ new Map();
133
+ const appRootSingletonModules = /* @__PURE__ */ new Set(["react"]);
134
+ let nextId = 0;
135
+ function packageNameFromModuleId(moduleName) {
136
+ const segments = moduleName.split("/");
137
+ return moduleName.startsWith("@") ? segments.slice(0, 2).join("/") : segments[0] ?? "";
138
+ }
139
+ function shouldResolveFromAppRoot(moduleName, platform) {
140
+ const packageName = packageNameFromModuleId(moduleName);
141
+ if (appRootSingletonModules.has(packageName)) return true;
142
+ return platform !== "web" && packageName === "react-native";
143
+ }
144
+ function resolveAppRootSingleton(projectRoot, moduleName, platform) {
145
+ if (platform !== "web" || ![
146
+ "react",
147
+ "react-dom",
148
+ "react-native-web"
149
+ ].includes(packageNameFromModuleId(moduleName))) return;
150
+ try {
151
+ return {
152
+ type: "sourceFile",
153
+ filePath: (0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json")).resolve(moduleName)
154
+ };
155
+ } catch {
156
+ return;
157
+ }
158
+ }
159
+ /** 为 Metro worker 提供不依赖进程内 registry 的 manifest 入口。 */
160
+ function getManifestPathsForProjectRoot(projectRoot) {
161
+ const projectKey = (0, node_crypto.createHash)("sha256").update(node_path.default.resolve(projectRoot)).digest("hex").slice(0, 24);
162
+ const base = node_path.default.join(node_os.default.tmpdir(), "weapp-tailwindcss-native", `project-${projectKey}`);
163
+ return {
164
+ manifestPath: `${base}.manifest.json`,
165
+ manifestReadyPath: `${base}.manifest.ready`
166
+ };
167
+ }
168
+ function resolveInput(input, projectRoot) {
169
+ return input ? node_path.default.resolve(projectRoot, input) : void 0;
170
+ }
171
+ function emptyManifest() {
172
+ return {
173
+ version: 1,
174
+ classSet: [],
175
+ rules: {},
176
+ variables: {},
177
+ warnings: []
178
+ };
179
+ }
180
+ function writeVirtualModule(entry) {
181
+ node_fs.default.mkdirSync(node_path.default.dirname(entry.virtualPath), { recursive: true });
182
+ node_fs.default.mkdirSync(node_path.default.dirname(entry.manifestPath), { recursive: true });
183
+ writeManifestFile(entry.virtualPath, virtualModuleCode(entry.manifest));
184
+ writeManifestFile(entry.manifestPath, JSON.stringify(entry.manifest));
185
+ }
186
+ /**
187
+ * Metro worker 会把文件路径先相对化再恢复为绝对路径。虚拟模块必须位于
188
+ * 项目根目录内,否则 Windows 跨盘符路径会被错误拼接成 `D:\\C:\\...`。
189
+ */
190
+ function getVirtualModulePath(projectRoot, id) {
191
+ return node_path.default.join(projectRoot, "node_modules", ".cache", "weapp-tailwindcss-native", `${id}.js`);
192
+ }
193
+ function markManifestPending(entry) {
194
+ node_fs.default.rmSync(entry.manifestReadyPath, { force: true });
195
+ }
196
+ function markManifestReady(entry) {
197
+ writeManifestFile(entry.manifestReadyPath, JSON.stringify({
198
+ revision: (0, node_crypto.randomUUID)(),
199
+ version: entry.version,
200
+ virtualPath: entry.virtualPath
201
+ }));
202
+ }
203
+ async function compileOptions(options, projectRoot) {
204
+ if (options.manifest) return options.manifest;
205
+ if (options.input) {
206
+ const { generateNativeStylesheet } = await (require("url").pathToFileURL(__filename).href.endsWith(".ts") ? Promise.resolve().then(() => require("./tailwind.cjs")) : Promise.resolve().then(() => require("./tailwind.cjs")));
207
+ return generateNativeStylesheet({
208
+ projectRoot,
209
+ cssEntries: [resolveInput(options.input, projectRoot)],
210
+ candidates: options.classSet,
211
+ sourceGlobs: options.sourceGlobs
212
+ });
213
+ }
214
+ return require_compiler.compileNativeStylesheet(options.css ?? "", { classSet: options.classSet });
215
+ }
216
+ function register(options) {
217
+ const id = `weapp-tailwindcss-native-${nextId++}`;
218
+ const projectRoot = node_path.default.resolve(options.projectRoot ?? node_process.default.cwd());
219
+ const manifestPaths = getManifestPathsForProjectRoot(projectRoot);
220
+ const entry = {
221
+ version: 0,
222
+ manifest: options.manifest ?? (options.css ? require_compiler.compileNativeStylesheet(options.css, { classSet: options.classSet }) : emptyManifest()),
223
+ projectRoot,
224
+ virtualPath: getVirtualModulePath(projectRoot, id),
225
+ manifestPath: manifestPaths.manifestPath,
226
+ manifestReadyPath: manifestPaths.manifestReadyPath,
227
+ ready: Promise.resolve(),
228
+ refresh: async () => {}
229
+ };
230
+ registry.set(id, entry);
231
+ markManifestPending(entry);
232
+ writeVirtualModule(entry);
233
+ const inputFiles = [...new Set([...options.watchFiles ?? [], ...options.input ? [options.input] : []].map((file) => node_path.default.resolve(projectRoot, file)))];
234
+ let generation = 0;
235
+ entry.refresh = () => {
236
+ const currentGeneration = ++generation;
237
+ markManifestPending(entry);
238
+ entry.ready = (async () => {
239
+ try {
240
+ const manifest = await compileStableInput(inputFiles, () => compileOptions(options, projectRoot), () => currentGeneration === generation);
241
+ if (manifest && currentGeneration === generation) {
242
+ entry.manifest = manifest;
243
+ entry.version++;
244
+ writeVirtualModule(entry);
245
+ markManifestReady(entry);
246
+ }
247
+ } catch (error) {
248
+ if (currentGeneration !== generation) return;
249
+ const message = `生成 React Native manifest 失败:${error instanceof Error ? error.message : String(error)}`;
250
+ writeManifestFile(entry.manifestReadyPath, JSON.stringify({ error: message }));
251
+ throw new Error(message, { cause: error });
252
+ }
253
+ })();
254
+ entry.ready.catch((error) => console.error(error.message));
255
+ return entry.ready;
256
+ };
257
+ entry.refresh();
258
+ const sourceRoots = (options.sourceGlobs ?? []).map((pattern) => pattern.split(/[*{[]/, 1)[0]?.replace(/[/\\]$/, "")).filter((root) => Boolean(root));
259
+ [
260
+ ...options.watchFiles ?? [],
261
+ ...options.input ? [options.input] : [],
262
+ ...sourceRoots
263
+ ].map((file) => {
264
+ const target = node_path.default.resolve(projectRoot, file);
265
+ try {
266
+ return watchInput(target, () => {
267
+ entry.refresh();
268
+ });
269
+ } catch {
270
+ return;
271
+ }
272
+ });
273
+ return {
274
+ id,
275
+ entry
276
+ };
277
+ }
278
+ function getRegisteredVirtualModule(filename) {
279
+ for (const entry of registry.values()) if (entry.virtualPath === filename) return entry;
280
+ }
281
+ async function getRegisteredManifest(id) {
282
+ const registered = registry.get(id);
283
+ if (!registered) return;
284
+ await waitForCurrentRefresh(registered);
285
+ return registered.manifest;
286
+ }
287
+ /** 等待并读取由 Metro 配置注册的 manifest 文件,兼容未透传自定义 Metro id 的 transformer。 */
288
+ async function getRegisteredManifestByPath(filename) {
289
+ for (const entry of registry.values()) if (entry.manifestPath === filename) {
290
+ await waitForCurrentRefresh(entry);
291
+ return entry.manifest;
292
+ }
293
+ }
294
+ /** 按 Metro 传入的项目根目录读取当前注册项,兼容 transformer 未透传自定义字段的实现。 */
295
+ async function getRegisteredManifestByProjectRoot(projectRoot) {
296
+ const resolvedRoot = node_path.default.resolve(projectRoot);
297
+ const entry = [...registry.values()].filter((entry) => entry.projectRoot === resolvedRoot).at(-1);
298
+ if (!entry) return;
299
+ await waitForCurrentRefresh(entry);
300
+ return entry.manifest;
301
+ }
302
+ function getVirtualModuleCode(filename) {
303
+ const registered = getRegisteredVirtualModule(filename);
304
+ if (!registered) return;
305
+ return virtualModuleCode(registered.manifest);
306
+ }
307
+ async function getVirtualModuleCodeAsync(filename) {
308
+ const registered = getRegisteredVirtualModule(filename);
309
+ if (!registered) return;
310
+ await waitForCurrentRefresh(registered);
311
+ return getVirtualModuleCode(filename);
312
+ }
313
+ function withWeappTailwindcss(config, options = {}) {
314
+ if (typeof config === "function" || config && typeof config.then === "function") return Promise.resolve(typeof config === "function" ? config() : config).then((resolved) => withWeappTailwindcss(resolved, options));
315
+ const resolvedConfig = config;
316
+ const { id, entry } = register(options);
317
+ const originalResolver = resolvedConfig.resolver?.resolveRequest;
318
+ const sourceExts = resolvedConfig.resolver?.sourceExts ?? [];
319
+ const virtualResolver = (context, moduleName, platform) => {
320
+ if (moduleName === "@weapp-tailwindcss/react-native/virtual") return {
321
+ type: "sourceFile",
322
+ filePath: entry.virtualPath
323
+ };
324
+ const appRootSingleton = resolveAppRootSingleton(entry.projectRoot, moduleName, platform);
325
+ if (appRootSingleton) return appRootSingleton;
326
+ const resolverContext = context;
327
+ const anchoredContext = resolverContext.originModulePath === entry.virtualPath || shouldResolveFromAppRoot(moduleName, platform) ? {
328
+ ...resolverContext,
329
+ originModulePath: node_path.default.join(entry.projectRoot, "package.json")
330
+ } : resolverContext;
331
+ return originalResolver?.(anchoredContext, moduleName, platform) ?? resolverContext.resolveRequest?.(anchoredContext, moduleName, platform) ?? resolveAppRootSingleton(entry.projectRoot, moduleName, platform);
332
+ };
333
+ return {
334
+ ...resolvedConfig,
335
+ watchFolders: [.../* @__PURE__ */ new Set([...resolvedConfig.watchFolders ?? [], node_path.default.dirname(entry.virtualPath)])],
336
+ transformerPath: (0, node_url.fileURLToPath)(new node_url.URL(require("url").pathToFileURL(__filename).href.includes(".ts") ? "./metro-transformer.ts" : "./metro-transformer.js", require("url").pathToFileURL(__filename).href)),
337
+ transformer: {
338
+ ...resolvedConfig.transformer,
339
+ weappTailwindcssMetroId: id,
340
+ weappTailwindcssOriginalTransformerPath: resolvedConfig.transformerPath,
341
+ weappTailwindcssManifestPath: entry.manifestPath,
342
+ weappTailwindcssManifestReadyPath: entry.manifestReadyPath,
343
+ weappTailwindcssVirtualModulePath: entry.virtualPath
344
+ },
345
+ resolver: {
346
+ ...resolvedConfig.resolver,
347
+ extraNodeModules: {
348
+ ...resolvedConfig.resolver?.extraNodeModules,
349
+ "react": node_path.default.resolve(entry.projectRoot, "node_modules/react"),
350
+ "react-dom": node_path.default.resolve(entry.projectRoot, "node_modules/react-dom")
351
+ },
352
+ sourceExts: sourceExts.includes("css") ? sourceExts : [...sourceExts, "css"],
353
+ resolveRequest: virtualResolver
354
+ }
355
+ };
356
+ }
357
+ //#endregion
358
+ Object.defineProperty(exports, "VIRTUAL_MANIFEST_MODULE", {
359
+ enumerable: true,
360
+ get: function() {
361
+ return VIRTUAL_MANIFEST_MODULE;
362
+ }
363
+ });
364
+ Object.defineProperty(exports, "getManifestPathsForProjectRoot", {
365
+ enumerable: true,
366
+ get: function() {
367
+ return getManifestPathsForProjectRoot;
368
+ }
369
+ });
370
+ Object.defineProperty(exports, "getRegisteredManifest", {
371
+ enumerable: true,
372
+ get: function() {
373
+ return getRegisteredManifest;
374
+ }
375
+ });
376
+ Object.defineProperty(exports, "getRegisteredManifestByPath", {
377
+ enumerable: true,
378
+ get: function() {
379
+ return getRegisteredManifestByPath;
380
+ }
381
+ });
382
+ Object.defineProperty(exports, "getRegisteredManifestByProjectRoot", {
383
+ enumerable: true,
384
+ get: function() {
385
+ return getRegisteredManifestByProjectRoot;
386
+ }
387
+ });
388
+ Object.defineProperty(exports, "getRegisteredVirtualModule", {
389
+ enumerable: true,
390
+ get: function() {
391
+ return getRegisteredVirtualModule;
392
+ }
393
+ });
394
+ Object.defineProperty(exports, "getVirtualModuleCode", {
395
+ enumerable: true,
396
+ get: function() {
397
+ return getVirtualModuleCode;
398
+ }
399
+ });
400
+ Object.defineProperty(exports, "getVirtualModuleCodeAsync", {
401
+ enumerable: true,
402
+ get: function() {
403
+ return getVirtualModuleCodeAsync;
404
+ }
405
+ });
406
+ Object.defineProperty(exports, "readPublishedSnapshot", {
407
+ enumerable: true,
408
+ get: function() {
409
+ return readPublishedSnapshot;
410
+ }
411
+ });
412
+ Object.defineProperty(exports, "virtualModuleCode", {
413
+ enumerable: true,
414
+ get: function() {
415
+ return virtualModuleCode;
416
+ }
417
+ });
418
+ Object.defineProperty(exports, "waitForCurrentRefresh", {
419
+ enumerable: true,
420
+ get: function() {
421
+ return waitForCurrentRefresh;
422
+ }
423
+ });
424
+ Object.defineProperty(exports, "withWeappTailwindcss", {
425
+ enumerable: true,
426
+ get: function() {
427
+ return withWeappTailwindcss;
428
+ }
429
+ });
@@ -1,23 +1,32 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_rolldown_runtime = require("./rolldown-runtime-VH7oDXx4.cjs");
3
3
  const require_babel = require("./babel.cjs");
4
- const require_metro = require("./metro.cjs");
4
+ const require_metro = require("./metro-Ds2ZUc6_.cjs");
5
5
  let node_fs = require("node:fs");
6
6
  node_fs = require_rolldown_runtime.__toESM(node_fs, 1);
7
7
  let node_module = require("node:module");
8
8
  let node_process = require("node:process");
9
9
  node_process = require_rolldown_runtime.__toESM(node_process, 1);
10
+ let node_buffer = require("node:buffer");
10
11
  let _babel_core = require("@babel/core");
11
12
  //#region src/metro-transformer.ts
12
13
  const require$1 = (0, node_module.createRequire)(require("url").pathToFileURL(__filename).href);
13
14
  async function transform(config, projectRoot, filename, data, options) {
14
- const virtualCode = await require_metro.getVirtualModuleCodeAsync(filename);
15
+ const registeredVirtual = require_metro.getRegisteredVirtualModule(filename);
16
+ if (registeredVirtual) await require_metro.waitForCurrentRefresh(registeredVirtual);
15
17
  const metroId = config.weappTailwindcssMetroId;
16
18
  const manifestPath = config.weappTailwindcssManifestPath;
17
19
  const manifestReadyPath = config.weappTailwindcssManifestReadyPath;
18
20
  const virtualModulePath = config.weappTailwindcssVirtualModulePath;
19
21
  const projectManifestPaths = require_metro.getManifestPathsForProjectRoot(projectRoot);
20
- const manifest = (metroId ? await require_metro.getRegisteredManifest(metroId) : void 0) ?? (manifestPath ? await require_metro.getRegisteredManifestByPath(manifestPath) : void 0) ?? await require_metro.getRegisteredManifestByProjectRoot(projectRoot) ?? await readManifest(manifestPath ?? projectManifestPaths.manifestPath, manifestReadyPath ?? projectManifestPaths.manifestReadyPath);
22
+ const registeredManifest = registeredVirtual?.manifest ?? (metroId ? await require_metro.getRegisteredManifest(metroId) : void 0) ?? (manifestPath ? await require_metro.getRegisteredManifestByPath(manifestPath) : void 0) ?? await require_metro.getRegisteredManifestByProjectRoot(projectRoot);
23
+ const snapshot = registeredManifest ? {
24
+ manifest: registeredManifest,
25
+ virtualPath: registeredVirtual?.virtualPath
26
+ } : await require_metro.readPublishedSnapshot(manifestPath ?? projectManifestPaths.manifestPath, manifestReadyPath ?? projectManifestPaths.manifestReadyPath);
27
+ const manifest = snapshot?.manifest;
28
+ const publishedVirtualPath = snapshot?.virtualPath ?? virtualModulePath;
29
+ const virtualCode = manifest && publishedVirtualPath === filename ? require_metro.virtualModuleCode(manifest) : void 0;
21
30
  if (node_process.default.env.WEAPP_TW_RN_DEBUG === "1" && !filename.replaceAll("\\", "/").includes("/node_modules/")) console.error(`[react-native-debug] ${JSON.stringify({
22
31
  filename,
23
32
  projectRoot,
@@ -30,8 +39,7 @@ async function transform(config, projectRoot, filename, data, options) {
30
39
  manifestReady: manifestReadyPath ? node_fs.default.existsSync(manifestReadyPath) : null,
31
40
  inputBytes: data.byteLength
32
41
  })}`);
33
- let source = virtualCode ? Buffer.from(virtualCode) : data;
34
- if (!virtualCode && manifest && virtualModulePath === filename) source = Buffer.from(node_fs.default.readFileSync(filename));
42
+ let source = virtualCode ? node_buffer.Buffer.from(virtualCode) : data;
35
43
  if (!virtualCode && manifest && /\.(?:[cm]?[jt]sx?|flow)$/i.test(filename) && !filename.replaceAll("\\", "/").includes("/node_modules/")) {
36
44
  const transformed = (0, _babel_core.transformSync)(data.toString(), {
37
45
  filename,
@@ -44,26 +52,10 @@ async function transform(config, projectRoot, filename, data, options) {
44
52
  staticStyleMap: manifest.staticLookup
45
53
  }]]
46
54
  });
47
- if (transformed?.code) source = Buffer.from(transformed.code);
55
+ if (transformed?.code) source = node_buffer.Buffer.from(transformed.code);
48
56
  }
49
57
  const originalPath = config.weappTailwindcssOriginalTransformerPath;
50
58
  return (originalPath ? require$1(originalPath) : require$1("metro-react-native-babel-transformer")).transform(config, projectRoot, filename, source, options);
51
59
  }
52
- async function readManifest(filename, readyPath) {
53
- if (readyPath) {
54
- const deadline = Date.now() + 12e4;
55
- while (Date.now() < deadline) try {
56
- await node_fs.default.promises.access(readyPath);
57
- break;
58
- } catch {
59
- await new Promise((resolve) => setTimeout(resolve, 25));
60
- }
61
- }
62
- try {
63
- return JSON.parse(await node_fs.default.promises.readFile(filename, "utf8"));
64
- } catch {
65
- return;
66
- }
67
- }
68
60
  //#endregion
69
61
  exports.transform = transform;
@@ -1,19 +1,28 @@
1
1
  import weappReactNativeBabel from "./babel.js";
2
- import { getManifestPathsForProjectRoot, getRegisteredManifest, getRegisteredManifestByPath, getRegisteredManifestByProjectRoot, getVirtualModuleCodeAsync } from "./metro.js";
2
+ import { a as getRegisteredManifestByProjectRoot, d as waitForCurrentRefresh, f as readPublishedSnapshot, i as getRegisteredManifestByPath, n as getManifestPathsForProjectRoot, o as getRegisteredVirtualModule, r as getRegisteredManifest, u as virtualModuleCode } from "./metro-BZbdIyTH.js";
3
3
  import { createRequire } from "node:module";
4
4
  import fs from "node:fs";
5
5
  import process from "node:process";
6
+ import { Buffer } from "node:buffer";
6
7
  import { transformSync } from "@babel/core";
7
8
  //#region src/metro-transformer.ts
8
9
  const require = createRequire(import.meta.url);
9
10
  async function transform(config, projectRoot, filename, data, options) {
10
- const virtualCode = await getVirtualModuleCodeAsync(filename);
11
+ const registeredVirtual = getRegisteredVirtualModule(filename);
12
+ if (registeredVirtual) await waitForCurrentRefresh(registeredVirtual);
11
13
  const metroId = config.weappTailwindcssMetroId;
12
14
  const manifestPath = config.weappTailwindcssManifestPath;
13
15
  const manifestReadyPath = config.weappTailwindcssManifestReadyPath;
14
16
  const virtualModulePath = config.weappTailwindcssVirtualModulePath;
15
17
  const projectManifestPaths = getManifestPathsForProjectRoot(projectRoot);
16
- const manifest = (metroId ? await getRegisteredManifest(metroId) : void 0) ?? (manifestPath ? await getRegisteredManifestByPath(manifestPath) : void 0) ?? await getRegisteredManifestByProjectRoot(projectRoot) ?? await readManifest(manifestPath ?? projectManifestPaths.manifestPath, manifestReadyPath ?? projectManifestPaths.manifestReadyPath);
18
+ const registeredManifest = registeredVirtual?.manifest ?? (metroId ? await getRegisteredManifest(metroId) : void 0) ?? (manifestPath ? await getRegisteredManifestByPath(manifestPath) : void 0) ?? await getRegisteredManifestByProjectRoot(projectRoot);
19
+ const snapshot = registeredManifest ? {
20
+ manifest: registeredManifest,
21
+ virtualPath: registeredVirtual?.virtualPath
22
+ } : await readPublishedSnapshot(manifestPath ?? projectManifestPaths.manifestPath, manifestReadyPath ?? projectManifestPaths.manifestReadyPath);
23
+ const manifest = snapshot?.manifest;
24
+ const publishedVirtualPath = snapshot?.virtualPath ?? virtualModulePath;
25
+ const virtualCode = manifest && publishedVirtualPath === filename ? virtualModuleCode(manifest) : void 0;
17
26
  if (process.env.WEAPP_TW_RN_DEBUG === "1" && !filename.replaceAll("\\", "/").includes("/node_modules/")) console.error(`[react-native-debug] ${JSON.stringify({
18
27
  filename,
19
28
  projectRoot,
@@ -27,7 +36,6 @@ async function transform(config, projectRoot, filename, data, options) {
27
36
  inputBytes: data.byteLength
28
37
  })}`);
29
38
  let source = virtualCode ? Buffer.from(virtualCode) : data;
30
- if (!virtualCode && manifest && virtualModulePath === filename) source = Buffer.from(fs.readFileSync(filename));
31
39
  if (!virtualCode && manifest && /\.(?:[cm]?[jt]sx?|flow)$/i.test(filename) && !filename.replaceAll("\\", "/").includes("/node_modules/")) {
32
40
  const transformed = transformSync(data.toString(), {
33
41
  filename,
@@ -45,21 +53,5 @@ async function transform(config, projectRoot, filename, data, options) {
45
53
  const originalPath = config.weappTailwindcssOriginalTransformerPath;
46
54
  return (originalPath ? require(originalPath) : require("metro-react-native-babel-transformer")).transform(config, projectRoot, filename, source, options);
47
55
  }
48
- async function readManifest(filename, readyPath) {
49
- if (readyPath) {
50
- const deadline = Date.now() + 12e4;
51
- while (Date.now() < deadline) try {
52
- await fs.promises.access(readyPath);
53
- break;
54
- } catch {
55
- await new Promise((resolve) => setTimeout(resolve, 25));
56
- }
57
- }
58
- try {
59
- return JSON.parse(await fs.promises.readFile(filename, "utf8"));
60
- } catch {
61
- return;
62
- }
63
- }
64
56
  //#endregion
65
57
  export { transform };