vitest-native 0.13.0 → 0.14.0-rc.2
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/CHANGELOG.md +339 -0
- package/README.md +138 -48
- package/dist/cli.cjs +70 -5
- package/dist/cli.d.cts +2 -3
- package/dist/cli.d.mts +2 -3
- package/dist/cli.mjs +70 -5
- package/dist/{ecosystem-Duw40-mO.mjs → ecosystem-DFbU_MPe.mjs} +389 -18
- package/dist/{ecosystem-CEIF_-jq.cjs → ecosystem-DI1AhK_k.cjs} +408 -25
- package/dist/errors.d.mts +12 -0
- package/dist/helpers.cjs +6 -3
- package/dist/helpers.d.cts +8 -9
- package/dist/helpers.d.mts +8 -9
- package/dist/helpers.mjs +6 -3
- package/dist/{index-CPGq6p95.d.cts → index-BxzW2D7K.d.cts} +33 -9
- package/dist/{index-CmETArCM.d.mts → index-CEbwa4QU.d.mts} +33 -9
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +537 -118
- package/dist/jest-compat/aliases.d.mts +5 -0
- package/dist/jest-compat/aliases.mjs +65 -0
- package/dist/jest-compat/caller.mjs +53 -0
- package/dist/jest-compat/setup.mjs +72 -36
- package/dist/jest-compat.cjs +1 -3
- package/dist/jest-compat.d.cts +4 -5
- package/dist/jest-compat.d.mts +4 -5
- package/dist/jest-compat.mjs +1 -1
- package/dist/matchers.d.cts +9 -9
- package/dist/matchers.d.mts +9 -9
- package/dist/memory-6EPHrJA1.cjs +144 -0
- package/dist/memory-FYbP0_gg.mjs +126 -0
- package/dist/native/boundary.mjs +13 -2
- package/dist/native/bounded-process.mjs +165 -0
- package/dist/native/export-condition-recovery-preload.mjs +5 -0
- package/dist/native/export-condition-recovery.mjs +195 -0
- package/dist/native/globals.mjs +133 -5
- package/dist/native/hooks.mjs +131 -71
- package/dist/native/loader.mjs +41 -28
- package/dist/native/match.d.mts +3 -0
- package/dist/native/match.mjs +38 -2
- package/dist/native/metro-profile-compiler.mjs +234 -0
- package/dist/native/metro-profile.mjs +115 -0
- package/dist/native/module-reset.mjs +111 -6
- package/dist/native/ownership.d.mts +105 -0
- package/dist/native/ownership.mjs +474 -0
- package/dist/native/registry-compiler.mjs +43 -0
- package/dist/native/registry-process.mjs +124 -0
- package/dist/native/registry.mjs +139 -32
- package/dist/native/reset.mjs +305 -135
- package/dist/native/resolve.d.mts +20 -3
- package/dist/native/resolve.mjs +32 -12
- package/dist/native/rntl-hooks.mjs +76 -0
- package/dist/native/setup.mjs +81 -32
- package/dist/native/state-manifest.d.mts +22 -0
- package/dist/native/state-manifest.mjs +136 -0
- package/dist/native/transform.mjs +29 -8
- package/dist/native/worker.mjs +52 -4
- package/dist/pool-B4yesGhI.cjs +121 -0
- package/dist/pool-DjpOjOf3.mjs +119 -0
- package/dist/{presets-CoNf4ByH.cjs → presets-BYHmi7-W.cjs} +117 -18
- package/dist/{presets-DGProTaA.mjs → presets-DC-eM5IA.mjs} +117 -18
- package/dist/presets.d.mts +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/rntl-matchers.d.ts +58 -12
- package/dist/serializer.cjs +4 -2
- package/dist/serializer.d.cts +3 -4
- package/dist/serializer.d.mts +3 -4
- package/dist/serializer.mjs +4 -2
- package/dist/setup.mjs +61 -50
- package/dist/worker-vitest-BGPo0ek6.mjs +49 -0
- package/dist/worker-vitest-BfiQB8ND.cjs +62 -0
- package/docs/hot-default-design.md +175 -88
- package/docs/migrating-from-jest.md +12 -5
- package/docs/migrating-from-vitest-react-native.md +4 -4
- package/docs/release-readiness.md +39 -13
- package/docs/versioning.md +19 -18
- package/package.json +42 -31
- package/dist/pool-BU-1ecIF.cjs +0 -122
- package/dist/pool-Bcxn9a3I.mjs +0 -118
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Project-scoped module ownership policy for the native engine.
|
|
3
|
+
*
|
|
4
|
+
* This module does not resolve or load anything. It turns already-established
|
|
5
|
+
* project facts into the patterns and predicates consumed by Vitest config, the
|
|
6
|
+
* CommonJS hook, the ESM loader, hot reset, and diagnostics. Keeping it pure lets
|
|
7
|
+
* the Vite main process and every worker reconstruct the same decision.
|
|
8
|
+
*/
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import {
|
|
11
|
+
NODE_MODULES_PATH,
|
|
12
|
+
REACT_NATIVE_PATH,
|
|
13
|
+
buildPkgMatcher,
|
|
14
|
+
containsPath,
|
|
15
|
+
installedPackageDirOf,
|
|
16
|
+
packageDirOf,
|
|
17
|
+
packagePatterns,
|
|
18
|
+
} from "./match.mjs";
|
|
19
|
+
|
|
20
|
+
const OUTSIDE_NODE_MODULES = String.raw`^(?!.*[\\/]node_modules[\\/])`;
|
|
21
|
+
|
|
22
|
+
/** Files Vitest may execute as entries. They always belong to Vite. */
|
|
23
|
+
export const TEST_ENTRY_PATTERNS = Object.freeze([
|
|
24
|
+
new RegExp(String.raw`${OUTSIDE_NODE_MODULES}.*\.(?:test|spec)\.[cm]?[jt]sx?$`),
|
|
25
|
+
new RegExp(String.raw`${OUTSIDE_NODE_MODULES}.*[\\/]__tests__[\\/].*\.[cm]?[jt]sx?$`),
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Identity-sensitive runtime packages whose CJS entries cannot be dropped while
|
|
30
|
+
* their ESM entries remain resident. Shared by ESM generation and hot CJS reset.
|
|
31
|
+
*/
|
|
32
|
+
export const RUNTIME_RESIDENT_PACKAGES = Object.freeze([
|
|
33
|
+
"react",
|
|
34
|
+
"react-is",
|
|
35
|
+
"react-dom",
|
|
36
|
+
"scheduler",
|
|
37
|
+
"react-reconciler",
|
|
38
|
+
"react-test-renderer",
|
|
39
|
+
"test-renderer",
|
|
40
|
+
"@testing-library/react-native",
|
|
41
|
+
]);
|
|
42
|
+
|
|
43
|
+
export const RUNTIME_RESIDENT_PATH =
|
|
44
|
+
/[\\/]node_modules[\\/](react|react-is|react-dom|scheduler|react-reconciler|react-test-renderer|test-renderer|@testing-library[\\/]react-native)[\\/]/;
|
|
45
|
+
|
|
46
|
+
/** The test runner/runtime must never receive per-file ESM generation stamps. */
|
|
47
|
+
export const TEST_RUNTIME_RESIDENT_PACKAGES = Object.freeze([
|
|
48
|
+
"vitest",
|
|
49
|
+
"@vitest/*",
|
|
50
|
+
"chai",
|
|
51
|
+
"vitest-native",
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
export const TEST_RUNTIME_RESIDENT_PATH =
|
|
55
|
+
/[\\/]node_modules[\\/](vitest|@vitest[\\/][^\\/]+|chai|vitest-native)[\\/]/;
|
|
56
|
+
|
|
57
|
+
const normalizeFile = (file) => file.replace(/\\/g, "/");
|
|
58
|
+
// Vite module ids may carry `?query` / `#fragment` suffixes. Ownership is a
|
|
59
|
+
// property of the resolved file, not a loader query; leaving the suffix attached
|
|
60
|
+
// makes the anchored test-entry patterns miss an otherwise ordinary test file.
|
|
61
|
+
const cleanRawFileId = (file) => file.replace(/[?#].*$/, "");
|
|
62
|
+
const cleanFileId = (file) => normalizeFile(cleanRawFileId(file));
|
|
63
|
+
const unique = (values) => [...new Set((values || []).filter(Boolean))];
|
|
64
|
+
const escapeRe = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
65
|
+
|
|
66
|
+
export function isRuntimeResidentFile(file) {
|
|
67
|
+
return RUNTIME_RESIDENT_PATH.test(normalizeFile(file));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function isTestRuntimeResidentFile(file) {
|
|
71
|
+
return TEST_RUNTIME_RESIDENT_PATH.test(normalizeFile(file));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function decision(owner, format, transform, reset, reasons, evidence) {
|
|
75
|
+
return Object.freeze({
|
|
76
|
+
owner,
|
|
77
|
+
format,
|
|
78
|
+
transform,
|
|
79
|
+
reset,
|
|
80
|
+
reason: reasons[0],
|
|
81
|
+
reasons: Object.freeze(reasons),
|
|
82
|
+
evidence: Object.freeze(evidence),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Build one immutable policy for a Vitest project.
|
|
88
|
+
*
|
|
89
|
+
* `explicitTransforms` is the public `transform` include list.
|
|
90
|
+
* `ecosystemPackages` is the detected RN dependency closure.
|
|
91
|
+
* `runtimeTransforms` is for workers reconstructing the already-combined list from
|
|
92
|
+
* serialized setup data; config-time callers normally leave it empty.
|
|
93
|
+
*/
|
|
94
|
+
export function createNativeOwnershipPolicy({
|
|
95
|
+
projectRoot = process.cwd(),
|
|
96
|
+
explicitTransforms = [],
|
|
97
|
+
ecosystemPackages = [],
|
|
98
|
+
runtimeTransforms = [],
|
|
99
|
+
runtimeTransformAugmentations = [],
|
|
100
|
+
projectDirs = [],
|
|
101
|
+
reactNativeRoots = [],
|
|
102
|
+
serverDepsInlineAll = false,
|
|
103
|
+
} = {}) {
|
|
104
|
+
const enforcement = serverDepsInlineAll ? "overridden-by-inline-all" : "enforced";
|
|
105
|
+
const root = path.resolve(projectRoot);
|
|
106
|
+
const explicit = unique(explicitTransforms);
|
|
107
|
+
const explicitSet = new Set(explicit);
|
|
108
|
+
const ecosystem = unique(ecosystemPackages).filter((name) => !explicitSet.has(name));
|
|
109
|
+
const runtime = unique(runtimeTransforms).filter(
|
|
110
|
+
(name) => !explicitSet.has(name) && !ecosystem.includes(name),
|
|
111
|
+
);
|
|
112
|
+
const runtimeAugmentations = Object.freeze(unique(runtimeTransformAugmentations));
|
|
113
|
+
const nodeTransformPackages = Object.freeze([...explicit, ...ecosystem, ...runtime]);
|
|
114
|
+
// A workspace/file-linked react-native resolves to its real directory, which may
|
|
115
|
+
// not contain a node_modules segment. Node ownership is semantic, not a path-layout
|
|
116
|
+
// heuristic, so index that real root explicitly. Unlike generic packagePatterns,
|
|
117
|
+
// this root is never dropped merely because it sits below the project root: RN is
|
|
118
|
+
// the engine even when it is vendored in the workspace. Test entries still win.
|
|
119
|
+
const inferredReactNativeRoot = installedPackageDirOf("react-native", root);
|
|
120
|
+
const normalizedReactNativeRoots = Object.freeze(
|
|
121
|
+
unique([inferredReactNativeRoot, ...reactNativeRoots]).map((dir) => path.resolve(dir)),
|
|
122
|
+
);
|
|
123
|
+
// Either separator at every boundary: these patterns are handed to Vitest's resolver
|
|
124
|
+
// and also meet paths in the form Node reports them, backslashed on Windows.
|
|
125
|
+
const reactNativeRootPatterns = normalizedReactNativeRoots.map(
|
|
126
|
+
(dir) =>
|
|
127
|
+
new RegExp(
|
|
128
|
+
`^${normalizeFile(dir).replace(/\/$/, "").split("/").map(escapeRe).join("[\\\\/]")}[\\\\/]`,
|
|
129
|
+
),
|
|
130
|
+
);
|
|
131
|
+
const isReactNativeCore = (file, rawFile) =>
|
|
132
|
+
REACT_NATIVE_PATH.test(file) ||
|
|
133
|
+
normalizedReactNativeRoots.some((dir) => containsPath(dir, cleanRawFileId(rawFile)));
|
|
134
|
+
const reactNativePathFor = (rawFile) => {
|
|
135
|
+
const clean = cleanRawFileId(rawFile);
|
|
136
|
+
const file = normalizeFile(clean);
|
|
137
|
+
if (REACT_NATIVE_PATH.test(file)) return file;
|
|
138
|
+
const packageRoot = normalizedReactNativeRoots.find((dir) => containsPath(dir, clean));
|
|
139
|
+
if (!packageRoot) return null;
|
|
140
|
+
const relative = normalizeFile(path.relative(packageRoot, clean));
|
|
141
|
+
return `/react-native/${relative}`;
|
|
142
|
+
};
|
|
143
|
+
// Framework boundary: Vitest sorts resolver pattern arrays in place while it
|
|
144
|
+
// constructs VitestResolver. Keep the POLICY immutable, but hand mutation-prone
|
|
145
|
+
// consumers their own mutable arrays (the first frozen version passed every unit
|
|
146
|
+
// test and failed before native test collection with "Cannot assign to read only
|
|
147
|
+
// property '0'"). The rules and serialized manifest remain deeply immutable.
|
|
148
|
+
const externalPatterns = [
|
|
149
|
+
REACT_NATIVE_PATH,
|
|
150
|
+
...reactNativeRootPatterns,
|
|
151
|
+
...nodeTransformPackages.flatMap((name) => packagePatterns(name, root)),
|
|
152
|
+
];
|
|
153
|
+
const matchesNodeTransformedFile = buildPkgMatcher(nodeTransformPackages, root);
|
|
154
|
+
const explicitMatcher = buildPkgMatcher(explicit, root);
|
|
155
|
+
const ecosystemMatcher = buildPkgMatcher(ecosystem, root);
|
|
156
|
+
const runtimeMatcher = buildPkgMatcher(runtime, root);
|
|
157
|
+
const normalizedProjectDirs = Object.freeze(unique(projectDirs).map((dir) => path.resolve(dir)));
|
|
158
|
+
|
|
159
|
+
const decideFile = (rawFile) => {
|
|
160
|
+
const file = cleanFileId(rawFile);
|
|
161
|
+
if (TEST_ENTRY_PATTERNS.some((pattern) => pattern.test(file))) {
|
|
162
|
+
return decision("vite", "source", "vite", "per-file", ["test-entry"], [file]);
|
|
163
|
+
}
|
|
164
|
+
const runtimeResident = isRuntimeResidentFile(file);
|
|
165
|
+
const testRuntimeResident = isTestRuntimeResidentFile(file);
|
|
166
|
+
if (isReactNativeCore(file, rawFile)) {
|
|
167
|
+
return decision(
|
|
168
|
+
"node",
|
|
169
|
+
"cjs",
|
|
170
|
+
"registry",
|
|
171
|
+
"worker-resident",
|
|
172
|
+
["react-native-core"],
|
|
173
|
+
[
|
|
174
|
+
normalizedReactNativeRoots.find((dir) => containsPath(dir, cleanRawFileId(rawFile))) ??
|
|
175
|
+
"react-native/@react-native path",
|
|
176
|
+
"preloaded before the hot reset baseline",
|
|
177
|
+
],
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
if (!NODE_MODULES_PATH.test(file)) {
|
|
181
|
+
const projectDir = normalizedProjectDirs.find((dir) =>
|
|
182
|
+
containsPath(dir, cleanRawFileId(rawFile)),
|
|
183
|
+
);
|
|
184
|
+
if (projectDir) {
|
|
185
|
+
return decision("vite", "source", "vite", "per-file", ["project-source"], [projectDir]);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (explicitMatcher(file)) {
|
|
189
|
+
return decision(
|
|
190
|
+
"node",
|
|
191
|
+
"package",
|
|
192
|
+
"native-babel",
|
|
193
|
+
runtimeResident ? "worker-resident" : "per-file",
|
|
194
|
+
runtimeResident
|
|
195
|
+
? ["explicit-transform", "identity-sensitive-runtime"]
|
|
196
|
+
: ["explicit-transform"],
|
|
197
|
+
runtimeResident
|
|
198
|
+
? ["reactNative.transform.include", "CJS/ESM identity must remain stable"]
|
|
199
|
+
: ["reactNative.transform.include"],
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
if (ecosystemMatcher(file)) {
|
|
203
|
+
return decision(
|
|
204
|
+
"node",
|
|
205
|
+
"package",
|
|
206
|
+
"native-babel",
|
|
207
|
+
runtimeResident ? "worker-resident" : "per-file",
|
|
208
|
+
runtimeResident
|
|
209
|
+
? ["ecosystem-detected", "identity-sensitive-runtime"]
|
|
210
|
+
: ["ecosystem-detected"],
|
|
211
|
+
runtimeResident
|
|
212
|
+
? ["React Native dependency closure", "CJS/ESM identity must remain stable"]
|
|
213
|
+
: ["React Native dependency closure"],
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
if (runtimeMatcher(file)) {
|
|
217
|
+
return decision(
|
|
218
|
+
"node",
|
|
219
|
+
"package",
|
|
220
|
+
"native-babel",
|
|
221
|
+
runtimeResident ? "worker-resident" : "per-file",
|
|
222
|
+
runtimeResident
|
|
223
|
+
? ["runtime-transform", "identity-sensitive-runtime"]
|
|
224
|
+
: ["runtime-transform"],
|
|
225
|
+
runtimeResident
|
|
226
|
+
? ["serialized worker transform set", "CJS/ESM identity must remain stable"]
|
|
227
|
+
: ["serialized worker transform set"],
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
if (runtimeResident) {
|
|
231
|
+
return decision(
|
|
232
|
+
"vitest-default",
|
|
233
|
+
"package",
|
|
234
|
+
"default",
|
|
235
|
+
"worker-resident-if-external",
|
|
236
|
+
["identity-sensitive-runtime"],
|
|
237
|
+
[
|
|
238
|
+
"Vitest decides whether this package is inlined or external",
|
|
239
|
+
"if external, CJS/ESM identity must remain stable",
|
|
240
|
+
],
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
if (testRuntimeResident) {
|
|
244
|
+
return decision(
|
|
245
|
+
"vitest-default",
|
|
246
|
+
"package",
|
|
247
|
+
"default",
|
|
248
|
+
"worker-resident-if-external",
|
|
249
|
+
["test-runtime"],
|
|
250
|
+
[
|
|
251
|
+
"Vitest decides whether this package is inlined or external",
|
|
252
|
+
"if external, runner identity must remain stable",
|
|
253
|
+
],
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
return decision(
|
|
257
|
+
"vitest-default",
|
|
258
|
+
"unknown",
|
|
259
|
+
"default",
|
|
260
|
+
"vitest-default",
|
|
261
|
+
["delegated"],
|
|
262
|
+
["not governed by native ownership policy"],
|
|
263
|
+
);
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const manifest = () =>
|
|
267
|
+
Object.freeze({
|
|
268
|
+
version: 1,
|
|
269
|
+
enforcement,
|
|
270
|
+
projectRoot: root,
|
|
271
|
+
projectDirs: normalizedProjectDirs,
|
|
272
|
+
reactNativeRoots: normalizedReactNativeRoots,
|
|
273
|
+
nodeTransformPackages,
|
|
274
|
+
runtimeTransformAugmentations: runtimeAugmentations,
|
|
275
|
+
identityResidentPackages: RUNTIME_RESIDENT_PACKAGES,
|
|
276
|
+
testRuntimeResidentPackages: TEST_RUNTIME_RESIDENT_PACKAGES,
|
|
277
|
+
rules: Object.freeze([
|
|
278
|
+
Object.freeze({
|
|
279
|
+
effect: "owner-transform-reset",
|
|
280
|
+
owner: "node",
|
|
281
|
+
transform: "registry",
|
|
282
|
+
reset: "worker-resident",
|
|
283
|
+
reason: "react-native-core",
|
|
284
|
+
packages: Object.freeze(["react-native", "@react-native/*"]),
|
|
285
|
+
roots: normalizedReactNativeRoots,
|
|
286
|
+
}),
|
|
287
|
+
Object.freeze({
|
|
288
|
+
effect: "owner-transform",
|
|
289
|
+
owner: "node",
|
|
290
|
+
transform: "native-babel",
|
|
291
|
+
reason: "explicit-transform",
|
|
292
|
+
packages: Object.freeze(explicit),
|
|
293
|
+
}),
|
|
294
|
+
Object.freeze({
|
|
295
|
+
effect: "owner-transform",
|
|
296
|
+
owner: "node",
|
|
297
|
+
transform: "native-babel",
|
|
298
|
+
reason: "ecosystem-detected",
|
|
299
|
+
packages: Object.freeze(ecosystem),
|
|
300
|
+
}),
|
|
301
|
+
Object.freeze({
|
|
302
|
+
effect: "owner-transform",
|
|
303
|
+
owner: "node",
|
|
304
|
+
transform: "native-babel",
|
|
305
|
+
reason: "runtime-transform",
|
|
306
|
+
packages: Object.freeze(runtime),
|
|
307
|
+
}),
|
|
308
|
+
Object.freeze({
|
|
309
|
+
effect: "owner-transform-reset",
|
|
310
|
+
owner: "vite",
|
|
311
|
+
transform: "vite",
|
|
312
|
+
reset: "per-file",
|
|
313
|
+
reason: "project-source",
|
|
314
|
+
roots: normalizedProjectDirs,
|
|
315
|
+
}),
|
|
316
|
+
Object.freeze({
|
|
317
|
+
effect: "owner-transform-reset",
|
|
318
|
+
owner: "vite",
|
|
319
|
+
transform: "vite",
|
|
320
|
+
reset: "per-file",
|
|
321
|
+
reason: "test-entry",
|
|
322
|
+
patterns: Object.freeze(TEST_ENTRY_PATTERNS.map(String)),
|
|
323
|
+
}),
|
|
324
|
+
Object.freeze({
|
|
325
|
+
effect: "reset-if-external",
|
|
326
|
+
reset: "worker-resident",
|
|
327
|
+
reason: "identity-sensitive-runtime",
|
|
328
|
+
packages: RUNTIME_RESIDENT_PACKAGES,
|
|
329
|
+
}),
|
|
330
|
+
Object.freeze({
|
|
331
|
+
effect: "reset-if-external",
|
|
332
|
+
reset: "worker-resident",
|
|
333
|
+
reason: "test-runtime",
|
|
334
|
+
packages: TEST_RUNTIME_RESIDENT_PACKAGES,
|
|
335
|
+
}),
|
|
336
|
+
]),
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
return Object.freeze({
|
|
340
|
+
enforcement,
|
|
341
|
+
projectRoot: root,
|
|
342
|
+
nodeTransformPackages,
|
|
343
|
+
externalPatterns,
|
|
344
|
+
testEntryPatterns: [...TEST_ENTRY_PATTERNS],
|
|
345
|
+
matchesNodeTransformedFile,
|
|
346
|
+
isReactNativeFile: (file) => isReactNativeCore(cleanFileId(file), file),
|
|
347
|
+
reactNativePathFor,
|
|
348
|
+
decideFile,
|
|
349
|
+
manifest,
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Compare an actual graph observation with the owner assigned by the project.
|
|
355
|
+
* Delegated packages are intentionally inconclusive: Vitest is allowed to inline or
|
|
356
|
+
* externalize them. Likewise an inline-all override disables enforcement because it
|
|
357
|
+
* has explicitly taken precedence over the engine's externalization rules.
|
|
358
|
+
*/
|
|
359
|
+
export function findNativeOwnershipConflict(policy, file, observedOwner) {
|
|
360
|
+
if (!policy || policy.enforcement !== "enforced") return null;
|
|
361
|
+
if (observedOwner !== "vite" && observedOwner !== "node") return null;
|
|
362
|
+
const expected = policy.decideFile(file);
|
|
363
|
+
if (expected.owner === "vitest-default" || expected.owner === observedOwner) return null;
|
|
364
|
+
return Object.freeze({
|
|
365
|
+
code: "NATIVE_MODULE_OWNER_CONFLICT",
|
|
366
|
+
file,
|
|
367
|
+
observedOwner,
|
|
368
|
+
expectedOwner: expected.owner,
|
|
369
|
+
decision: expected,
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Node-owned packages a Vitest inline rule would claim before externalization.
|
|
375
|
+
* Matching mirrors Vitest's `matchPattern`: strings are fragments below a module
|
|
376
|
+
* directory and regular expressions test the normalized resolved id.
|
|
377
|
+
*/
|
|
378
|
+
export function findNativeInlineConflicts(policy, inline) {
|
|
379
|
+
if (!policy || inline == null) return [];
|
|
380
|
+
if (inline === true) return ["*"];
|
|
381
|
+
const patterns = Array.isArray(inline) ? inline : [inline];
|
|
382
|
+
const packages = ["react-native", "@react-native/*", ...policy.nodeTransformPackages];
|
|
383
|
+
const conflicts = [];
|
|
384
|
+
for (const name of packages) {
|
|
385
|
+
const concrete = name === "@react-native/*" ? "@react-native/assets-registry" : name;
|
|
386
|
+
const probes = [
|
|
387
|
+
`/project/node_modules/${concrete}/index.js`,
|
|
388
|
+
`C:\\project\\node_modules\\${concrete.replaceAll("/", "\\")}\\index.js`,
|
|
389
|
+
];
|
|
390
|
+
const dir = name.includes("*") ? null : packageDirOf(name, policy.projectRoot);
|
|
391
|
+
if (dir) probes.push(`${normalizeFile(dir).replace(/\/$/, "")}/index.js`);
|
|
392
|
+
const hit = patterns.some((pattern) => {
|
|
393
|
+
if (typeof pattern === "string") {
|
|
394
|
+
const fragment = `/node_modules/${pattern.replace(/^[/\\]+/, "")}`;
|
|
395
|
+
return probes.some((probe) => normalizeFile(probe).includes(fragment));
|
|
396
|
+
}
|
|
397
|
+
if (!pattern || typeof pattern.test !== "function") return false;
|
|
398
|
+
return probes.some((probe) => {
|
|
399
|
+
pattern.lastIndex = 0;
|
|
400
|
+
const matched = pattern.test(normalizeFile(probe));
|
|
401
|
+
pattern.lastIndex = 0;
|
|
402
|
+
return matched;
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
if (hit) conflicts.push(name);
|
|
406
|
+
}
|
|
407
|
+
return [...new Set(conflicts)];
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Parse the project policy passed across the Vitest worker boundary.
|
|
412
|
+
*
|
|
413
|
+
* Treat environment data as untrusted even though the plugin produced it: users can
|
|
414
|
+
* set `test.env`, and a malformed diagnostic must never stop a test run. Unknown
|
|
415
|
+
* manifest versions deliberately return null rather than being guessed at.
|
|
416
|
+
*/
|
|
417
|
+
export function parseNativeOwnershipManifest(raw) {
|
|
418
|
+
try {
|
|
419
|
+
const value = typeof raw === "string" ? JSON.parse(raw) : raw;
|
|
420
|
+
if (!value || typeof value !== "object" || value.version !== 1) return null;
|
|
421
|
+
if (value.enforcement !== "enforced" && value.enforcement !== "overridden-by-inline-all") {
|
|
422
|
+
return null;
|
|
423
|
+
}
|
|
424
|
+
if (typeof value.projectRoot !== "string") return null;
|
|
425
|
+
for (const key of [
|
|
426
|
+
"projectDirs",
|
|
427
|
+
"reactNativeRoots",
|
|
428
|
+
"nodeTransformPackages",
|
|
429
|
+
"runtimeTransformAugmentations",
|
|
430
|
+
"identityResidentPackages",
|
|
431
|
+
"testRuntimeResidentPackages",
|
|
432
|
+
"rules",
|
|
433
|
+
]) {
|
|
434
|
+
if (!Array.isArray(value[key])) return null;
|
|
435
|
+
}
|
|
436
|
+
if (
|
|
437
|
+
![
|
|
438
|
+
...value.projectDirs,
|
|
439
|
+
...value.reactNativeRoots,
|
|
440
|
+
...value.nodeTransformPackages,
|
|
441
|
+
...value.runtimeTransformAugmentations,
|
|
442
|
+
...value.identityResidentPackages,
|
|
443
|
+
...value.testRuntimeResidentPackages,
|
|
444
|
+
].every((entry) => typeof entry === "string")
|
|
445
|
+
) {
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
448
|
+
return value;
|
|
449
|
+
} catch {
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** Human-readable lines for diagnostics and `doctor`; generated from the manifest. */
|
|
455
|
+
export function formatNativeOwnershipManifest(input) {
|
|
456
|
+
const manifest = parseNativeOwnershipManifest(input);
|
|
457
|
+
if (!manifest) return ["ownership manifest unavailable or invalid"];
|
|
458
|
+
const list = (values) => (values.length > 0 ? values.join(", ") : "(none)");
|
|
459
|
+
const lines = [
|
|
460
|
+
`project: ${manifest.projectRoot}`,
|
|
461
|
+
`Vite / reset per file: ${list(manifest.projectDirs)} plus test entries`,
|
|
462
|
+
`Node / worker resident: react-native, @react-native/* (${list(manifest.reactNativeRoots)})`,
|
|
463
|
+
`Node / native transform / reset per file: ${list(manifest.nodeTransformPackages)}`,
|
|
464
|
+
`worker-time transform augmentations: ${list(manifest.runtimeTransformAugmentations)}`,
|
|
465
|
+
`resident if Vitest externalizes them: ${list(manifest.identityResidentPackages)}`,
|
|
466
|
+
`test runtime resident if external: ${list(manifest.testRuntimeResidentPackages)}`,
|
|
467
|
+
];
|
|
468
|
+
if (manifest.enforcement === "overridden-by-inline-all") {
|
|
469
|
+
lines.push(
|
|
470
|
+
"WARNING: server.deps.inline=true takes precedence over Node ownership; graph uniqueness is not guaranteed",
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
return lines;
|
|
474
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Short-lived entrypoint for a cold React Native registry build.
|
|
2
|
+
//
|
|
3
|
+
// The parent sends one JSON options object over stdin and receives one JSON result
|
|
4
|
+
// on stdout. No diagnostic logging is allowed here: stdout is a protocol, while a
|
|
5
|
+
// compiler failure is returned structurally for the parent to report once.
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import { buildRegistry } from "./registry.mjs";
|
|
8
|
+
|
|
9
|
+
function messageOf(error) {
|
|
10
|
+
return error instanceof Error ? error.message : String(error);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function respond(value) {
|
|
14
|
+
fs.writeSync(3, JSON.stringify(value));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const options = JSON.parse(fs.readFileSync(0, "utf8"));
|
|
19
|
+
const started = performance.now();
|
|
20
|
+
const registryFile = buildRegistry({
|
|
21
|
+
...options,
|
|
22
|
+
diagnostics: false,
|
|
23
|
+
failOnError: true,
|
|
24
|
+
});
|
|
25
|
+
let count = null;
|
|
26
|
+
if (registryFile) {
|
|
27
|
+
try {
|
|
28
|
+
count = JSON.parse(fs.readFileSync(`${registryFile}.json`, "utf8")).count ?? null;
|
|
29
|
+
} catch {}
|
|
30
|
+
}
|
|
31
|
+
respond({
|
|
32
|
+
ok: registryFile !== null,
|
|
33
|
+
registryFile,
|
|
34
|
+
count,
|
|
35
|
+
durationMs: Math.round(performance.now() - started),
|
|
36
|
+
heapUsed: process.memoryUsage().heapUsed,
|
|
37
|
+
rss: process.memoryUsage().rss,
|
|
38
|
+
});
|
|
39
|
+
if (registryFile === null) process.exitCode = 2;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
respond({ ok: false, error: messageOf(error) });
|
|
42
|
+
process.exitCode = 1;
|
|
43
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Parent-side controller for bounded cold registry compilation.
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { _warnRegistryUnavailable, buildRegistry } from "./registry.mjs";
|
|
4
|
+
import { runBoundedProcess } from "./bounded-process.mjs";
|
|
5
|
+
|
|
6
|
+
const INITIAL_HEAP_MB = 96;
|
|
7
|
+
const RETRY_HEAP_MB = 256;
|
|
8
|
+
const DEFAULT_TIMEOUT_MS = 120_000;
|
|
9
|
+
|
|
10
|
+
/** Compile once at 96 MiB, retrying at 256 MiB only for a genuine V8 heap OOM. */
|
|
11
|
+
export async function compileRegistryInChild(
|
|
12
|
+
compilerFile,
|
|
13
|
+
options,
|
|
14
|
+
{
|
|
15
|
+
initialHeapMb = INITIAL_HEAP_MB,
|
|
16
|
+
retryHeapMb = RETRY_HEAP_MB,
|
|
17
|
+
timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
18
|
+
spawnProcess,
|
|
19
|
+
} = {},
|
|
20
|
+
) {
|
|
21
|
+
const result = await runBoundedProcess(compilerFile, options, {
|
|
22
|
+
initialHeapMb,
|
|
23
|
+
retryHeapMb,
|
|
24
|
+
timeoutMs,
|
|
25
|
+
spawnProcess,
|
|
26
|
+
label: "compiler",
|
|
27
|
+
});
|
|
28
|
+
if (result.ok && typeof result.registryFile !== "string") {
|
|
29
|
+
return {
|
|
30
|
+
ok: false,
|
|
31
|
+
oom: false,
|
|
32
|
+
error: "compiler returned no registry file",
|
|
33
|
+
heapMb: result.heapMb,
|
|
34
|
+
attempts: result.attempts,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const registryCompilerDefaults = Object.freeze({
|
|
41
|
+
initialHeapMb: INITIAL_HEAP_MB,
|
|
42
|
+
retryHeapMb: RETRY_HEAP_MB,
|
|
43
|
+
timeoutMs: DEFAULT_TIMEOUT_MS,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Resolve a warm registry in the parent, or compile a cache miss in the bounded
|
|
48
|
+
* child. The optional compiler is a test seam proving warm production lookups do
|
|
49
|
+
* not spawn; ordinary callers always use compileRegistryInChild.
|
|
50
|
+
*/
|
|
51
|
+
export async function buildRegistryFor(options, compiler = compileRegistryInChild) {
|
|
52
|
+
try {
|
|
53
|
+
// Preserve the explicit escape hatch's existing diagnostic and silence
|
|
54
|
+
// contracts, and avoid launching a process for work the user disabled.
|
|
55
|
+
if (process.env.VITEST_NATIVE_NO_REGISTRY === "1") {
|
|
56
|
+
return buildRegistry(options);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// A warm run is just the cache key + manifest stats in this process. In
|
|
60
|
+
// particular, it never initializes Babel and never launches a child.
|
|
61
|
+
const cached = buildRegistry({ ...options, cacheOnly: true, failOnError: true });
|
|
62
|
+
if (cached) return cached;
|
|
63
|
+
|
|
64
|
+
const result = await compiler(
|
|
65
|
+
fileURLToPath(new URL("./registry-compiler.mjs", import.meta.url)),
|
|
66
|
+
options,
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// Validate through the same cache contract every reader uses instead of
|
|
70
|
+
// trusting a path returned over the child protocol. This also turns a losing
|
|
71
|
+
// compiler in a concurrent cold-cache race into success once the winner's
|
|
72
|
+
// atomic metadata write becomes visible (notably on Windows, where rename
|
|
73
|
+
// cannot replace an existing target).
|
|
74
|
+
let registryFile = buildRegistry({
|
|
75
|
+
...options,
|
|
76
|
+
cacheOnly: true,
|
|
77
|
+
failOnError: true,
|
|
78
|
+
diagnostics: false,
|
|
79
|
+
});
|
|
80
|
+
if (!registryFile && !result.ok) {
|
|
81
|
+
for (const delayMs of [10, 20, 40, 80]) {
|
|
82
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
83
|
+
registryFile = buildRegistry({
|
|
84
|
+
...options,
|
|
85
|
+
cacheOnly: true,
|
|
86
|
+
failOnError: true,
|
|
87
|
+
diagnostics: false,
|
|
88
|
+
});
|
|
89
|
+
if (registryFile) break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (registryFile) {
|
|
93
|
+
if (options.diagnostics) {
|
|
94
|
+
if (result.ok) {
|
|
95
|
+
const count = typeof result.count === "number" ? `${result.count} modules, ` : "";
|
|
96
|
+
const duration = typeof result.durationMs === "number" ? `${result.durationMs}ms, ` : "";
|
|
97
|
+
const rss =
|
|
98
|
+
typeof result.rss === "number"
|
|
99
|
+
? `${Math.ceil(result.rss / 1024 / 1024)} MiB RSS, `
|
|
100
|
+
: "";
|
|
101
|
+
console.log(
|
|
102
|
+
`[vitest-native] (native) precompiled RN registry in a bounded child ` +
|
|
103
|
+
`(${count}${duration}${rss}${result.heapMb} MiB heap cap, ` +
|
|
104
|
+
`${result.attempts} attempt${result.attempts === 1 ? "" : "s"})`,
|
|
105
|
+
);
|
|
106
|
+
} else {
|
|
107
|
+
console.log(
|
|
108
|
+
`[vitest-native] (native) reused a registry published by a concurrent compiler ` +
|
|
109
|
+
`(local compiler: ${result.error ?? "unknown failure"})`,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return registryFile;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
_warnRegistryUnavailable(
|
|
117
|
+
result.error ?? "bounded compiler completed without a usable cache entry",
|
|
118
|
+
);
|
|
119
|
+
return null;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
_warnRegistryUnavailable(error?.message ?? error);
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|