@tamagui/metro-plugin 2.7.7 → 3.0.0-beta.643.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 +12 -0
- package/dist/cjs/babel.cjs +77 -0
- package/dist/cjs/compilerCache.cjs +237 -0
- package/dist/cjs/diagnostics.cjs +41 -0
- package/dist/cjs/frontend.cjs +870 -0
- package/dist/cjs/index.cjs +102 -0
- package/dist/cjs/lowering.cjs +109 -0
- package/dist/cjs/metroResolver.cjs +197 -0
- package/dist/cjs/transformOptions.cjs +35 -0
- package/dist/cjs/transformer.cjs +142 -0
- package/dist/cjs/zeroRuntime.cjs +140 -0
- package/dist/cjs/zeroSerializer.cjs +150 -0
- package/dist/esm/babel.mjs +52 -0
- package/dist/esm/babel.mjs.map +1 -0
- package/dist/esm/compilerCache.mjs +212 -0
- package/dist/esm/compilerCache.mjs.map +1 -0
- package/dist/esm/diagnostics.mjs +18 -0
- package/dist/esm/diagnostics.mjs.map +1 -0
- package/dist/esm/frontend.mjs +839 -0
- package/dist/esm/frontend.mjs.map +1 -0
- package/dist/esm/index.mjs +64 -22
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/lowering.mjs +89 -0
- package/dist/esm/lowering.mjs.map +1 -0
- package/dist/esm/metroResolver.mjs +173 -0
- package/dist/esm/metroResolver.mjs.map +1 -0
- package/dist/esm/transformOptions.mjs +14 -0
- package/dist/esm/transformOptions.mjs.map +1 -0
- package/dist/esm/transformer.mjs +119 -0
- package/dist/esm/transformer.mjs.map +1 -0
- package/dist/esm/zeroRuntime.mjs +105 -0
- package/dist/esm/zeroRuntime.mjs.map +1 -0
- package/dist/esm/zeroSerializer.mjs +123 -0
- package/dist/esm/zeroSerializer.mjs.map +1 -0
- package/package.json +33 -5
- package/src/babel.ts +87 -0
- package/src/compilerCache.ts +346 -0
- package/src/diagnostics.ts +47 -0
- package/src/frontend.ts +1178 -0
- package/src/index.ts +117 -14
- package/src/lowering.ts +136 -0
- package/src/metroResolver.ts +209 -0
- package/src/transformOptions.ts +36 -0
- package/src/transformer.ts +210 -0
- package/src/zeroRuntime.ts +212 -0
- package/src/zeroSerializer.ts +175 -0
- package/types/babel.d.ts +28 -0
- package/types/babel.d.ts.map +11 -0
- package/types/compilerCache.d.ts +63 -0
- package/types/compilerCache.d.ts.map +11 -0
- package/types/diagnostics.d.ts +16 -0
- package/types/diagnostics.d.ts.map +11 -0
- package/types/frontend.d.ts +73 -0
- package/types/frontend.d.ts.map +11 -0
- package/types/index.d.ts +49 -32
- package/types/index.d.ts.map +11 -1
- package/types/lowering.d.ts +20 -0
- package/types/lowering.d.ts.map +11 -0
- package/types/metroResolver.d.ts +21 -0
- package/types/metroResolver.d.ts.map +11 -0
- package/types/transformOptions.d.ts +13 -0
- package/types/transformOptions.d.ts.map +11 -0
- package/types/transformer.d.ts +26 -0
- package/types/transformer.d.ts.map +11 -0
- package/types/zeroRuntime.d.ts +75 -0
- package/types/zeroRuntime.d.ts.map +11 -0
- package/types/zeroSerializer.d.ts +6 -0
- package/types/zeroSerializer.d.ts.map +11 -0
- package/dist/cjs/index.js +0 -45
- package/dist/cjs/index.js.map +0 -6
- package/dist/esm/index.js +0 -25
- package/dist/esm/index.js.map +0 -1
|
@@ -0,0 +1,839 @@
|
|
|
1
|
+
import { existsSync, watch } from "node:fs";
|
|
2
|
+
import { readFile, readdir, realpath } from "node:fs/promises";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
5
|
+
import ignore from "ignore";
|
|
6
|
+
import { JsonFileCache, ModulePlanCache, PLAN_CACHE_SCHEMA_VERSION, ProjectGraph, contentHash, defaultPlanCacheRoot, lowerModule, materializeModule, moduleClosureDigest, moduleClosureNode, planCacheKey, resolvedModuleId, stableStringify, yukuFactory } from "@tamagui/compiler-core";
|
|
7
|
+
import Static, { createTamaguiCompilerHost } from "@tamagui/static";
|
|
8
|
+
import { compileWithUserBabel, userBabelCacheKey } from "./babel.mjs";
|
|
9
|
+
import { zeroModuleKey } from "./zeroRuntime.mjs";
|
|
10
|
+
import { METRO_COMPILER_CACHE_VERSION, MetroCompilerCache, defaultMetroCompilerCacheRoot } from "./compilerCache.mjs";
|
|
11
|
+
import { metroDiagnostic } from "./diagnostics.mjs";
|
|
12
|
+
import { createMetroCompilerResolver, isCompilerSourceFile, moduleSpecifiersFromAst } from "./metroResolver.mjs";
|
|
13
|
+
|
|
14
|
+
const METRO_RECORD_CACHE_VERSION = 1;
|
|
15
|
+
function compareCodeUnits(left, right) {
|
|
16
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
17
|
+
}
|
|
18
|
+
const requireFromFrontend = createRequire(typeof __filename === "string" ? __filename : import.meta.url);
|
|
19
|
+
const compilerImplementationVersions = [
|
|
20
|
+
"@tamagui/metro-plugin",
|
|
21
|
+
"@tamagui/static",
|
|
22
|
+
"@tamagui/compiler-core"
|
|
23
|
+
].map((packageName) => {
|
|
24
|
+
const { version } = requireFromFrontend(`${packageName}/package.json`);
|
|
25
|
+
return `${packageName}@${version}`;
|
|
26
|
+
});
|
|
27
|
+
function scanOptionsHash(options, projectGeneration, projectSourcesHash) {
|
|
28
|
+
return contentHash(JSON.stringify({
|
|
29
|
+
options,
|
|
30
|
+
projectGeneration,
|
|
31
|
+
projectSourcesHash
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
const speculativeWalkExcludedDirs = /* @__PURE__ */ new Set([
|
|
35
|
+
"__tests__",
|
|
36
|
+
"e2e",
|
|
37
|
+
"flows",
|
|
38
|
+
"plugins",
|
|
39
|
+
"screenshots",
|
|
40
|
+
"scripts",
|
|
41
|
+
"test",
|
|
42
|
+
"test-results",
|
|
43
|
+
"tests"
|
|
44
|
+
]);
|
|
45
|
+
async function walkProjectSources(root) {
|
|
46
|
+
const inherited = [];
|
|
47
|
+
let ancestor = root;
|
|
48
|
+
while (!existsSync(join(ancestor, ".git"))) {
|
|
49
|
+
const parent = dirname(ancestor);
|
|
50
|
+
if (parent === ancestor) break;
|
|
51
|
+
inherited.unshift(parent);
|
|
52
|
+
ancestor = parent;
|
|
53
|
+
}
|
|
54
|
+
const rootScopes = [];
|
|
55
|
+
for (const dir of inherited) {
|
|
56
|
+
const source = await readFile(join(dir, ".gitignore"), "utf8").catch(() => null);
|
|
57
|
+
if (source) rootScopes.push({
|
|
58
|
+
dir,
|
|
59
|
+
matcher: ignore().add(source)
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const found = [];
|
|
63
|
+
const stack = [{
|
|
64
|
+
dir: root,
|
|
65
|
+
scopes: rootScopes
|
|
66
|
+
}];
|
|
67
|
+
while (stack.length) {
|
|
68
|
+
const { dir, scopes } = stack.pop();
|
|
69
|
+
let entries;
|
|
70
|
+
try {
|
|
71
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
72
|
+
} catch {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
let active = scopes;
|
|
76
|
+
if (entries.some((entry) => entry.isFile() && entry.name === ".gitignore")) {
|
|
77
|
+
const source = await readFile(join(dir, ".gitignore"), "utf8").catch(() => null);
|
|
78
|
+
if (source) active = [...scopes, {
|
|
79
|
+
dir,
|
|
80
|
+
matcher: ignore().add(source)
|
|
81
|
+
}];
|
|
82
|
+
}
|
|
83
|
+
for (const entry of entries) {
|
|
84
|
+
if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
|
|
85
|
+
const isDirectory = entry.isDirectory();
|
|
86
|
+
if (isDirectory && speculativeWalkExcludedDirs.has(entry.name)) continue;
|
|
87
|
+
if (!isDirectory && !(entry.isFile() && isCompilerSourceFile(entry.name))) continue;
|
|
88
|
+
if (!isDirectory && (/(?:^|[-.])(?:probe|run|spec|tests?)(?:[-.]|$)/i.test(entry.name) || /\.(?:build|config|workspace)\.[cm]?[jt]sx?$/.test(entry.name))) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const path = join(dir, entry.name);
|
|
92
|
+
let ignored = false;
|
|
93
|
+
for (const scope of active) {
|
|
94
|
+
const relativePath = relative(scope.dir, path);
|
|
95
|
+
if (!relativePath || relativePath.startsWith("..")) continue;
|
|
96
|
+
const candidate = relativePath.split(sep).join("/") + (isDirectory ? "/" : "");
|
|
97
|
+
if (scope.matcher.ignores(candidate)) {
|
|
98
|
+
ignored = true;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (ignored) continue;
|
|
103
|
+
if (isDirectory) stack.push({
|
|
104
|
+
dir: path,
|
|
105
|
+
scopes: active
|
|
106
|
+
});
|
|
107
|
+
else found.push(path);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return found.sort(compareCodeUnits);
|
|
111
|
+
}
|
|
112
|
+
function compilerTarget(platform) {
|
|
113
|
+
return platform === "web" ? "web" : "native";
|
|
114
|
+
}
|
|
115
|
+
function retainsLiveGraph(options) {
|
|
116
|
+
return options.dev && options.hot;
|
|
117
|
+
}
|
|
118
|
+
var MetroCompilerFrontend = class {
|
|
119
|
+
constructor(config) {
|
|
120
|
+
this.config = config;
|
|
121
|
+
this.#cacheBaseRoot = config.cacheRoot ?? defaultMetroCompilerCacheRoot(config.projectRoot);
|
|
122
|
+
this.#resolver = createMetroCompilerResolver(config);
|
|
123
|
+
}
|
|
124
|
+
config;
|
|
125
|
+
#cacheBaseRoot;
|
|
126
|
+
#entries = /* @__PURE__ */ new Map();
|
|
127
|
+
#records = /* @__PURE__ */ new Map();
|
|
128
|
+
#watchers = /* @__PURE__ */ new Map();
|
|
129
|
+
#resolver;
|
|
130
|
+
#graph = null;
|
|
131
|
+
#host = null;
|
|
132
|
+
#projectGeneration = null;
|
|
133
|
+
#publishedGeneration = null;
|
|
134
|
+
#scanOptions = null;
|
|
135
|
+
#scanOptionsHash = null;
|
|
136
|
+
#operationQueue = Promise.resolve();
|
|
137
|
+
#tamaguiConfig = null;
|
|
138
|
+
#zeroEntryGraph = null;
|
|
139
|
+
#planKeys = /* @__PURE__ */ new Map();
|
|
140
|
+
#recordCache = null;
|
|
141
|
+
#recordCacheIdentity = null;
|
|
142
|
+
#planCache = null;
|
|
143
|
+
#planCacheStamp = null;
|
|
144
|
+
get metroResolverVersion() {
|
|
145
|
+
return this.#resolver.version;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Per-file cache accounting for the last scan. The point of these caches is
|
|
149
|
+
* that one edited module leaves every other module's entry valid, and this is
|
|
150
|
+
* how that is observed rather than assumed.
|
|
151
|
+
*/
|
|
152
|
+
get compileCacheStats() {
|
|
153
|
+
const empty = {
|
|
154
|
+
hits: 0,
|
|
155
|
+
misses: 0,
|
|
156
|
+
writes: 0
|
|
157
|
+
};
|
|
158
|
+
return {
|
|
159
|
+
plans: this.#planCache?.stats ?? empty,
|
|
160
|
+
records: this.#recordCache?.stats ?? empty
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
cacheRootFor(platform) {
|
|
164
|
+
return join(this.#cacheBaseRoot, platform ?? "default");
|
|
165
|
+
}
|
|
166
|
+
scan(options) {
|
|
167
|
+
return this.#enqueue(() => this.#scan(options));
|
|
168
|
+
}
|
|
169
|
+
async #scan(options, preparedProject, preparedProjectSources) {
|
|
170
|
+
this.#scanOptions = options;
|
|
171
|
+
this.#publishedGeneration = null;
|
|
172
|
+
const diagnostics = [];
|
|
173
|
+
const entryRoots = (await Promise.all(options.entryFiles.map((path) => realpath(resolve(this.config.projectRoot, path))))).sort(compareCodeUnits);
|
|
174
|
+
const compilerProject = preparedProject ?? await this.#loadCompilerProject(options, entryRoots[0], diagnostics);
|
|
175
|
+
this.#projectGeneration = compilerProject.generation;
|
|
176
|
+
const projectSources = preparedProjectSources ?? await walkProjectSources(this.config.projectRoot);
|
|
177
|
+
const projectSourcesHash = contentHash(JSON.stringify(projectSources));
|
|
178
|
+
this.#scanOptionsHash = scanOptionsHash(options, compilerProject.generation, projectSourcesHash);
|
|
179
|
+
this.#installCaches(options, compilerProject, projectSourcesHash);
|
|
180
|
+
const speculativeRoots = /* @__PURE__ */ new Set();
|
|
181
|
+
for (const file of projectSources) {
|
|
182
|
+
try {
|
|
183
|
+
const id = await realpath(file);
|
|
184
|
+
if (!entryRoots.includes(id)) speculativeRoots.add(id);
|
|
185
|
+
} catch {}
|
|
186
|
+
}
|
|
187
|
+
const roots = [.../* @__PURE__ */ new Set([...entryRoots, ...speculativeRoots])].sort(compareCodeUnits);
|
|
188
|
+
const queue = [...roots];
|
|
189
|
+
const queued = new Set(queue);
|
|
190
|
+
for (const watcher of this.#watchers.values()) watcher.close();
|
|
191
|
+
this.#watchers.clear();
|
|
192
|
+
this.#records.clear();
|
|
193
|
+
while (queue.length) {
|
|
194
|
+
const path = queue.shift();
|
|
195
|
+
try {
|
|
196
|
+
const record = await this.#compileRecord(path, options, diagnostics);
|
|
197
|
+
this.#records.set(record.input.id, record);
|
|
198
|
+
for (const dependency of record.input.imports) {
|
|
199
|
+
if (dependency.external || !isCompilerSourceFile(dependency.resolvedId) || queued.has(dependency.resolvedId)) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
queued.add(dependency.resolvedId);
|
|
203
|
+
queue.push(dependency.resolvedId);
|
|
204
|
+
}
|
|
205
|
+
} catch (error) {
|
|
206
|
+
if (speculativeRoots.has(path)) continue;
|
|
207
|
+
const diagnostic = metroDiagnostic("metro/transform-failed", `Failed to compile ${path}: ${error instanceof Error ? error.message : String(error)}`, { moduleId: path });
|
|
208
|
+
diagnostics.push(diagnostic);
|
|
209
|
+
this.#report(diagnostic);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (!compilerProject.projectInfo.tamaguiConfig || !compilerProject.projectInfo.components) {
|
|
213
|
+
throw new Error("Metro compiler project has no Tamagui config or components");
|
|
214
|
+
}
|
|
215
|
+
this.#tamaguiConfig = compilerProject.projectInfo.tamaguiConfig;
|
|
216
|
+
this.#entries.clear();
|
|
217
|
+
const unplanned = await this.#restorePlans(options);
|
|
218
|
+
const zero = this.config.zero;
|
|
219
|
+
this.#graph = null;
|
|
220
|
+
this.#host = null;
|
|
221
|
+
if (unplanned.length || retainsLiveGraph(options)) {
|
|
222
|
+
this.#graph = new ProjectGraph(yukuFactory, { modules: [...this.#records.values()].map(({ input }) => input) });
|
|
223
|
+
this.#host = createTamaguiCompilerHost({
|
|
224
|
+
target: compilerTarget(options.platform),
|
|
225
|
+
tamaguiConfig: compilerProject.projectInfo.tamaguiConfig,
|
|
226
|
+
components: compilerProject.projectInfo.components,
|
|
227
|
+
componentModules: compilerProject.componentModules.map(({ moduleName, id }) => ({
|
|
228
|
+
moduleName,
|
|
229
|
+
resolvedId: id
|
|
230
|
+
})),
|
|
231
|
+
disablePartialExtraction: compilerProject.disablePartialExtraction,
|
|
232
|
+
experimentalNativeFastPath: compilerProject.experimentalNativeFastPath,
|
|
233
|
+
zeroRuntime: compilerProject.zeroRuntime
|
|
234
|
+
});
|
|
235
|
+
if (zero) {
|
|
236
|
+
if (zero.isEnforcing) {
|
|
237
|
+
Static.assertZeroConfigDrivers(compilerProject.projectInfo.tamaguiConfig);
|
|
238
|
+
}
|
|
239
|
+
zero.plansRestoredFromCache = false;
|
|
240
|
+
zero.configCSS = compilerProject.projectInfo.tamaguiConfig.getCSS?.() ?? "";
|
|
241
|
+
zero.artifact.clearGraphs();
|
|
242
|
+
zero.bridges.clear();
|
|
243
|
+
zero.violations.length = 0;
|
|
244
|
+
zero.transformed.clear();
|
|
245
|
+
zero.erasedExports.clear();
|
|
246
|
+
this.#zeroEntryGraph = this.#reachableFrom(entryRoots.map(resolvedModuleId));
|
|
247
|
+
}
|
|
248
|
+
for (const id of unplanned) this.#refreshEntry(id);
|
|
249
|
+
await this.#storePlans(unplanned);
|
|
250
|
+
}
|
|
251
|
+
if (zero) {
|
|
252
|
+
Static.writeZeroViolationReport(zero.resolved.outDir, "metro-zero", {
|
|
253
|
+
integration: "metro-web",
|
|
254
|
+
mode: zero.isEnforcing ? "enforce" : "report",
|
|
255
|
+
violations: zero.violations
|
|
256
|
+
});
|
|
257
|
+
if (zero.isEnforcing && zero.violations.length) {
|
|
258
|
+
throw new Error(Static.formatZeroViolations(zero.violations));
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const totalFound = [...this.#entries.values()].reduce((sum, entry) => sum + entry.plan.stats.found, 0);
|
|
262
|
+
if (this.#entries.size > 0 && totalFound === 0) {
|
|
263
|
+
const componentNames = compilerProject.componentModules.map(({ moduleName }) => moduleName);
|
|
264
|
+
const cjsComponentImporters = [...this.#records.values()].filter((record) => record.requireSpecifiers.some((specifier) => componentNames.some((name) => specifier === name || specifier.startsWith(`${name}/`)))).length;
|
|
265
|
+
if (cjsComponentImporters > 0) {
|
|
266
|
+
const diagnostic = metroDiagnostic("metro/no-linked-components", `The Tamagui compiler linked 0 components across ${this.#entries.size} modules even though ${cjsComponentImporters} module(s) reference ${componentNames.join(", ")} through require() calls. Metro compiled modules to CommonJS before the compiler could analyze them, so component imports cannot be linked and nothing will be optimized. Enable experimentalImportSupport in your transformer's getTransformOptions (Expo enables it by default) to restore Tamagui compilation.`);
|
|
267
|
+
diagnostics.push(diagnostic);
|
|
268
|
+
this.#report(diagnostic);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
const generation = await this.#publish(options.platform);
|
|
272
|
+
const moduleIds = [...this.#records.keys()].sort(compareCodeUnits);
|
|
273
|
+
if (this.config.watch !== false && retainsLiveGraph(options)) {
|
|
274
|
+
this.#installWatchers();
|
|
275
|
+
} else if (!retainsLiveGraph(options)) {
|
|
276
|
+
this.#releaseGraph();
|
|
277
|
+
}
|
|
278
|
+
return {
|
|
279
|
+
generation,
|
|
280
|
+
moduleIds,
|
|
281
|
+
diagnostics
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
ensureValidCache(options) {
|
|
285
|
+
return this.#enqueue(() => this.#ensureValidCache(options));
|
|
286
|
+
}
|
|
287
|
+
async #ensureValidCache(options) {
|
|
288
|
+
const diagnostics = [];
|
|
289
|
+
const firstEntry = options.entryFiles[0];
|
|
290
|
+
const importer = firstEntry ? await realpath(resolve(this.config.projectRoot, firstEntry)) : this.config.projectRoot;
|
|
291
|
+
const compilerProject = await this.#loadCompilerProject(options, importer, diagnostics);
|
|
292
|
+
const cache = new MetroCompilerCache(this.cacheRootFor(options.platform));
|
|
293
|
+
const validation = await cache.validate();
|
|
294
|
+
const projectSources = await walkProjectSources(this.config.projectRoot);
|
|
295
|
+
const optionsHash = scanOptionsHash(options, compilerProject.generation, contentHash(JSON.stringify(projectSources)));
|
|
296
|
+
if (validation.valid && validation.generation && validation.optionsHash === optionsHash && await this.#sourcesAreFresh(validation.sourceHashes) && (!retainsLiveGraph(options) && !this.#graph || this.#publishedGeneration && this.#scanOptionsHash === optionsHash) && await this.#rehydrateZeroCSS(cache, validation.generation)) {
|
|
297
|
+
this.#publishedGeneration = validation.generation;
|
|
298
|
+
this.#scanOptions = options;
|
|
299
|
+
this.#scanOptionsHash = optionsHash;
|
|
300
|
+
this.#projectGeneration = compilerProject.generation;
|
|
301
|
+
return {
|
|
302
|
+
generation: validation.generation,
|
|
303
|
+
moduleIds: validation.moduleIds,
|
|
304
|
+
diagnostics
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
for (const diagnostic of validation.diagnostics) this.#report(diagnostic);
|
|
308
|
+
await cache.discardManifest();
|
|
309
|
+
return await this.#scan(options, compilerProject, projectSources);
|
|
310
|
+
}
|
|
311
|
+
async updateFile(path) {
|
|
312
|
+
let result = {
|
|
313
|
+
changed: false,
|
|
314
|
+
affectedIds: [],
|
|
315
|
+
generation: null
|
|
316
|
+
};
|
|
317
|
+
return this.#enqueue(async () => {
|
|
318
|
+
const graph = this.#graph;
|
|
319
|
+
const options = this.#scanOptions;
|
|
320
|
+
if (!graph || !options) return result;
|
|
321
|
+
let record;
|
|
322
|
+
const diagnostics = [];
|
|
323
|
+
try {
|
|
324
|
+
record = await this.#compileRecord(path, options, diagnostics);
|
|
325
|
+
} catch (error) {
|
|
326
|
+
if (error.code === "ENOENT") {
|
|
327
|
+
const id = resolvedModuleId(resolve(path));
|
|
328
|
+
const invalidation2 = graph.removeModule(id);
|
|
329
|
+
this.#watchers.get(id)?.close();
|
|
330
|
+
this.#watchers.delete(id);
|
|
331
|
+
this.#records.delete(id);
|
|
332
|
+
this.#entries.delete(id);
|
|
333
|
+
for (const affected of invalidation2.invalidatedIds) {
|
|
334
|
+
if (affected !== id) this.#refreshEntry(affected);
|
|
335
|
+
}
|
|
336
|
+
const generation2 = await this.#publish(options.platform);
|
|
337
|
+
result = {
|
|
338
|
+
changed: invalidation2.changed,
|
|
339
|
+
affectedIds: invalidation2.invalidatedIds,
|
|
340
|
+
generation: generation2
|
|
341
|
+
};
|
|
342
|
+
return result;
|
|
343
|
+
}
|
|
344
|
+
const diagnostic = metroDiagnostic("metro/transform-failed", `Failed to update ${path}: ${error instanceof Error ? error.message : String(error)}`, { moduleId: path });
|
|
345
|
+
this.#report(diagnostic);
|
|
346
|
+
return result;
|
|
347
|
+
}
|
|
348
|
+
for (const dependency of record.input.imports) {
|
|
349
|
+
if (dependency.external || !isCompilerSourceFile(dependency.resolvedId) || this.#records.has(dependency.resolvedId)) {
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
await this.#addDependency(dependency.resolvedId, options, diagnostics);
|
|
353
|
+
}
|
|
354
|
+
this.#records.set(record.input.id, record);
|
|
355
|
+
const invalidation = graph.updateModule(record.input);
|
|
356
|
+
for (const affected of invalidation.invalidatedIds) this.#refreshEntry(affected);
|
|
357
|
+
const generation = invalidation.changed ? await this.#publish(options.platform) : null;
|
|
358
|
+
result = {
|
|
359
|
+
changed: invalidation.changed,
|
|
360
|
+
affectedIds: invalidation.invalidatedIds,
|
|
361
|
+
generation
|
|
362
|
+
};
|
|
363
|
+
if (this.config.watch !== false && retainsLiveGraph(options)) {
|
|
364
|
+
this.#watchModule(record.input.id);
|
|
365
|
+
}
|
|
366
|
+
return result;
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
/** A published plan only applies while every recorded module source is unchanged. */
|
|
370
|
+
async #sourcesAreFresh(sourceHashes) {
|
|
371
|
+
const checks = Object.entries(sourceHashes).map(async ([moduleId, sourceHash]) => {
|
|
372
|
+
try {
|
|
373
|
+
return contentHash(await readFile(moduleId, "utf8")) === sourceHash;
|
|
374
|
+
} catch {
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
return (await Promise.all(checks)).every(Boolean);
|
|
379
|
+
}
|
|
380
|
+
#enqueue(operation) {
|
|
381
|
+
const queued = this.#operationQueue.then(operation);
|
|
382
|
+
this.#operationQueue = queued.then(() => void 0, () => void 0);
|
|
383
|
+
return queued;
|
|
384
|
+
}
|
|
385
|
+
close() {
|
|
386
|
+
return this.#enqueue(async () => {
|
|
387
|
+
this.#releaseGraph();
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
#releaseGraph() {
|
|
391
|
+
for (const watcher of this.#watchers.values()) watcher.close();
|
|
392
|
+
this.#watchers.clear();
|
|
393
|
+
this.#entries.clear();
|
|
394
|
+
this.#records.clear();
|
|
395
|
+
this.#planKeys.clear();
|
|
396
|
+
this.#graph = null;
|
|
397
|
+
this.#host = null;
|
|
398
|
+
this.#projectGeneration = null;
|
|
399
|
+
}
|
|
400
|
+
async #loadCompilerProject(options, importer, diagnostics) {
|
|
401
|
+
const target = compilerTarget(options.platform);
|
|
402
|
+
if (this.config.loadCompilerProject) {
|
|
403
|
+
return await this.config.loadCompilerProject(target, options.platform);
|
|
404
|
+
}
|
|
405
|
+
return Static.loadCompilerProject({
|
|
406
|
+
root: this.config.projectRoot,
|
|
407
|
+
target,
|
|
408
|
+
options: this.config.tamaguiOptions ?? {},
|
|
409
|
+
hostVersions: compilerImplementationVersions,
|
|
410
|
+
missingProjectMessage: "Unable to load the Tamagui project for Metro compilation",
|
|
411
|
+
generation: (projectInfo, componentModules, normalizedOptions) => {
|
|
412
|
+
return contentHash(JSON.stringify({
|
|
413
|
+
cacheVersion: METRO_COMPILER_CACHE_VERSION,
|
|
414
|
+
compilerImplementationVersions,
|
|
415
|
+
componentModules,
|
|
416
|
+
configCss: projectInfo.tamaguiConfig?.getCSS?.() ?? "",
|
|
417
|
+
disablePartialExtraction: !!normalizedOptions.disablePartialExtraction,
|
|
418
|
+
experimentalNativeFastPath: target === "native" && normalizedOptions.experimental?.nativeFastPath === true,
|
|
419
|
+
target,
|
|
420
|
+
zeroRuntime: !!this.config.zero
|
|
421
|
+
}));
|
|
422
|
+
},
|
|
423
|
+
resolveComponents: async (moduleNames) => {
|
|
424
|
+
const componentModules = [];
|
|
425
|
+
for (const moduleName of moduleNames) {
|
|
426
|
+
try {
|
|
427
|
+
const resolution = this.#resolver.resolve(importer, {
|
|
428
|
+
specifier: moduleName,
|
|
429
|
+
isESMImport: true
|
|
430
|
+
}, options.platform);
|
|
431
|
+
if (!resolution) continue;
|
|
432
|
+
componentModules.push({
|
|
433
|
+
moduleName,
|
|
434
|
+
id: resolution.resolvedId
|
|
435
|
+
});
|
|
436
|
+
} catch (error) {
|
|
437
|
+
const diagnostic = metroDiagnostic("metro/resolve-failed", `Failed to resolve compiler component ${moduleName}: ${error instanceof Error ? error.message : String(error)}`, {
|
|
438
|
+
moduleId: importer,
|
|
439
|
+
dependency: moduleName
|
|
440
|
+
});
|
|
441
|
+
diagnostics.push(diagnostic);
|
|
442
|
+
this.#report(diagnostic);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return componentModules;
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
async #compileRecord(rawPath, options, diagnostics) {
|
|
450
|
+
const path = await realpath(resolve(rawPath));
|
|
451
|
+
const source = await readFile(path, "utf8");
|
|
452
|
+
const sourceHash = contentHash(source);
|
|
453
|
+
const id = resolvedModuleId(path);
|
|
454
|
+
const cache = this.#recordCache;
|
|
455
|
+
const identity = this.#recordCacheIdentity;
|
|
456
|
+
const key = cache && identity ? contentHash(`${identity}\0${sourceHash}`) : null;
|
|
457
|
+
if (cache && key) {
|
|
458
|
+
const cached = await cache.read(key, (value) => {
|
|
459
|
+
const entry = value;
|
|
460
|
+
return entry?.schemaVersion === 1 && entry.sourceHash === sourceHash && Array.isArray(entry.imports) && Array.isArray(entry.requireSpecifiers) && Array.isArray(entry.diagnostics) ? entry : null;
|
|
461
|
+
});
|
|
462
|
+
if (cached) {
|
|
463
|
+
for (const diagnostic of cached.diagnostics) {
|
|
464
|
+
diagnostics.push(diagnostic);
|
|
465
|
+
this.#report(diagnostic);
|
|
466
|
+
}
|
|
467
|
+
return {
|
|
468
|
+
input: {
|
|
469
|
+
id,
|
|
470
|
+
source,
|
|
471
|
+
imports: cached.imports
|
|
472
|
+
},
|
|
473
|
+
sourceHash,
|
|
474
|
+
requireSpecifiers: cached.requireSpecifiers
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
const args = this.#babelArgs(path, source, options);
|
|
479
|
+
const compiled = await compileWithUserBabel(this.config.originalBabelTransformerPath, args);
|
|
480
|
+
const imports = [];
|
|
481
|
+
const requireSpecifiers = [];
|
|
482
|
+
const recordDiagnostics = [];
|
|
483
|
+
for (const dependency of moduleSpecifiersFromAst(compiled.result.ast)) {
|
|
484
|
+
if (!dependency.isESMImport) requireSpecifiers.push(dependency.specifier);
|
|
485
|
+
try {
|
|
486
|
+
const resolution = this.#resolver.resolve(path, dependency, options.platform);
|
|
487
|
+
if (!resolution) continue;
|
|
488
|
+
imports.push({
|
|
489
|
+
specifier: resolution.specifier,
|
|
490
|
+
resolvedId: resolvedModuleId(resolution.resolvedId),
|
|
491
|
+
external: resolution.external
|
|
492
|
+
});
|
|
493
|
+
} catch (error) {
|
|
494
|
+
recordDiagnostics.push(metroDiagnostic("metro/resolve-failed", `Failed to resolve ${dependency.specifier} from ${path}: ${error instanceof Error ? error.message : String(error)}`, {
|
|
495
|
+
moduleId: path,
|
|
496
|
+
dependency: dependency.specifier
|
|
497
|
+
}));
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
for (const diagnostic of recordDiagnostics) {
|
|
501
|
+
diagnostics.push(diagnostic);
|
|
502
|
+
this.#report(diagnostic);
|
|
503
|
+
}
|
|
504
|
+
if (cache && key) {
|
|
505
|
+
await cache.write(key, {
|
|
506
|
+
schemaVersion: 1,
|
|
507
|
+
sourceHash,
|
|
508
|
+
imports,
|
|
509
|
+
requireSpecifiers,
|
|
510
|
+
diagnostics: recordDiagnostics
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
return {
|
|
514
|
+
input: {
|
|
515
|
+
id,
|
|
516
|
+
source,
|
|
517
|
+
imports
|
|
518
|
+
},
|
|
519
|
+
sourceHash,
|
|
520
|
+
requireSpecifiers
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
#babelOptions(options) {
|
|
524
|
+
const transformer = this.config.transformer ?? {};
|
|
525
|
+
return {
|
|
526
|
+
...options.transform,
|
|
527
|
+
dev: options.dev,
|
|
528
|
+
hot: options.hot,
|
|
529
|
+
platform: options.platform,
|
|
530
|
+
projectRoot: this.config.projectRoot,
|
|
531
|
+
enableBabelRCLookup: transformer.enableBabelRCLookup ?? true,
|
|
532
|
+
enableBabelRuntime: transformer.enableBabelRuntime ?? true,
|
|
533
|
+
hermesParser: transformer.hermesParser ?? false,
|
|
534
|
+
publicPath: transformer.publicPath ?? "/assets"
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
#babelArgs(filename, src, options) {
|
|
538
|
+
return {
|
|
539
|
+
filename,
|
|
540
|
+
src,
|
|
541
|
+
plugins: [],
|
|
542
|
+
options: this.#babelOptions(options)
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
async #addDependency(id, options, diagnostics, visiting = /* @__PURE__ */ new Set()) {
|
|
546
|
+
if (this.#records.has(id) || visiting.has(id)) return;
|
|
547
|
+
visiting.add(id);
|
|
548
|
+
try {
|
|
549
|
+
const record = await this.#compileRecord(id, options, diagnostics);
|
|
550
|
+
for (const dependency of record.input.imports) {
|
|
551
|
+
if (!dependency.external && isCompilerSourceFile(dependency.resolvedId)) {
|
|
552
|
+
await this.#addDependency(dependency.resolvedId, options, diagnostics, visiting);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
this.#records.set(id, record);
|
|
556
|
+
const invalidation = this.#graph?.updateModule(record.input);
|
|
557
|
+
for (const affected of invalidation?.invalidatedIds ?? [id]) {
|
|
558
|
+
this.#refreshEntry(affected);
|
|
559
|
+
}
|
|
560
|
+
if (this.config.watch !== false && this.#scanOptions && retainsLiveGraph(this.#scanOptions)) {
|
|
561
|
+
this.#watchModule(id);
|
|
562
|
+
}
|
|
563
|
+
} finally {
|
|
564
|
+
visiting.delete(id);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
#refreshEntry(id) {
|
|
568
|
+
const graph = this.#graph;
|
|
569
|
+
const host = this.#host;
|
|
570
|
+
const record = this.#records.get(id);
|
|
571
|
+
if (!graph || !host || !record || !this.#scanOptions || !this.#projectGeneration) return;
|
|
572
|
+
const target = compilerTarget(this.#scanOptions.platform);
|
|
573
|
+
const plan = lowerModule({
|
|
574
|
+
module: materializeModule(graph, id),
|
|
575
|
+
source: record.input.source,
|
|
576
|
+
target,
|
|
577
|
+
host,
|
|
578
|
+
options: { projectGeneration: this.#projectGeneration }
|
|
579
|
+
});
|
|
580
|
+
const zeroPlan = this.#zeroPlanFor(id, record.input.source, plan);
|
|
581
|
+
this.#entries.set(id, this.#entryFor(id, record, zeroPlan ?? plan));
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* One plan becomes one cache entry the same way whether the plan was just
|
|
585
|
+
* lowered or read back off disk, so a restored build reports exactly the
|
|
586
|
+
* diagnostics a fresh one did.
|
|
587
|
+
*/
|
|
588
|
+
#entryFor(id, record, plan) {
|
|
589
|
+
return {
|
|
590
|
+
schemaVersion: METRO_COMPILER_CACHE_VERSION,
|
|
591
|
+
moduleId: id,
|
|
592
|
+
sourceHash: record.sourceHash,
|
|
593
|
+
plan,
|
|
594
|
+
diagnostics: plan.diagnostics.map(({ code, message, dependencyId, span, component }) => {
|
|
595
|
+
const { line, column } = Static.offsetToLineColumn(record.input.source, span.start);
|
|
596
|
+
return metroDiagnostic(code.startsWith("linked/") ? "metro/resolve-failed" : "metro/transform-failed", message, {
|
|
597
|
+
moduleId: id,
|
|
598
|
+
dependency: dependencyId,
|
|
599
|
+
span,
|
|
600
|
+
line,
|
|
601
|
+
column,
|
|
602
|
+
component
|
|
603
|
+
});
|
|
604
|
+
})
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Both per-file caches for this scan. A project with no content stamp gets
|
|
609
|
+
* neither: a stamp that cannot see a config change would serve styles built
|
|
610
|
+
* against the old config, so the answer is no cache rather than a partial one.
|
|
611
|
+
*
|
|
612
|
+
* Zero builds opt out of the plan cache because a zero plan is produced
|
|
613
|
+
* alongside side effects that do not travel in the plan - the CSS artifact,
|
|
614
|
+
* the bridge manifest, the violation list - so replaying one module's plan
|
|
615
|
+
* without them would emit an artifact missing its rules.
|
|
616
|
+
*/
|
|
617
|
+
#installCaches(options, project, projectSourcesHash) {
|
|
618
|
+
const platform = options.platform ?? "default";
|
|
619
|
+
const root = defaultPlanCacheRoot(this.config.projectRoot, platform);
|
|
620
|
+
this.#recordCache = new JsonFileCache(join(root, "records"), 1);
|
|
621
|
+
this.#recordCacheIdentity = contentHash(stableStringify({
|
|
622
|
+
schema: 1,
|
|
623
|
+
resolver: this.#resolver.version,
|
|
624
|
+
babel: userBabelCacheKey(this.config.originalBabelTransformerPath),
|
|
625
|
+
projectSourcesHash,
|
|
626
|
+
platform,
|
|
627
|
+
transform: this.#babelOptions(options)
|
|
628
|
+
}));
|
|
629
|
+
const stamp = project.cacheStamp;
|
|
630
|
+
const usePlanCache = typeof stamp === "string" && stamp !== "" && !this.config.zero;
|
|
631
|
+
this.#planCache = usePlanCache ? new ModulePlanCache(join(root, "plans")) : null;
|
|
632
|
+
this.#planCacheStamp = usePlanCache ? stamp : null;
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Fills `#entries` from disk for every module whose whole compile input is
|
|
636
|
+
* unchanged, and returns the ids that still have to be compiled. This is the
|
|
637
|
+
* per-file property: one edited module leaves every other module's entry
|
|
638
|
+
* valid, where the plan manifest would have discarded all of them.
|
|
639
|
+
*/
|
|
640
|
+
async #restorePlans(options) {
|
|
641
|
+
this.#planKeys.clear();
|
|
642
|
+
const cache = this.#planCache;
|
|
643
|
+
const stamp = this.#planCacheStamp;
|
|
644
|
+
if (!cache || !stamp) return [...this.#records.keys()].sort(compareCodeUnits);
|
|
645
|
+
const target = compilerTarget(options.platform);
|
|
646
|
+
const identity = {
|
|
647
|
+
stamp,
|
|
648
|
+
target,
|
|
649
|
+
structuralPassHash: `${target}-noop-v1`
|
|
650
|
+
};
|
|
651
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
652
|
+
const lookup = (id) => {
|
|
653
|
+
let node = nodes.get(id);
|
|
654
|
+
if (node === void 0) {
|
|
655
|
+
const record = this.#records.get(id);
|
|
656
|
+
node = record ? moduleClosureNode(record.input) : null;
|
|
657
|
+
nodes.set(id, node);
|
|
658
|
+
}
|
|
659
|
+
return node;
|
|
660
|
+
};
|
|
661
|
+
const memo = /* @__PURE__ */ new Map();
|
|
662
|
+
const unplanned = [];
|
|
663
|
+
for (const id of [...this.#records.keys()].sort(compareCodeUnits)) {
|
|
664
|
+
const record = this.#records.get(id);
|
|
665
|
+
const digest = moduleClosureDigest(id, lookup, memo);
|
|
666
|
+
const key = digest && planCacheKey(identity, id, digest);
|
|
667
|
+
const entry = key && digest ? await cache.read(key, id, digest) : null;
|
|
668
|
+
if (entry) {
|
|
669
|
+
this.#entries.set(id, this.#entryFor(id, record, entry.plan));
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
if (key && digest) this.#planKeys.set(id, {
|
|
673
|
+
key,
|
|
674
|
+
digest
|
|
675
|
+
});
|
|
676
|
+
unplanned.push(id);
|
|
677
|
+
}
|
|
678
|
+
return unplanned;
|
|
679
|
+
}
|
|
680
|
+
async #storePlans(ids) {
|
|
681
|
+
const cache = this.#planCache;
|
|
682
|
+
if (!cache) return;
|
|
683
|
+
const pending = ids.flatMap((id) => {
|
|
684
|
+
const entry = this.#entries.get(id);
|
|
685
|
+
const key = this.#planKeys.get(id);
|
|
686
|
+
return entry && key ? [{
|
|
687
|
+
id,
|
|
688
|
+
entry,
|
|
689
|
+
key
|
|
690
|
+
}] : [];
|
|
691
|
+
});
|
|
692
|
+
for (let index = 0; index < pending.length; index += 32) {
|
|
693
|
+
await Promise.all(pending.slice(index, index + 32).map(({ id, entry, key }) => cache.write(key.key, {
|
|
694
|
+
schemaVersion: PLAN_CACHE_SCHEMA_VERSION,
|
|
695
|
+
moduleId: id,
|
|
696
|
+
closureDigest: key.digest,
|
|
697
|
+
plan: entry.plan
|
|
698
|
+
})));
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
/** Modules reachable from the bundle's entry, over the frontend's own graph. */
|
|
702
|
+
#reachableFrom(roots) {
|
|
703
|
+
const reached = /* @__PURE__ */ new Set();
|
|
704
|
+
const queue = [...roots];
|
|
705
|
+
while (queue.length) {
|
|
706
|
+
const id = queue.pop();
|
|
707
|
+
if (reached.has(id)) continue;
|
|
708
|
+
reached.add(id);
|
|
709
|
+
for (const dependency of this.#records.get(id)?.input.imports ?? []) {
|
|
710
|
+
if (!dependency.external) queue.push(dependency.resolvedId);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return reached;
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* The zero transform for one module, returning a plan whose edits also carry
|
|
717
|
+
* the static Theme lowering, the island bridge, and reference erasure.
|
|
718
|
+
*/
|
|
719
|
+
#zeroPlanFor(id, source, plan) {
|
|
720
|
+
const zero = this.config.zero;
|
|
721
|
+
const config = this.#tamaguiConfig;
|
|
722
|
+
if (!zero || !config) return null;
|
|
723
|
+
if (zero.islandBuild) {
|
|
724
|
+
zero.artifact.setIslandModuleCSS(zero.islandBuild, id, plan.css);
|
|
725
|
+
return null;
|
|
726
|
+
}
|
|
727
|
+
if (this.#zeroEntryGraph && !this.#zeroEntryGraph.has(id)) return null;
|
|
728
|
+
const relativePath = relative(this.config.projectRoot, id);
|
|
729
|
+
if (relativePath === "" || relativePath.startsWith("..") || relativePath.split(/[\\/]/).includes("node_modules")) {
|
|
730
|
+
return null;
|
|
731
|
+
}
|
|
732
|
+
const result = Static.transformZeroModule({
|
|
733
|
+
mode: zero.isEnforcing ? "enforce" : "report",
|
|
734
|
+
id,
|
|
735
|
+
root: this.config.projectRoot,
|
|
736
|
+
source,
|
|
737
|
+
plan,
|
|
738
|
+
config,
|
|
739
|
+
isTamaguiSpecifier: (specifier) => specifier === "tamagui" || specifier.startsWith("@tamagui/"),
|
|
740
|
+
resolveIslandLoader: (specifier) => {
|
|
741
|
+
const islandId = zero.loaderIds.get(zeroModuleKey(resolve(id, "..", specifier)));
|
|
742
|
+
return islandId ? { islandId } : null;
|
|
743
|
+
},
|
|
744
|
+
resolveIslandModule: (specifier) => zero.islandModuleIds.get(zeroModuleKey(resolve(id, "..", specifier))) ?? null
|
|
745
|
+
});
|
|
746
|
+
zero.transformed.add(id);
|
|
747
|
+
if (result.erased.exports.length) {
|
|
748
|
+
zero.erasedExports.set(id, result.erased.exports);
|
|
749
|
+
}
|
|
750
|
+
for (const violation of result.violations) {
|
|
751
|
+
const { line, column } = Static.offsetToLineColumn(source, violation.span.start);
|
|
752
|
+
zero.violations.push({
|
|
753
|
+
file: relativePath,
|
|
754
|
+
line,
|
|
755
|
+
column,
|
|
756
|
+
rule: violation.rule,
|
|
757
|
+
code: violation.code,
|
|
758
|
+
component: violation.component,
|
|
759
|
+
message: violation.message
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
if (result.violations.length || !zero.isEnforcing) return null;
|
|
763
|
+
Static.mergeIslandBridges(zero.bridges, result.bridges);
|
|
764
|
+
for (const [identifier, rules] of result.bridgeCSS) {
|
|
765
|
+
zero.artifact.setBridgeRules(identifier, rules);
|
|
766
|
+
}
|
|
767
|
+
zero.artifact.setZeroModuleCSS(id, plan.css);
|
|
768
|
+
return {
|
|
769
|
+
...plan,
|
|
770
|
+
edits: [...plan.edits, ...result.edits]
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
async #publish(platform) {
|
|
774
|
+
const cache = new MetroCompilerCache(this.cacheRootFor(platform));
|
|
775
|
+
const generation = await cache.publish(platform, [...this.#entries.values()], this.#scanOptionsHash ?? "");
|
|
776
|
+
const zero = this.config.zero;
|
|
777
|
+
if (zero && !zero.islandBuild) {
|
|
778
|
+
await cache.publishZeroCSS({
|
|
779
|
+
schemaVersion: METRO_COMPILER_CACHE_VERSION,
|
|
780
|
+
generation,
|
|
781
|
+
configCSS: zero.configCSS,
|
|
782
|
+
zeroModuleCSS: Object.fromEntries(zero.artifact.zeroModuleEntries()),
|
|
783
|
+
bridgeCSS: Object.fromEntries(zero.artifact.bridgeEntries()),
|
|
784
|
+
bridges: Object.fromEntries(zero.bridges)
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
this.#publishedGeneration = generation;
|
|
788
|
+
return generation;
|
|
789
|
+
}
|
|
790
|
+
/**
|
|
791
|
+
* Restores the zero build's CSS side effects from the sidecar published with
|
|
792
|
+
* this plan generation. Returns false when there is nothing trustworthy to
|
|
793
|
+
* restore, which sends the caller to a full scan.
|
|
794
|
+
*/
|
|
795
|
+
async #rehydrateZeroCSS(cache, generation) {
|
|
796
|
+
const zero = this.config.zero;
|
|
797
|
+
if (!zero || zero.islandBuild) return true;
|
|
798
|
+
const sidecar = await cache.readZeroCSS(generation);
|
|
799
|
+
if (!sidecar) return false;
|
|
800
|
+
zero.artifact.clearGraphs();
|
|
801
|
+
zero.bridges.clear();
|
|
802
|
+
zero.violations.length = 0;
|
|
803
|
+
zero.configCSS = sidecar.configCSS;
|
|
804
|
+
for (const [moduleId, css] of Object.entries(sidecar.zeroModuleCSS)) {
|
|
805
|
+
zero.artifact.setZeroModuleCSS(moduleId, css);
|
|
806
|
+
}
|
|
807
|
+
for (const [bridgeId, css] of Object.entries(sidecar.bridgeCSS)) {
|
|
808
|
+
zero.artifact.setBridgeRules(bridgeId, css);
|
|
809
|
+
}
|
|
810
|
+
for (const [islandId, bridges] of Object.entries(sidecar.bridges)) {
|
|
811
|
+
zero.bridges.set(islandId, bridges);
|
|
812
|
+
}
|
|
813
|
+
zero.plansRestoredFromCache = true;
|
|
814
|
+
return true;
|
|
815
|
+
}
|
|
816
|
+
#installWatchers() {
|
|
817
|
+
for (const id of this.#records.keys()) this.#watchModule(id);
|
|
818
|
+
}
|
|
819
|
+
#watchModule(id) {
|
|
820
|
+
if (this.#watchers.has(id)) return;
|
|
821
|
+
try {
|
|
822
|
+
const watcher = watch(id, { persistent: false }, () => {
|
|
823
|
+
void this.updateFile(id);
|
|
824
|
+
});
|
|
825
|
+
watcher.unref();
|
|
826
|
+
this.#watchers.set(id, watcher);
|
|
827
|
+
} catch {}
|
|
828
|
+
}
|
|
829
|
+
#report(diagnostic) {
|
|
830
|
+
this.config.reportDiagnostic?.(diagnostic);
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
function describeMetroCompilerRoot(projectRoot, moduleId) {
|
|
834
|
+
const path = relative(projectRoot, moduleId);
|
|
835
|
+
return path.startsWith("..") ? basename(moduleId) : path;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
export { METRO_RECORD_CACHE_VERSION, MetroCompilerFrontend, describeMetroCompilerRoot };
|
|
839
|
+
//# sourceMappingURL=frontend.mjs.map
|