vitest-native 0.14.1 → 0.15.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/README.md +11 -10
- package/dist/cli.d.mts +9 -1
- package/dist/cli.mjs +2609 -361
- package/dist/context.d.mts +19 -0
- package/dist/context.mjs +47 -1
- package/dist/docs-url.d.mts +4 -0
- package/dist/docs-url.mjs +9 -0
- package/dist/errors.d.mts +21 -1
- package/dist/hang-watch-setup.mjs +55 -0
- package/dist/hang-watch.d.mts +56 -0
- package/dist/hang-watch.mjs +343 -0
- package/dist/helpers.cjs +24 -53
- package/dist/helpers.mjs +22 -1
- package/dist/host-D0r3P_T1.mjs +1985 -0
- package/dist/{index-aNBCTfA5.d.mts → index-JOR602XU.d.mts} +105 -2
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +723 -149
- package/dist/jest-compat/aliases.d.mts +1 -0
- package/dist/jest-compat/aliases.mjs +11 -0
- package/dist/jest-compat/interop.mjs +1 -1
- package/dist/jest-compat/jest-object.mjs +23 -5
- package/dist/jest-compat/node-registry.mjs +836 -68
- package/dist/jest-compat/setup.mjs +205 -12
- package/dist/jest-compat.cjs +383 -47
- package/dist/jest-compat.d.cts +19 -2
- package/dist/jest-compat.d.mts +19 -2
- package/dist/jest-compat.mjs +232 -47
- package/dist/matchers.d.cts +0 -1
- package/dist/matchers.d.mts +0 -1
- package/dist/native/assets.mjs +3 -1
- package/dist/native/babel-caller.d.mts +13 -0
- package/dist/native/babel-caller.mjs +24 -0
- package/dist/native/babel-toolchain.mjs +27 -3
- package/dist/native/boundary.mjs +45 -8
- package/dist/native/bounded-process.mjs +63 -5
- package/dist/native/bridge-client.mjs +133 -0
- package/dist/native/bridge-server.mjs +215 -0
- package/dist/native/bridge-thread.mjs +79 -0
- package/dist/native/bridge.mjs +512 -0
- package/dist/native/compile-error.mjs +120 -0
- package/dist/native/explain.mjs +3 -2
- package/dist/native/expo-module-mocks.mjs +2 -0
- package/dist/native/export-condition-recovery.mjs +20 -99
- package/dist/native/hooks.mjs +256 -133
- package/dist/native/jest-resolvers.mjs +94 -0
- package/dist/native/library-jest.mjs +352 -0
- package/dist/native/loader.mjs +167 -73
- package/dist/native/match.mjs +67 -7
- package/dist/native/module-reset.mjs +44 -49
- package/dist/native/ownership-ledger.d.mts +87 -0
- package/dist/native/ownership-ledger.mjs +524 -0
- package/dist/native/ownership.d.mts +20 -0
- package/dist/native/ownership.mjs +114 -0
- package/dist/native/registry-compiler.mjs +39 -5
- package/dist/native/registry-process.mjs +327 -28
- package/dist/native/registry.mjs +752 -200
- package/dist/native/resident-health.mjs +1 -0
- package/dist/native/resolve.d.mts +7 -0
- package/dist/native/resolve.mjs +119 -4
- package/dist/native/rntl-hooks.mjs +9 -5
- package/dist/native/rntl-text-host.mjs +5 -15
- package/dist/native/root-spelling.d.mts +6 -0
- package/dist/native/root-spelling.mjs +77 -0
- package/dist/native/runner.mjs +2 -19
- package/dist/native/setup.mjs +119 -38
- package/dist/native/shared-resolver.d.mts +141 -0
- package/dist/native/shared-resolver.mjs +719 -0
- package/dist/native/test-runner.mjs +26 -0
- package/dist/native/transform.mjs +18 -22
- package/dist/native/worker.mjs +9 -4
- package/dist/node-dispatch.mjs +293 -0
- package/dist/{preset-map-CogjYmSw.mjs → preset-map-DdAexIwz.mjs} +113 -2
- package/dist/{presets-CXDyg51l.mjs → presets-_EaSw29m.mjs} +710 -130
- package/dist/presets.d.mts +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/rntl-matchers.d.ts +11 -15
- package/dist/rntl-support.mjs +28 -13
- package/dist/root-mocks-BNXeosbR.mjs +155 -0
- package/dist/serializer.cjs +7 -5
- package/dist/serializer.d.cts +7 -5
- package/dist/serializer.d.mts +7 -5
- package/dist/serializer.mjs +7 -5
- package/dist/setup.mjs +26 -27
- package/docs/migrating-from-jest.md +9 -232
- package/docs/migrating-from-vitest-react-native.md +4 -4
- package/docs/release-readiness.md +9 -11
- package/docs/versioning.md +38 -9
- package/package.json +59 -33
- package/dist/host-BAAAlSkO.mjs +0 -1028
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
// Jest, as a library's own test support sees it.
|
|
2
|
+
//
|
|
3
|
+
// React Native libraries ship their test support for Jest: setup files that call
|
|
4
|
+
// `jest.mock` for their native modules (react-native-gesture-handler/jestSetup.js,
|
|
5
|
+
// @shopify/flash-list/jestSetup.js) and mocks that call `jest.fn` or import
|
|
6
|
+
// `@jest/globals` (react-native-safe-area-context/jest/mock). Under Vitest a global
|
|
7
|
+
// `jest` is Vitest's `vi`, whose `mock` never reaches Node's `require`, and a library
|
|
8
|
+
// file is not compiled by jestMockTransform, so such a setup file ran and changed
|
|
9
|
+
// nothing. Here a library's support files get a `jest` of their own, with Jest's
|
|
10
|
+
// semantics where they differ:
|
|
11
|
+
//
|
|
12
|
+
// - `jest.mock(spec, factory)` registers the factory in the per-file Node registry
|
|
13
|
+
// (jest-compat/node-registry.mjs), keyed from the file that called it, so every
|
|
14
|
+
// require of the module gets it, as under Jest's single registry;
|
|
15
|
+
// - `jest.requireActual` gives the module itself, past a preset's shadow;
|
|
16
|
+
// - `jest.fn`: an implementation a library installs while it builds its mocks
|
|
17
|
+
// (`jest.fn().mockImplementation(...)`, `mockReturnValue(...)`) is the mock's own, so
|
|
18
|
+
// `vi.resetAllMocks()` and `mockReset: true` return the mock to it, where Vitest would
|
|
19
|
+
// drop it (the bug class mock-reset-fragile presets had, #278/#287);
|
|
20
|
+
// - `@jest/globals` required from such a file is the same `jest`.
|
|
21
|
+
//
|
|
22
|
+
// The `jest` reaches a file as a function parameter around its compiled code, never as
|
|
23
|
+
// `globalThis.jest`: code elsewhere (RNTL's fake-timer detection) reads `typeof jest` to
|
|
24
|
+
// decide whether it runs under Jest.
|
|
25
|
+
//
|
|
26
|
+
// A package's test support is active once one of its support entries loads: a request
|
|
27
|
+
// whose leaf is `jestSetup` or `mock`, or a file under its `jest/` directory, whether a
|
|
28
|
+
// preset loads it or a suite migrated from Jest imports it. Its files that mention
|
|
29
|
+
// `jest` then get one.
|
|
30
|
+
//
|
|
31
|
+
// What a support entry registers lasts as long as the entry's module does. Each test
|
|
32
|
+
// file gets a fresh module registry by default, and under the hot runtime, which evicts
|
|
33
|
+
// node_modules per file, so the entry runs again and registers again. Under
|
|
34
|
+
// `isolate: false` modules outlive the file, a cached entry never runs again, and its
|
|
35
|
+
// registrations stay with it, as the module's other state does.
|
|
36
|
+
import fs from "node:fs";
|
|
37
|
+
import Module from "node:module";
|
|
38
|
+
import path from "node:path";
|
|
39
|
+
import vm from "node:vm";
|
|
40
|
+
import { createJestObject } from "../jest-compat/jest-object.mjs";
|
|
41
|
+
import { nodeRegistry } from "../jest-compat/node-registry.mjs";
|
|
42
|
+
import { nodeDispatch } from "../node-dispatch.mjs";
|
|
43
|
+
import { projectContext } from "../context.mjs";
|
|
44
|
+
import { rootSpellingOf, rootSpellings } from "./root-spelling.mjs";
|
|
45
|
+
|
|
46
|
+
const SUPPORT_LEAVES = new Set(["jestSetup", "mock"]);
|
|
47
|
+
const MENTIONS_JEST = /\bjest\b/;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The worker's library-support state, on the dispatcher's "library-jest" channel: one per
|
|
51
|
+
* worker however many instances of this file are evaluated (Node loads it for the require
|
|
52
|
+
* hooks; the setup file may reach another instance, through Vite's runner under the hot
|
|
53
|
+
* runtime), as node-dispatch.mjs keeps its own.
|
|
54
|
+
*/
|
|
55
|
+
const lib = nodeDispatch().channel("library-jest");
|
|
56
|
+
/** Packages whose test support has loaded in this worker. */
|
|
57
|
+
const active = (lib.active ??= new Set());
|
|
58
|
+
/** The support entries evaluated in this worker: `{ pkg, module }`. */
|
|
59
|
+
const entries = (lib.entries ??= new Set());
|
|
60
|
+
/** How many library-building scopes are open (support entries, their factories). */
|
|
61
|
+
lib.building ??= 0;
|
|
62
|
+
/** The support entries being evaluated, innermost last: `{ pkg, module }`. */
|
|
63
|
+
const owners = (lib.owners ??= []);
|
|
64
|
+
/** One `jest` per file. */
|
|
65
|
+
const jestByFile = (lib.jestByFile ??= new Map());
|
|
66
|
+
/** Each directory's package, by its nearest named package.json (workspace links too). */
|
|
67
|
+
const packageByFile = (lib.packageByFile ??= new Map());
|
|
68
|
+
const projectRoot = path.resolve(projectContext()?.projectRoot ?? process.cwd());
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The package `file` belongs to and its directory: the nearest package.json that names
|
|
72
|
+
* a package (a `lib/commonjs/package.json` holding only `"type"` does not). By manifest
|
|
73
|
+
* rather than by node_modules path, so a workspace or `file:` link, whose directory need
|
|
74
|
+
* not carry the package's name, is found too. Cached per directory.
|
|
75
|
+
*/
|
|
76
|
+
function packageRootOf(file) {
|
|
77
|
+
if (typeof file !== "string") return null;
|
|
78
|
+
const start = path.dirname(file);
|
|
79
|
+
if (packageByFile.has(start)) return packageByFile.get(start);
|
|
80
|
+
let root = null;
|
|
81
|
+
for (let dir = start; ; dir = path.dirname(dir)) {
|
|
82
|
+
const manifest = path.join(dir, "package.json");
|
|
83
|
+
if (fs.existsSync(manifest)) {
|
|
84
|
+
let name;
|
|
85
|
+
try {
|
|
86
|
+
name = JSON.parse(fs.readFileSync(manifest, "utf8")).name;
|
|
87
|
+
} catch {
|
|
88
|
+
name = undefined;
|
|
89
|
+
}
|
|
90
|
+
if (typeof name === "string") {
|
|
91
|
+
root = { name, dir };
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (path.dirname(dir) === dir) break;
|
|
96
|
+
}
|
|
97
|
+
packageByFile.set(start, root);
|
|
98
|
+
return root;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Whether `file` belongs to a package whose test support has loaded. */
|
|
102
|
+
export function isSupportFile(file) {
|
|
103
|
+
const root = packageRootOf(file);
|
|
104
|
+
return root !== null && active.has(root.name);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The package `file` is a test-support entry of (its `jestSetup`, `mock`, or a file
|
|
109
|
+
* under its `jest/`), or null: a support file loaded by `import` or a `setupFiles` entry
|
|
110
|
+
* reaches the CommonJS loader without the request `Module._load` sees.
|
|
111
|
+
*/
|
|
112
|
+
function supportPackageOfFile(file) {
|
|
113
|
+
const root = packageRootOf(file);
|
|
114
|
+
if (root === null) return null;
|
|
115
|
+
// The project's own `jest/setup.js` or `src/api/mock.ts` is not a library's support:
|
|
116
|
+
// giving it a library's `jest` would make its mocks outlive a reset.
|
|
117
|
+
// The package is the project root or contains it, in any spelling of the root
|
|
118
|
+
// (root-spelling.mjs): the directory comes from a file name, which may be spelled
|
|
119
|
+
// otherwise than the root.
|
|
120
|
+
if (rootSpellings(projectRoot).some((spelling) => rootSpellingOf(root.dir, spelling) !== null)) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
const rest = path.relative(root.dir, file).split(path.sep).join("/");
|
|
124
|
+
const leaf = path.posix.basename(rest).replace(/\.[cm]?[jt]sx?$/, "");
|
|
125
|
+
return SUPPORT_LEAVES.has(leaf) || rest.split("/")[0] === "jest" ? root.name : null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Whether the support entry `owner` is still the module a `require` of it returns. */
|
|
129
|
+
function loaded(owner) {
|
|
130
|
+
return owner !== null && Module._cache[owner.module.filename] === owner.module;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Start a test file: of what libraries registered, only what a still-loaded support
|
|
135
|
+
* entry made is kept (see the top of this file), and only those packages stay active.
|
|
136
|
+
* The engine's own library setup files run after this, for every file (setup.mjs).
|
|
137
|
+
*/
|
|
138
|
+
export function beginTestFile() {
|
|
139
|
+
active.clear();
|
|
140
|
+
jestByFile.clear();
|
|
141
|
+
// A support entry still loaded (`isolate: false` keeps modules) keeps its package's
|
|
142
|
+
// support active, whether or not it registered a mock.
|
|
143
|
+
for (const owner of entries) {
|
|
144
|
+
if (loaded(owner)) active.add(owner.pkg);
|
|
145
|
+
else entries.delete(owner);
|
|
146
|
+
}
|
|
147
|
+
globalThis.__vitest_native_jest_registry?.clear({
|
|
148
|
+
keep(entry) {
|
|
149
|
+
if (!entry.library || !loaded(entry.owner)) return false;
|
|
150
|
+
active.add(entry.owner.pkg);
|
|
151
|
+
return true;
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const BASELINE_SETTERS = [
|
|
157
|
+
["mockImplementation", (impl) => impl],
|
|
158
|
+
["mockReturnValue", (value) => () => value],
|
|
159
|
+
["mockResolvedValue", (value) => () => Promise.resolve(value)],
|
|
160
|
+
["mockRejectedValue", (value) => () => Promise.reject(value)],
|
|
161
|
+
];
|
|
162
|
+
|
|
163
|
+
/** The `jest` a library file sees. */
|
|
164
|
+
export function libraryJestFor(vi, file) {
|
|
165
|
+
let jest = jestByFile.get(file);
|
|
166
|
+
if (jest) return jest;
|
|
167
|
+
const registry = () => nodeRegistry();
|
|
168
|
+
const base = createJestObject(vi);
|
|
169
|
+
// `jest.fn` whose built implementation is the mock's own: a delegate Vitest keeps
|
|
170
|
+
// through a reset, which calls whatever implementation the library installed.
|
|
171
|
+
const fn = (impl) => {
|
|
172
|
+
let own = impl;
|
|
173
|
+
const mock = base.fn(function (...args) {
|
|
174
|
+
return own === undefined ? undefined : own.apply(this, args);
|
|
175
|
+
});
|
|
176
|
+
for (const [name, toImpl] of BASELINE_SETTERS) {
|
|
177
|
+
const set = mock[name];
|
|
178
|
+
mock[name] = (value) => {
|
|
179
|
+
if (lib.building === 0) return set.call(mock, value);
|
|
180
|
+
own = toImpl(value);
|
|
181
|
+
return mock;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
return mock;
|
|
185
|
+
};
|
|
186
|
+
// A factory the library registers runs later, when the module is first required;
|
|
187
|
+
// what it builds is part of the library's mocks too, and belongs to the same entry.
|
|
188
|
+
const asBuilding = (factory, owner) => () => {
|
|
189
|
+
lib.building++;
|
|
190
|
+
if (owner !== null) owners.push(owner);
|
|
191
|
+
try {
|
|
192
|
+
return factory();
|
|
193
|
+
} finally {
|
|
194
|
+
if (owner !== null) owners.pop();
|
|
195
|
+
lib.building--;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
const mock = (spec, factory) => {
|
|
199
|
+
const owner = owners.at(-1) ?? null;
|
|
200
|
+
registry().register(file, spec, asBuilding(factory ?? (() => ({})), owner), undefined, {
|
|
201
|
+
library: true,
|
|
202
|
+
owner,
|
|
203
|
+
});
|
|
204
|
+
return jest;
|
|
205
|
+
};
|
|
206
|
+
jest = createJestObject(vi, {
|
|
207
|
+
fn,
|
|
208
|
+
mock,
|
|
209
|
+
doMock: mock,
|
|
210
|
+
unmock: (spec) => {
|
|
211
|
+
registry().unregister(file, spec);
|
|
212
|
+
return jest;
|
|
213
|
+
},
|
|
214
|
+
requireActual: (spec) => registry().require(spec, file, true),
|
|
215
|
+
requireMock: (spec) => registry().require(spec, file, false),
|
|
216
|
+
});
|
|
217
|
+
jestByFile.set(file, jest);
|
|
218
|
+
return jest;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Install the pieces in the worker: a support file's code runs with its own `jest`, which
|
|
223
|
+
* it reaches through the dispatcher's "library-jest" channel (no global of its own), and
|
|
224
|
+
* its `@jest/globals` is that `jest` (the dispatcher's librarySupport step). Its code is
|
|
225
|
+
* compiled by the engine's CommonJS handlers (native/hooks.mjs), which call
|
|
226
|
+
* compileSupport/loadSupport. Once per worker.
|
|
227
|
+
*/
|
|
228
|
+
export function installLibraryJest(vi, globals = {}) {
|
|
229
|
+
lib.vi = vi;
|
|
230
|
+
lib.globals = globals;
|
|
231
|
+
lib.libraryJestFor = (file) => libraryJestFor(lib.vi, file);
|
|
232
|
+
// `@jest/globals` from a support file is that file's `jest`, with the test API beside it
|
|
233
|
+
// (LOAD_STEPS). jest-runtime answers it before any mock, as this step's rank does.
|
|
234
|
+
nodeDispatch().load.add(
|
|
235
|
+
"librarySupport",
|
|
236
|
+
(next) =>
|
|
237
|
+
function (request, parent, ...rest) {
|
|
238
|
+
if (request === "@jest/globals" && isSupportFile(parent?.filename)) {
|
|
239
|
+
const jest = libraryJestFor(lib.vi, parent.filename);
|
|
240
|
+
const api = { ...lib.globals, jest };
|
|
241
|
+
return { ...api, __esModule: true, default: api };
|
|
242
|
+
}
|
|
243
|
+
return next(request, parent, ...rest);
|
|
244
|
+
},
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* What library support needs of `filename`'s compile, or null (the common case, one
|
|
250
|
+
* cached lookup): the package it is a support entry of, and whether its code may need a
|
|
251
|
+
* `jest`.
|
|
252
|
+
*/
|
|
253
|
+
function supportOf(filename) {
|
|
254
|
+
if (lib.vi === undefined) return null;
|
|
255
|
+
const entryOf = supportPackageOfFile(filename);
|
|
256
|
+
if (entryOf !== null) active.add(entryOf);
|
|
257
|
+
const wantsJest = isSupportFile(filename);
|
|
258
|
+
return entryOf === null && !wantsJest ? null : { entryOf, wantsJest };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** `content` with a `jest` of the file's own, or null when it needs none or cannot have one. */
|
|
262
|
+
function withJest(content, filename, probe) {
|
|
263
|
+
if (typeof content !== "string" || !MENTIONS_JEST.test(content)) return null;
|
|
264
|
+
// On the first line, so line numbers stay. A parameter rather than a `var`, and the
|
|
265
|
+
// code in a function of its own, so a file that declares its own `jest`
|
|
266
|
+
// (`const { jest } = require("@jest/globals")`) still compiles and shadows it. Node's
|
|
267
|
+
// module arguments pass through, for code that reads `arguments`.
|
|
268
|
+
const wrapped =
|
|
269
|
+
`(function (jest, args) {return (function () {${stripShebang(content)}\n}).apply(this, args);}).call(this, ` +
|
|
270
|
+
`globalThis[Symbol.for("vitest-native.node-dispatch")].channel("library-jest").libraryJestFor(__filename), arguments);`;
|
|
271
|
+
// An ES module's syntax cannot sit in a function body: a file read from disk is probed,
|
|
272
|
+
// and left as it is when it is one. The engine's compiled output is CommonJS already.
|
|
273
|
+
return !probe || parses(wrapped, filename) ? wrapped : null;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Run `compile` as the evaluation of `mod`, a support entry of `entryOf` when it is one. */
|
|
277
|
+
function asSupport(mod, entryOf, compile) {
|
|
278
|
+
// A support entry, however it is reached (`require`, `import`, `setupFiles`), builds the
|
|
279
|
+
// library's mocks, and owns what it registers.
|
|
280
|
+
if (entryOf === null) return compile();
|
|
281
|
+
const owner = { pkg: entryOf, module: mod };
|
|
282
|
+
entries.add(owner);
|
|
283
|
+
lib.building++;
|
|
284
|
+
owners.push(owner);
|
|
285
|
+
try {
|
|
286
|
+
return compile();
|
|
287
|
+
} finally {
|
|
288
|
+
owners.pop();
|
|
289
|
+
lib.building--;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Compile `content` (already read or compiled by the engine) as `mod`. */
|
|
294
|
+
export function compileSupport(mod, content, filename) {
|
|
295
|
+
const support = supportOf(filename);
|
|
296
|
+
if (support === null) return mod._compile(content, filename);
|
|
297
|
+
const wrapped = support.wantsJest ? withJest(content, filename, false) : null;
|
|
298
|
+
return asSupport(mod, support.entryOf, () => mod._compile(wrapped ?? content, filename));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Load `filename` as Node's own `.js` handler (`loadJs`) would. A support file Node loads
|
|
303
|
+
* as CommonJS and whose code needs a `jest` is read and compiled with it here; anything
|
|
304
|
+
* else, an ES module among them, is Node's.
|
|
305
|
+
*/
|
|
306
|
+
export function loadSupport(mod, filename, loadJs) {
|
|
307
|
+
const support = supportOf(filename);
|
|
308
|
+
if (support === null) return loadJs(mod, filename);
|
|
309
|
+
let wrapped = null;
|
|
310
|
+
if (support.wantsJest && !isModuleType(filename)) {
|
|
311
|
+
wrapped = withJest(fs.readFileSync(filename, "utf8").replace(/^\uFEFF/, ""), filename, true);
|
|
312
|
+
}
|
|
313
|
+
return asSupport(mod, support.entryOf, () =>
|
|
314
|
+
wrapped !== null ? mod._compile(wrapped, filename) : loadJs(mod, filename),
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** Whether Node loads `.js` in `filename`'s directory as an ES module (`"type": "module"`). */
|
|
319
|
+
function isModuleType(filename) {
|
|
320
|
+
if (filename.endsWith(".cjs")) return false;
|
|
321
|
+
if (filename.endsWith(".mjs")) return true;
|
|
322
|
+
for (let dir = path.dirname(filename); ; dir = path.dirname(dir)) {
|
|
323
|
+
if (moduleTypeByDir.has(dir)) return moduleTypeByDir.get(dir);
|
|
324
|
+
const manifest = path.join(dir, "package.json");
|
|
325
|
+
if (fs.existsSync(manifest)) {
|
|
326
|
+
let type;
|
|
327
|
+
try {
|
|
328
|
+
type = JSON.parse(fs.readFileSync(manifest, "utf8")).type;
|
|
329
|
+
} catch {
|
|
330
|
+
type = undefined;
|
|
331
|
+
}
|
|
332
|
+
const isModule = type === "module";
|
|
333
|
+
moduleTypeByDir.set(dir, isModule);
|
|
334
|
+
return isModule;
|
|
335
|
+
}
|
|
336
|
+
if (path.dirname(dir) === dir) return false;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
const moduleTypeByDir = (lib.moduleTypeByDir ??= new Map());
|
|
340
|
+
|
|
341
|
+
/** Whether `code` parses as a script; it is not run. */
|
|
342
|
+
function parses(code, filename) {
|
|
343
|
+
try {
|
|
344
|
+
return new vm.Script(code, { filename }) instanceof vm.Script;
|
|
345
|
+
} catch {
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function stripShebang(code) {
|
|
351
|
+
return code.startsWith("#!") ? code.replace(/^#!.*/, "") : code;
|
|
352
|
+
}
|