Skip to content

Commit 96bc596

Browse files
authoredMay 4, 2024
fix(tsImport): handle importing packages (#14)
1 parent 63631ac commit 96bc596

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed
 

‎src/esm/hook/resolve.ts

+29-22
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ export const resolve: resolve = async (
121121
return nextResolve(specifier, context);
122122
}
123123

124+
const isPath = (
125+
specifier.startsWith(fileProtocol)
126+
|| isRelativePathPattern.test(specifier)
127+
);
128+
129+
// bare specifier
130+
if (!isPath) {
131+
// TS path alias
132+
if (
133+
tsconfigPathsMatcher
134+
&& !context.parentURL?.includes('/node_modules/')
135+
) {
136+
const possiblePaths = tsconfigPathsMatcher(specifier);
137+
for (const possiblePath of possiblePaths) {
138+
try {
139+
return await resolve(
140+
pathToFileURL(possiblePath).toString(),
141+
context,
142+
nextResolve,
143+
);
144+
} catch {}
145+
}
146+
}
147+
148+
// npm package -- use default resolution
149+
return nextResolve(specifier, context);
150+
}
151+
152+
// Inherit namespace from parent
124153
let requestNamespace = getNamespace(specifier);
125154
if (context.parentURL) {
126155
const parentNamespace = getNamespace(context.parentURL);
@@ -139,28 +168,6 @@ export const resolve: resolve = async (
139168
return await tryDirectory(specifier, context, nextResolve);
140169
}
141170

142-
const isPath = (
143-
specifier.startsWith(fileProtocol)
144-
|| isRelativePathPattern.test(specifier)
145-
);
146-
147-
if (
148-
tsconfigPathsMatcher
149-
&& !isPath // bare specifier
150-
&& !context.parentURL?.includes('/node_modules/')
151-
) {
152-
const possiblePaths = tsconfigPathsMatcher(specifier);
153-
for (const possiblePath of possiblePaths) {
154-
try {
155-
return await resolve(
156-
pathToFileURL(possiblePath).toString(),
157-
context,
158-
nextResolve,
159-
);
160-
} catch {}
161-
}
162-
}
163-
164171
// Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
165172
//
166173
// If `allowJs` is set in `tsconfig.json`, then we'll apply the same resolution logic

‎tests/specs/api.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ const tsFiles = {
2020
import { bar } from './bar.js'
2121
export const foo = \`foo \${bar}\` as string
2222
`,
23-
'bar.ts': 'export const bar = "bar" as string',
23+
'bar.ts': 'export type A = 1; export { bar } from "pkg"',
24+
'node_modules/pkg': {
25+
'package.json': JSON.stringify({
26+
name: 'pkg',
27+
type: 'module',
28+
exports: './index.js',
29+
}),
30+
'index.js': 'export const bar = "bar"',
31+
},
2432
};
2533

2634
export default testSuite(({ describe }, node: NodeApis) => {
@@ -244,7 +252,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
244252
nodePath: node.path,
245253
nodeOptions: [],
246254
});
247-
expect(stdout).toBe('file.ts\nfoo.ts\nbar.ts');
255+
expect(stdout).toBe('file.ts\nfoo.ts\nbar.ts\nindex.js');
248256
});
249257
});
250258

0 commit comments

Comments
 (0)
Please sign in to comment.