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
|
@@ -10,6 +10,7 @@ const KNOWN_OPTIONS = [
|
|
|
10
10
|
"mocks",
|
|
11
11
|
"diagnostics",
|
|
12
12
|
"assetExts",
|
|
13
|
+
"metroConfig",
|
|
13
14
|
"engine",
|
|
14
15
|
"transform",
|
|
15
16
|
"hotRuntime"
|
|
@@ -17,9 +18,11 @@ const KNOWN_OPTIONS = [
|
|
|
17
18
|
const KNOWN_HOT_RUNTIME_OPTIONS = [
|
|
18
19
|
"recycleAfterFiles",
|
|
19
20
|
"memoryLimit",
|
|
21
|
+
"allowUnboundedMemory",
|
|
20
22
|
"preserveGlobals",
|
|
21
23
|
"esmGeneration"
|
|
22
24
|
];
|
|
25
|
+
const KNOWN_METRO_CONFIG_OPTIONS = ["configFile"];
|
|
23
26
|
function assertStringArray(value, option) {
|
|
24
27
|
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string" || entry.length === 0)) throw new VitestNativeTypeError("INVALID_OPTION", `"${option}" must be an array of non-empty strings.`);
|
|
25
28
|
}
|
|
@@ -31,21 +34,30 @@ function validateOptions(options) {
|
|
|
31
34
|
if (options.platform !== void 0 && options.platform !== "ios" && options.platform !== "android") throw new VitestNativeTypeError("INVALID_OPTION", `"platform" must be "ios" or "android".`);
|
|
32
35
|
if (options.diagnostics !== void 0 && typeof options.diagnostics !== "boolean") throw new VitestNativeTypeError("INVALID_OPTION", `"diagnostics" must be a boolean.`);
|
|
33
36
|
if (options.assetExts !== void 0) assertStringArray(options.assetExts, "assetExts");
|
|
34
|
-
if (options.
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
if (
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
if (options.metroConfig !== void 0 && typeof options.metroConfig !== "boolean") {
|
|
38
|
+
if (options.metroConfig === null || Array.isArray(options.metroConfig) || typeof options.metroConfig !== "object") throw new VitestNativeTypeError("INVALID_OPTION", `"metroConfig" must be a boolean or an options object.`);
|
|
39
|
+
const metro = options.metroConfig;
|
|
40
|
+
const unknown = Object.keys(metro).filter((key) => !KNOWN_METRO_CONFIG_OPTIONS.includes(key));
|
|
41
|
+
if (unknown.length > 0) throw new VitestNativeTypeError("UNKNOWN_OPTION", `Unknown metroConfig option "${unknown[0]}". Valid options: ${KNOWN_METRO_CONFIG_OPTIONS.join(", ")}.`);
|
|
42
|
+
if (metro.configFile !== void 0 && (typeof metro.configFile !== "string" || !metro.configFile.trim())) throw new VitestNativeTypeError("INVALID_OPTION", `"metroConfig.configFile" must be a non-empty string.`);
|
|
43
|
+
}
|
|
44
|
+
if (options.transform !== void 0) {
|
|
45
|
+
if (Array.isArray(options.transform)) assertStringArray(options.transform, "transform");
|
|
46
|
+
else if (options.transform !== null && typeof options.transform === "object") {
|
|
47
|
+
const shape = options.transform;
|
|
48
|
+
const unknownKeys = Object.keys(shape).filter((k) => k !== "include" && k !== "exclude");
|
|
49
|
+
if (unknownKeys.length > 0) throw new VitestNativeTypeError("INVALID_OPTION", `"transform" accepts only "include" and "exclude"; received ${unknownKeys.map((k) => `"${k}"`).join(", ")}.`);
|
|
50
|
+
if (shape.include !== void 0) assertStringArray(shape.include, "transform.include");
|
|
51
|
+
if (shape.exclude !== void 0) assertStringArray(shape.exclude, "transform.exclude");
|
|
52
|
+
} else throw new VitestNativeTypeError("INVALID_OPTION", `"transform" must be an array of package names, or an object with "include" and/or "exclude".`);
|
|
53
|
+
}
|
|
42
54
|
if (options.presets !== void 0 && !Array.isArray(options.presets)) {
|
|
43
55
|
if (!(options.presets !== null && typeof options.presets === "object" && Object.values(options.presets).every((v) => typeof v === "boolean"))) throw new VitestNativeTypeError("INVALID_OPTION", "\"presets\" must be an array of presets, or an object of booleans to switch auto-detected presets off (for example { navigation: false }).");
|
|
44
56
|
}
|
|
45
57
|
if (options.mocks !== void 0 && (options.mocks === null || Array.isArray(options.mocks) || typeof options.mocks !== "object")) throw new VitestNativeTypeError("INVALID_OPTION", `"mocks" must be a plain object.`);
|
|
46
58
|
const hotRuntime = options.hotRuntime;
|
|
47
|
-
if (hotRuntime === void 0 || typeof hotRuntime === "boolean") return;
|
|
48
|
-
if (hotRuntime === null || Array.isArray(hotRuntime) || typeof hotRuntime !== "object") throw new VitestNativeTypeError("INVALID_OPTION", `"hotRuntime" must be a boolean or an options object.`);
|
|
59
|
+
if (hotRuntime === void 0 || typeof hotRuntime === "boolean" || hotRuntime === "auto") return;
|
|
60
|
+
if (hotRuntime === null || Array.isArray(hotRuntime) || typeof hotRuntime !== "object") throw new VitestNativeTypeError("INVALID_OPTION", `"hotRuntime" must be a boolean, "auto", or an options object.`);
|
|
49
61
|
const hotOptions = hotRuntime;
|
|
50
62
|
for (const key of Object.keys(hotOptions)) if (!KNOWN_HOT_RUNTIME_OPTIONS.includes(key)) {
|
|
51
63
|
const suggestion = findClosest(key, KNOWN_HOT_RUNTIME_OPTIONS);
|
|
@@ -53,6 +65,7 @@ function validateOptions(options) {
|
|
|
53
65
|
}
|
|
54
66
|
if (hotOptions.recycleAfterFiles !== void 0) assertNonNegativeInteger(hotOptions.recycleAfterFiles, "hotRuntime.recycleAfterFiles");
|
|
55
67
|
if (hotOptions.memoryLimit !== void 0) assertNonNegativeInteger(hotOptions.memoryLimit, "hotRuntime.memoryLimit");
|
|
68
|
+
if (hotOptions.allowUnboundedMemory !== void 0 && typeof hotOptions.allowUnboundedMemory !== "boolean") throw new VitestNativeTypeError("INVALID_OPTION", `"hotRuntime.allowUnboundedMemory" must be a boolean.`);
|
|
56
69
|
if (hotOptions.preserveGlobals !== void 0) assertStringArray(hotOptions.preserveGlobals, "hotRuntime.preserveGlobals");
|
|
57
70
|
if (hotOptions.esmGeneration !== void 0 && typeof hotOptions.esmGeneration !== "boolean") throw new VitestNativeTypeError("INVALID_OPTION", `"hotRuntime.esmGeneration" must be a boolean.`);
|
|
58
71
|
}
|
|
@@ -158,14 +171,47 @@ const PEER_REQUIREMENTS = [
|
|
|
158
171
|
* worker loads this file directly, so it must stay that way — module-level state
|
|
159
172
|
* would exist twice.
|
|
160
173
|
*/
|
|
174
|
+
/** Any file under a node_modules directory. */
|
|
175
|
+
const NODE_MODULES_PATH = /[\\/]node_modules[\\/]/;
|
|
161
176
|
/**
|
|
162
177
|
* React Native's own source, including the `@react-native/*` packages. Node owns
|
|
163
178
|
* these unconditionally: the Flow strip, the boundary mocks and the precompiled
|
|
164
179
|
* registry are all Node loader hooks.
|
|
165
180
|
*/
|
|
166
181
|
const REACT_NATIVE_PATH = /[\\/]node_modules[\\/](react-native|@react-native)[\\/]/;
|
|
182
|
+
/**
|
|
183
|
+
* A directory in the form Node's module loader reports paths: fs.realpathSync, not
|
|
184
|
+
* realpathSync.native. The native call expands Windows 8.3 short names (RUNNER~1) and
|
|
185
|
+
* rewrites letter case on case-insensitive disks, so a directory it returns can fail
|
|
186
|
+
* to contain the module ids Node hands the matchers.
|
|
187
|
+
*/
|
|
188
|
+
function canonicalDir(dir) {
|
|
189
|
+
try {
|
|
190
|
+
return fs.realpathSync(dir);
|
|
191
|
+
} catch {
|
|
192
|
+
return dir;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Resolve only from a physical node_modules directory at/above the project.
|
|
197
|
+
*
|
|
198
|
+
* Unlike createRequire.resolve(), this deliberately ignores NODE_PATH and loader
|
|
199
|
+
* hooks. It is for dependencies whose presence must be consumer-visible rather than
|
|
200
|
+
* borrowed from the test runner's own install.
|
|
201
|
+
*/
|
|
202
|
+
function installedPackageDirOf(name, projectRoot) {
|
|
203
|
+
const packagePath = name.split("/");
|
|
204
|
+
let dir = path.resolve(projectRoot);
|
|
205
|
+
for (;;) {
|
|
206
|
+
const candidate = path.join(dir, "node_modules", ...packagePath);
|
|
207
|
+
if (fs.existsSync(path.join(candidate, "package.json"))) return canonicalDir(candidate);
|
|
208
|
+
const parent = path.dirname(dir);
|
|
209
|
+
if (parent === dir) return null;
|
|
210
|
+
dir = parent;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
167
213
|
/** Escape a string for literal use inside a RegExp. */
|
|
168
|
-
function escapeRe(value) {
|
|
214
|
+
function escapeRe$1(value) {
|
|
169
215
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
170
216
|
}
|
|
171
217
|
/** Forward slashes, the form Vitest presents module ids in. */
|
|
@@ -204,7 +250,7 @@ function containsPath(dir, target) {
|
|
|
204
250
|
function packageDirOf(name, projectRoot) {
|
|
205
251
|
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
206
252
|
try {
|
|
207
|
-
return path.dirname(req.resolve(`${name}/package.json`));
|
|
253
|
+
return canonicalDir(path.dirname(req.resolve(`${name}/package.json`)));
|
|
208
254
|
} catch {}
|
|
209
255
|
let dir;
|
|
210
256
|
try {
|
|
@@ -214,7 +260,7 @@ function packageDirOf(name, projectRoot) {
|
|
|
214
260
|
}
|
|
215
261
|
for (;;) {
|
|
216
262
|
try {
|
|
217
|
-
if (createRequire(path.join(dir, "index.js"))("./package.json").name === name) return dir;
|
|
263
|
+
if (createRequire(path.join(dir, "index.js"))("./package.json").name === name) return canonicalDir(dir);
|
|
218
264
|
} catch {}
|
|
219
265
|
const parent = path.dirname(dir);
|
|
220
266
|
if (parent === dir) return null;
|
|
@@ -248,13 +294,337 @@ function packageDirOf(name, projectRoot) {
|
|
|
248
294
|
* there is nothing to resolve against.
|
|
249
295
|
*/
|
|
250
296
|
function packagePatterns(name, projectRoot) {
|
|
251
|
-
const patterns = [new RegExp(`[\\\\/]node_modules[\\\\/]${escapeRe(name)}[\\\\/]`)];
|
|
297
|
+
const patterns = [new RegExp(`[\\\\/]node_modules[\\\\/]${escapeRe$1(name)}[\\\\/]`)];
|
|
252
298
|
if (!projectRoot) return patterns;
|
|
253
299
|
const dir = packageDirOf(name, projectRoot);
|
|
254
|
-
if (dir && !containsPath(dir, projectRoot)) patterns.push(new RegExp(`^${escapeRe(toPosix(dir).replace(/\/$/, ""))}[\\\\/]`));
|
|
300
|
+
if (dir && !containsPath(dir, projectRoot)) patterns.push(new RegExp(`^${escapeRe$1(toPosix(dir).replace(/\/$/, ""))}[\\\\/]`));
|
|
255
301
|
return patterns;
|
|
256
302
|
}
|
|
257
303
|
/**
|
|
304
|
+
* A predicate over the same patterns Vitest is given, so what the loader transforms
|
|
305
|
+
* and what Vitest externalizes cannot drift apart.
|
|
306
|
+
*
|
|
307
|
+
* Paths are normalised before testing: the directory anchor is written with forward
|
|
308
|
+
* slashes (the form Vitest presents module ids in) while callers here pass whatever
|
|
309
|
+
* Node handed them, which on Windows is backslashes.
|
|
310
|
+
*/
|
|
311
|
+
function buildPkgMatcher(pkgs, projectRoot) {
|
|
312
|
+
const patterns = (pkgs || []).flatMap((name) => packagePatterns(name, projectRoot));
|
|
313
|
+
return (file) => {
|
|
314
|
+
const norm = toPosix(file);
|
|
315
|
+
return patterns.some((re) => re.test(norm));
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
//#endregion
|
|
319
|
+
//#region src/native/ownership.mjs
|
|
320
|
+
/**
|
|
321
|
+
* Project-scoped module ownership policy for the native engine.
|
|
322
|
+
*
|
|
323
|
+
* This module does not resolve or load anything. It turns already-established
|
|
324
|
+
* project facts into the patterns and predicates consumed by Vitest config, the
|
|
325
|
+
* CommonJS hook, the ESM loader, hot reset, and diagnostics. Keeping it pure lets
|
|
326
|
+
* the Vite main process and every worker reconstruct the same decision.
|
|
327
|
+
*/
|
|
328
|
+
const OUTSIDE_NODE_MODULES = String.raw`^(?!.*[\\/]node_modules[\\/])`;
|
|
329
|
+
/** Files Vitest may execute as entries. They always belong to Vite. */
|
|
330
|
+
const TEST_ENTRY_PATTERNS = Object.freeze([new RegExp(String.raw`${OUTSIDE_NODE_MODULES}.*\.(?:test|spec)\.[cm]?[jt]sx?$`), new RegExp(String.raw`${OUTSIDE_NODE_MODULES}.*[\\/]__tests__[\\/].*\.[cm]?[jt]sx?$`)]);
|
|
331
|
+
/**
|
|
332
|
+
* Identity-sensitive runtime packages whose CJS entries cannot be dropped while
|
|
333
|
+
* their ESM entries remain resident. Shared by ESM generation and hot CJS reset.
|
|
334
|
+
*/
|
|
335
|
+
const RUNTIME_RESIDENT_PACKAGES = Object.freeze([
|
|
336
|
+
"react",
|
|
337
|
+
"react-is",
|
|
338
|
+
"react-dom",
|
|
339
|
+
"scheduler",
|
|
340
|
+
"react-reconciler",
|
|
341
|
+
"react-test-renderer",
|
|
342
|
+
"test-renderer",
|
|
343
|
+
"@testing-library/react-native"
|
|
344
|
+
]);
|
|
345
|
+
const RUNTIME_RESIDENT_PATH = /[\\/]node_modules[\\/](react|react-is|react-dom|scheduler|react-reconciler|react-test-renderer|test-renderer|@testing-library[\\/]react-native)[\\/]/;
|
|
346
|
+
/** The test runner/runtime must never receive per-file ESM generation stamps. */
|
|
347
|
+
const TEST_RUNTIME_RESIDENT_PACKAGES = Object.freeze([
|
|
348
|
+
"vitest",
|
|
349
|
+
"@vitest/*",
|
|
350
|
+
"chai",
|
|
351
|
+
"vitest-native"
|
|
352
|
+
]);
|
|
353
|
+
const TEST_RUNTIME_RESIDENT_PATH = /[\\/]node_modules[\\/](vitest|@vitest[\\/][^\\/]+|chai|vitest-native)[\\/]/;
|
|
354
|
+
const normalizeFile = (file) => file.replace(/\\/g, "/");
|
|
355
|
+
const cleanRawFileId = (file) => file.replace(/[?#].*$/, "");
|
|
356
|
+
const cleanFileId = (file) => normalizeFile(cleanRawFileId(file));
|
|
357
|
+
const unique = (values) => [...new Set((values || []).filter(Boolean))];
|
|
358
|
+
const escapeRe = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
359
|
+
function isRuntimeResidentFile(file) {
|
|
360
|
+
return RUNTIME_RESIDENT_PATH.test(normalizeFile(file));
|
|
361
|
+
}
|
|
362
|
+
function isTestRuntimeResidentFile(file) {
|
|
363
|
+
return TEST_RUNTIME_RESIDENT_PATH.test(normalizeFile(file));
|
|
364
|
+
}
|
|
365
|
+
function decision(owner, format, transform, reset, reasons, evidence) {
|
|
366
|
+
return Object.freeze({
|
|
367
|
+
owner,
|
|
368
|
+
format,
|
|
369
|
+
transform,
|
|
370
|
+
reset,
|
|
371
|
+
reason: reasons[0],
|
|
372
|
+
reasons: Object.freeze(reasons),
|
|
373
|
+
evidence: Object.freeze(evidence)
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Build one immutable policy for a Vitest project.
|
|
378
|
+
*
|
|
379
|
+
* `explicitTransforms` is the public `transform` include list.
|
|
380
|
+
* `ecosystemPackages` is the detected RN dependency closure.
|
|
381
|
+
* `runtimeTransforms` is for workers reconstructing the already-combined list from
|
|
382
|
+
* serialized setup data; config-time callers normally leave it empty.
|
|
383
|
+
*/
|
|
384
|
+
function createNativeOwnershipPolicy({ projectRoot = process.cwd(), explicitTransforms = [], ecosystemPackages = [], runtimeTransforms = [], runtimeTransformAugmentations = [], projectDirs = [], reactNativeRoots = [], serverDepsInlineAll = false } = {}) {
|
|
385
|
+
const enforcement = serverDepsInlineAll ? "overridden-by-inline-all" : "enforced";
|
|
386
|
+
const root = path.resolve(projectRoot);
|
|
387
|
+
const explicit = unique(explicitTransforms);
|
|
388
|
+
const explicitSet = new Set(explicit);
|
|
389
|
+
const ecosystem = unique(ecosystemPackages).filter((name) => !explicitSet.has(name));
|
|
390
|
+
const runtime = unique(runtimeTransforms).filter((name) => !explicitSet.has(name) && !ecosystem.includes(name));
|
|
391
|
+
const runtimeAugmentations = Object.freeze(unique(runtimeTransformAugmentations));
|
|
392
|
+
const nodeTransformPackages = Object.freeze([
|
|
393
|
+
...explicit,
|
|
394
|
+
...ecosystem,
|
|
395
|
+
...runtime
|
|
396
|
+
]);
|
|
397
|
+
const inferredReactNativeRoot = installedPackageDirOf("react-native", root);
|
|
398
|
+
const normalizedReactNativeRoots = Object.freeze(unique([inferredReactNativeRoot, ...reactNativeRoots]).map((dir) => path.resolve(dir)));
|
|
399
|
+
const reactNativeRootPatterns = normalizedReactNativeRoots.map((dir) => new RegExp(`^${normalizeFile(dir).replace(/\/$/, "").split("/").map(escapeRe).join("[\\\\/]")}[\\\\/]`));
|
|
400
|
+
const isReactNativeCore = (file, rawFile) => REACT_NATIVE_PATH.test(file) || normalizedReactNativeRoots.some((dir) => containsPath(dir, cleanRawFileId(rawFile)));
|
|
401
|
+
const reactNativePathFor = (rawFile) => {
|
|
402
|
+
const clean = cleanRawFileId(rawFile);
|
|
403
|
+
const file = normalizeFile(clean);
|
|
404
|
+
if (REACT_NATIVE_PATH.test(file)) return file;
|
|
405
|
+
const packageRoot = normalizedReactNativeRoots.find((dir) => containsPath(dir, clean));
|
|
406
|
+
if (!packageRoot) return null;
|
|
407
|
+
return `/react-native/${normalizeFile(path.relative(packageRoot, clean))}`;
|
|
408
|
+
};
|
|
409
|
+
const externalPatterns = [
|
|
410
|
+
REACT_NATIVE_PATH,
|
|
411
|
+
...reactNativeRootPatterns,
|
|
412
|
+
...nodeTransformPackages.flatMap((name) => packagePatterns(name, root))
|
|
413
|
+
];
|
|
414
|
+
const matchesNodeTransformedFile = buildPkgMatcher(nodeTransformPackages, root);
|
|
415
|
+
const explicitMatcher = buildPkgMatcher(explicit, root);
|
|
416
|
+
const ecosystemMatcher = buildPkgMatcher(ecosystem, root);
|
|
417
|
+
const runtimeMatcher = buildPkgMatcher(runtime, root);
|
|
418
|
+
const normalizedProjectDirs = Object.freeze(unique(projectDirs).map((dir) => path.resolve(dir)));
|
|
419
|
+
const decideFile = (rawFile) => {
|
|
420
|
+
const file = cleanFileId(rawFile);
|
|
421
|
+
if (TEST_ENTRY_PATTERNS.some((pattern) => pattern.test(file))) return decision("vite", "source", "vite", "per-file", ["test-entry"], [file]);
|
|
422
|
+
const runtimeResident = isRuntimeResidentFile(file);
|
|
423
|
+
const testRuntimeResident = isTestRuntimeResidentFile(file);
|
|
424
|
+
if (isReactNativeCore(file, rawFile)) return decision("node", "cjs", "registry", "worker-resident", ["react-native-core"], [normalizedReactNativeRoots.find((dir) => containsPath(dir, cleanRawFileId(rawFile))) ?? "react-native/@react-native path", "preloaded before the hot reset baseline"]);
|
|
425
|
+
if (!NODE_MODULES_PATH.test(file)) {
|
|
426
|
+
const projectDir = normalizedProjectDirs.find((dir) => containsPath(dir, cleanRawFileId(rawFile)));
|
|
427
|
+
if (projectDir) return decision("vite", "source", "vite", "per-file", ["project-source"], [projectDir]);
|
|
428
|
+
}
|
|
429
|
+
if (explicitMatcher(file)) return decision("node", "package", "native-babel", runtimeResident ? "worker-resident" : "per-file", runtimeResident ? ["explicit-transform", "identity-sensitive-runtime"] : ["explicit-transform"], runtimeResident ? ["reactNative.transform.include", "CJS/ESM identity must remain stable"] : ["reactNative.transform.include"]);
|
|
430
|
+
if (ecosystemMatcher(file)) return decision("node", "package", "native-babel", runtimeResident ? "worker-resident" : "per-file", runtimeResident ? ["ecosystem-detected", "identity-sensitive-runtime"] : ["ecosystem-detected"], runtimeResident ? ["React Native dependency closure", "CJS/ESM identity must remain stable"] : ["React Native dependency closure"]);
|
|
431
|
+
if (runtimeMatcher(file)) return decision("node", "package", "native-babel", runtimeResident ? "worker-resident" : "per-file", runtimeResident ? ["runtime-transform", "identity-sensitive-runtime"] : ["runtime-transform"], runtimeResident ? ["serialized worker transform set", "CJS/ESM identity must remain stable"] : ["serialized worker transform set"]);
|
|
432
|
+
if (runtimeResident) return decision("vitest-default", "package", "default", "worker-resident-if-external", ["identity-sensitive-runtime"], ["Vitest decides whether this package is inlined or external", "if external, CJS/ESM identity must remain stable"]);
|
|
433
|
+
if (testRuntimeResident) return decision("vitest-default", "package", "default", "worker-resident-if-external", ["test-runtime"], ["Vitest decides whether this package is inlined or external", "if external, runner identity must remain stable"]);
|
|
434
|
+
return decision("vitest-default", "unknown", "default", "vitest-default", ["delegated"], ["not governed by native ownership policy"]);
|
|
435
|
+
};
|
|
436
|
+
const manifest = () => Object.freeze({
|
|
437
|
+
version: 1,
|
|
438
|
+
enforcement,
|
|
439
|
+
projectRoot: root,
|
|
440
|
+
projectDirs: normalizedProjectDirs,
|
|
441
|
+
reactNativeRoots: normalizedReactNativeRoots,
|
|
442
|
+
nodeTransformPackages,
|
|
443
|
+
runtimeTransformAugmentations: runtimeAugmentations,
|
|
444
|
+
identityResidentPackages: RUNTIME_RESIDENT_PACKAGES,
|
|
445
|
+
testRuntimeResidentPackages: TEST_RUNTIME_RESIDENT_PACKAGES,
|
|
446
|
+
rules: Object.freeze([
|
|
447
|
+
Object.freeze({
|
|
448
|
+
effect: "owner-transform-reset",
|
|
449
|
+
owner: "node",
|
|
450
|
+
transform: "registry",
|
|
451
|
+
reset: "worker-resident",
|
|
452
|
+
reason: "react-native-core",
|
|
453
|
+
packages: Object.freeze(["react-native", "@react-native/*"]),
|
|
454
|
+
roots: normalizedReactNativeRoots
|
|
455
|
+
}),
|
|
456
|
+
Object.freeze({
|
|
457
|
+
effect: "owner-transform",
|
|
458
|
+
owner: "node",
|
|
459
|
+
transform: "native-babel",
|
|
460
|
+
reason: "explicit-transform",
|
|
461
|
+
packages: Object.freeze(explicit)
|
|
462
|
+
}),
|
|
463
|
+
Object.freeze({
|
|
464
|
+
effect: "owner-transform",
|
|
465
|
+
owner: "node",
|
|
466
|
+
transform: "native-babel",
|
|
467
|
+
reason: "ecosystem-detected",
|
|
468
|
+
packages: Object.freeze(ecosystem)
|
|
469
|
+
}),
|
|
470
|
+
Object.freeze({
|
|
471
|
+
effect: "owner-transform",
|
|
472
|
+
owner: "node",
|
|
473
|
+
transform: "native-babel",
|
|
474
|
+
reason: "runtime-transform",
|
|
475
|
+
packages: Object.freeze(runtime)
|
|
476
|
+
}),
|
|
477
|
+
Object.freeze({
|
|
478
|
+
effect: "owner-transform-reset",
|
|
479
|
+
owner: "vite",
|
|
480
|
+
transform: "vite",
|
|
481
|
+
reset: "per-file",
|
|
482
|
+
reason: "project-source",
|
|
483
|
+
roots: normalizedProjectDirs
|
|
484
|
+
}),
|
|
485
|
+
Object.freeze({
|
|
486
|
+
effect: "owner-transform-reset",
|
|
487
|
+
owner: "vite",
|
|
488
|
+
transform: "vite",
|
|
489
|
+
reset: "per-file",
|
|
490
|
+
reason: "test-entry",
|
|
491
|
+
patterns: Object.freeze(TEST_ENTRY_PATTERNS.map(String))
|
|
492
|
+
}),
|
|
493
|
+
Object.freeze({
|
|
494
|
+
effect: "reset-if-external",
|
|
495
|
+
reset: "worker-resident",
|
|
496
|
+
reason: "identity-sensitive-runtime",
|
|
497
|
+
packages: RUNTIME_RESIDENT_PACKAGES
|
|
498
|
+
}),
|
|
499
|
+
Object.freeze({
|
|
500
|
+
effect: "reset-if-external",
|
|
501
|
+
reset: "worker-resident",
|
|
502
|
+
reason: "test-runtime",
|
|
503
|
+
packages: TEST_RUNTIME_RESIDENT_PACKAGES
|
|
504
|
+
})
|
|
505
|
+
])
|
|
506
|
+
});
|
|
507
|
+
return Object.freeze({
|
|
508
|
+
enforcement,
|
|
509
|
+
projectRoot: root,
|
|
510
|
+
nodeTransformPackages,
|
|
511
|
+
externalPatterns,
|
|
512
|
+
testEntryPatterns: [...TEST_ENTRY_PATTERNS],
|
|
513
|
+
matchesNodeTransformedFile,
|
|
514
|
+
isReactNativeFile: (file) => isReactNativeCore(cleanFileId(file), file),
|
|
515
|
+
reactNativePathFor,
|
|
516
|
+
decideFile,
|
|
517
|
+
manifest
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Compare an actual graph observation with the owner assigned by the project.
|
|
522
|
+
* Delegated packages are intentionally inconclusive: Vitest is allowed to inline or
|
|
523
|
+
* externalize them. Likewise an inline-all override disables enforcement because it
|
|
524
|
+
* has explicitly taken precedence over the engine's externalization rules.
|
|
525
|
+
*/
|
|
526
|
+
function findNativeOwnershipConflict(policy, file, observedOwner) {
|
|
527
|
+
if (!policy || policy.enforcement !== "enforced") return null;
|
|
528
|
+
if (observedOwner !== "vite" && observedOwner !== "node") return null;
|
|
529
|
+
const expected = policy.decideFile(file);
|
|
530
|
+
if (expected.owner === "vitest-default" || expected.owner === observedOwner) return null;
|
|
531
|
+
return Object.freeze({
|
|
532
|
+
code: "NATIVE_MODULE_OWNER_CONFLICT",
|
|
533
|
+
file,
|
|
534
|
+
observedOwner,
|
|
535
|
+
expectedOwner: expected.owner,
|
|
536
|
+
decision: expected
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Node-owned packages a Vitest inline rule would claim before externalization.
|
|
541
|
+
* Matching mirrors Vitest's `matchPattern`: strings are fragments below a module
|
|
542
|
+
* directory and regular expressions test the normalized resolved id.
|
|
543
|
+
*/
|
|
544
|
+
function findNativeInlineConflicts(policy, inline) {
|
|
545
|
+
if (!policy || inline == null) return [];
|
|
546
|
+
if (inline === true) return ["*"];
|
|
547
|
+
const patterns = Array.isArray(inline) ? inline : [inline];
|
|
548
|
+
const packages = [
|
|
549
|
+
"react-native",
|
|
550
|
+
"@react-native/*",
|
|
551
|
+
...policy.nodeTransformPackages
|
|
552
|
+
];
|
|
553
|
+
const conflicts = [];
|
|
554
|
+
for (const name of packages) {
|
|
555
|
+
const concrete = name === "@react-native/*" ? "@react-native/assets-registry" : name;
|
|
556
|
+
const probes = [`/project/node_modules/${concrete}/index.js`, `C:\\project\\node_modules\\${concrete.replaceAll("/", "\\")}\\index.js`];
|
|
557
|
+
const dir = name.includes("*") ? null : packageDirOf(name, policy.projectRoot);
|
|
558
|
+
if (dir) probes.push(`${normalizeFile(dir).replace(/\/$/, "")}/index.js`);
|
|
559
|
+
if (patterns.some((pattern) => {
|
|
560
|
+
if (typeof pattern === "string") {
|
|
561
|
+
const fragment = `/node_modules/${pattern.replace(/^[/\\]+/, "")}`;
|
|
562
|
+
return probes.some((probe) => normalizeFile(probe).includes(fragment));
|
|
563
|
+
}
|
|
564
|
+
if (!pattern || typeof pattern.test !== "function") return false;
|
|
565
|
+
return probes.some((probe) => {
|
|
566
|
+
pattern.lastIndex = 0;
|
|
567
|
+
const matched = pattern.test(normalizeFile(probe));
|
|
568
|
+
pattern.lastIndex = 0;
|
|
569
|
+
return matched;
|
|
570
|
+
});
|
|
571
|
+
})) conflicts.push(name);
|
|
572
|
+
}
|
|
573
|
+
return [...new Set(conflicts)];
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Parse the project policy passed across the Vitest worker boundary.
|
|
577
|
+
*
|
|
578
|
+
* Treat environment data as untrusted even though the plugin produced it: users can
|
|
579
|
+
* set `test.env`, and a malformed diagnostic must never stop a test run. Unknown
|
|
580
|
+
* manifest versions deliberately return null rather than being guessed at.
|
|
581
|
+
*/
|
|
582
|
+
function parseNativeOwnershipManifest(raw) {
|
|
583
|
+
try {
|
|
584
|
+
const value = typeof raw === "string" ? JSON.parse(raw) : raw;
|
|
585
|
+
if (!value || typeof value !== "object" || value.version !== 1) return null;
|
|
586
|
+
if (value.enforcement !== "enforced" && value.enforcement !== "overridden-by-inline-all") return null;
|
|
587
|
+
if (typeof value.projectRoot !== "string") return null;
|
|
588
|
+
for (const key of [
|
|
589
|
+
"projectDirs",
|
|
590
|
+
"reactNativeRoots",
|
|
591
|
+
"nodeTransformPackages",
|
|
592
|
+
"runtimeTransformAugmentations",
|
|
593
|
+
"identityResidentPackages",
|
|
594
|
+
"testRuntimeResidentPackages",
|
|
595
|
+
"rules"
|
|
596
|
+
]) if (!Array.isArray(value[key])) return null;
|
|
597
|
+
if (![
|
|
598
|
+
...value.projectDirs,
|
|
599
|
+
...value.reactNativeRoots,
|
|
600
|
+
...value.nodeTransformPackages,
|
|
601
|
+
...value.runtimeTransformAugmentations,
|
|
602
|
+
...value.identityResidentPackages,
|
|
603
|
+
...value.testRuntimeResidentPackages
|
|
604
|
+
].every((entry) => typeof entry === "string")) return null;
|
|
605
|
+
return value;
|
|
606
|
+
} catch {
|
|
607
|
+
return null;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
/** Human-readable lines for diagnostics and `doctor`; generated from the manifest. */
|
|
611
|
+
function formatNativeOwnershipManifest(input) {
|
|
612
|
+
const manifest = parseNativeOwnershipManifest(input);
|
|
613
|
+
if (!manifest) return ["ownership manifest unavailable or invalid"];
|
|
614
|
+
const list = (values) => values.length > 0 ? values.join(", ") : "(none)";
|
|
615
|
+
const lines = [
|
|
616
|
+
`project: ${manifest.projectRoot}`,
|
|
617
|
+
`Vite / reset per file: ${list(manifest.projectDirs)} plus test entries`,
|
|
618
|
+
`Node / worker resident: react-native, @react-native/* (${list(manifest.reactNativeRoots)})`,
|
|
619
|
+
`Node / native transform / reset per file: ${list(manifest.nodeTransformPackages)}`,
|
|
620
|
+
`worker-time transform augmentations: ${list(manifest.runtimeTransformAugmentations)}`,
|
|
621
|
+
`resident if Vitest externalizes them: ${list(manifest.identityResidentPackages)}`,
|
|
622
|
+
`test runtime resident if external: ${list(manifest.testRuntimeResidentPackages)}`
|
|
623
|
+
];
|
|
624
|
+
if (manifest.enforcement === "overridden-by-inline-all") lines.push("WARNING: server.deps.inline=true takes precedence over Node ownership; graph uniqueness is not guaranteed");
|
|
625
|
+
return lines;
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
258
628
|
* The native engine's dependencies that do NOT resolve from the project root.
|
|
259
629
|
*
|
|
260
630
|
* react-native itself is part of the check: the Babel toolchain alone chose
|
|
@@ -482,11 +852,12 @@ function manifestsFrom(projectRoot) {
|
|
|
482
852
|
* test infrastructure in NEVER_INLINE, and anything the consumer listed explicitly
|
|
483
853
|
* in `transform` — that option keeps its existing meaning and takes precedence.
|
|
484
854
|
*/
|
|
485
|
-
function detectEcosystemPackages(projectRoot, explicit = [], testRoots = [], neverTransform = []) {
|
|
855
|
+
function detectEcosystemPackages(projectRoot, explicit = [], testRoots = [], neverTransform = [], activePresets) {
|
|
486
856
|
const roots = (Array.isArray(projectRoot) ? projectRoot : [projectRoot]).filter(Boolean);
|
|
857
|
+
const shadowed = Object.entries(AUTO_DETECT_PRESETS).filter(([, presetName]) => activePresets === void 0 || activePresets.includes(presetName)).map(([pkg]) => pkg);
|
|
487
858
|
const skip = /* @__PURE__ */ new Set([
|
|
488
859
|
...NEVER_INLINE,
|
|
489
|
-
...
|
|
860
|
+
...shadowed,
|
|
490
861
|
...explicit,
|
|
491
862
|
...neverTransform
|
|
492
863
|
]);
|
|
@@ -594,4 +965,4 @@ function detectEcosystemPackages(projectRoot, explicit = [], testRoots = [], nev
|
|
|
594
965
|
return [...inClosure].sort();
|
|
595
966
|
}
|
|
596
967
|
//#endregion
|
|
597
|
-
export {
|
|
968
|
+
export { findNativeOwnershipConflict as a, packageDirOf as c, validatePeerDependency as d, warnUnknownOptions as f, findNativeInlineConflicts as i, PEER_REQUIREMENTS as l, detectEngine as n, formatNativeOwnershipManifest as o, createNativeOwnershipPolicy as r, containsPath as s, detectEcosystemPackages as t, validateOptions as u };
|