Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions packages/core/src/runtime/worker/loadEsModule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { builtinModules } from 'node:module';
import { builtinModules, createRequire } from 'node:module';
import { isAbsolute } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import vm, { type SourceTextModule } from 'node:vm';
Expand All @@ -12,6 +12,8 @@ import {
shouldInterop,
} from './interop';

const importMetaResolve = import.meta.resolve?.bind(import.meta);

export enum EsmMode {
Unknown = 0,
Evaluated = 1,
Expand All @@ -28,6 +30,23 @@ const isRelativePath = (p: string) => /^\.\.?\//.test(p);
const isBuiltinSpecifier = (specifier: string) =>
specifier.startsWith('node:') || builtinModules.includes(specifier);

const resolveModule = (
specifier: string,
resolveBase: string,
): string | URL => {
const parentURL = resolveBase.startsWith('file:')
? resolveBase
: pathToFileURL(resolveBase).href;

if (!importMetaResolve) {
return pathToFileURL(createRequire(parentURL).resolve(specifier)).href;
}

// Node's loader hook worker clones the parent URL when native TypeScript
// loading is active. Passing URL objects can throw DataCloneError there.
return importMetaResolve(specifier, parentURL);
};

export const appendSourceURL = (
codeContent: string,
sourceUrl: string,
Expand Down Expand Up @@ -117,10 +136,10 @@ const defineRstestDynamicImport =
// (which have no origin to pass) working as before.
const resolveBase = origin ?? testPath;
const resolvedPath = isAbsolute(specifier)
? pathToFileURL(specifier)
? pathToFileURL(specifier).href
: isBuiltinSpecifier(specifier)
? specifier
: import.meta.resolve(specifier, pathToFileURL(resolveBase));
: resolveModule(specifier, resolveBase);

// Use `.href` (full file:// URL) rather than `.pathname` so absolute
// Windows specifiers (`D:\a\foo.mjs`) remain valid import targets. With
Expand Down Expand Up @@ -262,7 +281,11 @@ export const loadModule = async ({
interopDefault,
returnModule: true,
esmMode: EsmMode.Unlinked,
})(specifier, referencingModule as ImportCallOptions),
})(
specifier,
{},
isRelativePath(specifier) ? referencingModule.identifier : undefined,
),
);
}

Expand Down
14 changes: 12 additions & 2 deletions packages/core/src/runtime/worker/loadModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ import {
shouldInterop,
} from './interop';

const importMetaResolve = import.meta.resolve;

const isRelativePath = (p: string) => /^\.\.?\//.test(p);

const resolveModule = (specifier: string, resolveBase: string): string | URL =>
importMetaResolve(
specifier,
resolveBase.startsWith('file:')
? resolveBase
: pathToFileURL(resolveBase).href,
);

const createRequire = (
filename: string,
distPath: string,
Expand Down Expand Up @@ -88,8 +98,8 @@ const defineRstestDynamicImport =
// to pass) working as before.
const resolveBase = origin ?? testPath;
const resolvedPath = isAbsolute(specifier)
? pathToFileURL(specifier)
: import.meta.resolve(specifier, pathToFileURL(resolveBase));
? pathToFileURL(specifier).href
: resolveModule(specifier, resolveBase);

// Use `.href` rather than `.pathname` so Windows absolute specifiers
// round-trip through Node's ESM loader as valid `file:///D:/...` URLs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 'fixture-pkg-value';
7 changes: 7 additions & 0 deletions packages/core/tests/runner/fixtures/bare-parent/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "bare-parent",
"type": "module",
"imports": {
"#fixture-pkg": "./bare-parent-pkg/index.mjs"
}
}
3 changes: 3 additions & 0 deletions packages/core/tests/runner/fixtures/nativeTsLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
base: import.meta.dirname,
};
37 changes: 37 additions & 0 deletions packages/core/tests/runner/loadEsModule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,43 @@ describe('loadEsModule', () => {
expect(mod.default.bar).toBe('bar-val');
});

it('should resolve external ESM after Node native TypeScript loader is used', async () => {
await import(fixturePath('nativeTsLoader.ts'));

const mod = await loadModule({
codeContent: [
`import { foo } from ${JSON.stringify(fixturePath('namedOnly.mjs'))};`,
'export default foo;',
].join('\n'),
distPath: '/virtual/dist/entry.mjs',
testPath: __filename,
rstestContext: {},
assetFiles: {},
interopDefault: false,
});

expect(mod.default).toBe('foo-val');
});

it('should resolve bare static imports from the test path', async () => {
const testPath = fixturePath('bare-parent/index.test.ts');
const distPath = '/virtual/dist/.rstest-temp/bare-parent_index~test~ts.mjs';

const mod = await loadModule({
codeContent: [
"import { value } from '#fixture-pkg';",
'export default value;',
].join('\n'),
distPath,
testPath,
rstestContext: {},
assetFiles: {},
interopDefault: false,
});

expect(mod.default).toBe('fixture-pkg-value');
});

it('should preserve the real default export of a real native ESM module', async () => {
const mod = await loadModule({
codeContent: [
Expand Down
Loading