Skip to content

Commit

Permalink
fix(tsImport): resolve specifiers that are absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
fardjad committed May 5, 2024
1 parent b407435 commit 5a5ca97
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/esm/hook/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export const resolve: resolve = async (
const isPath = (
specifier.startsWith(fileProtocol)
|| isRelativePathPattern.test(specifier)
|| path.isAbsolute(specifier)
);

// bare specifier
Expand Down
28 changes: 28 additions & 0 deletions tests/specs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,34 @@ export default testSuite(({ describe }, node: NodeApis) => {
expect(stdout).toBe('Fails as expected 1\nfoo bar\nFails as expected 2');
});

test('import from an absolute path', async ({ onTestFinish }) => {
const fixture = await createFixture({
'package.json': JSON.stringify({ type: 'module' }),
'register.mjs': `
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { register } from ${JSON.stringify(tsxEsmApiPath)};
const unregister = register();
const __dirname = path.dirname(fileURLToPath(new URL(import.meta.url)));
const moduleAbsolutePath = path.join(__dirname, './file.ts');
const { message } = await import(moduleAbsolutePath);
console.log(message);
await unregister();
`,
...tsFiles,
});
onTestFinish(async () => await fixture.rm());

const { stdout } = await execaNode(path.join(fixture.path, 'register.mjs'), [], {
nodePath: node.path,
nodeOptions: [],
});
expect(stdout).toBe('foo bar');
});

test('onImport', async ({ onTestFinish }) => {
const fixture = await createFixture({
'package.json': JSON.stringify({ type: 'module' }),
Expand Down

0 comments on commit 5a5ca97

Please sign in to comment.