Skip to content

Commit 8022fcf

Browse files
committedMay 6, 2024·
fix: import implicit extensions from packages
fixes #542
1 parent 3a0ea18 commit 8022fcf

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed
 

‎src/esm/hook/resolve.ts

+33-36
Original file line numberDiff line numberDiff line change
@@ -127,53 +127,50 @@ export const resolve: resolve = async (
127127
|| isRelativePathPattern.test(specifier)
128128
);
129129

130-
// bare specifier
131-
if (!isPath) {
132-
// TS path alias
133-
if (
134-
tsconfigPathsMatcher
135-
&& !context.parentURL?.includes('/node_modules/')
136-
) {
137-
const possiblePaths = tsconfigPathsMatcher(specifier);
138-
for (const possiblePath of possiblePaths) {
139-
try {
140-
return await resolve(
141-
pathToFileURL(possiblePath).toString(),
142-
context,
143-
nextResolve,
144-
);
145-
} catch {}
130+
if (isPath) {
131+
// Inherit namespace from parent
132+
let requestNamespace = getNamespace(specifier);
133+
if (context.parentURL) {
134+
const parentNamespace = getNamespace(context.parentURL);
135+
if (parentNamespace && !requestNamespace) {
136+
requestNamespace = parentNamespace;
137+
specifier += `${specifier.includes('?') ? '&' : '?'}${namespaceQuery}${parentNamespace}`;
146138
}
147139
}
148140

149-
// npm package -- use default resolution
150-
return nextResolve(specifier, context);
151-
}
152-
153-
// Inherit namespace from parent
154-
let requestNamespace = getNamespace(specifier);
155-
if (context.parentURL) {
156-
const parentNamespace = getNamespace(context.parentURL);
157-
if (parentNamespace && !requestNamespace) {
158-
requestNamespace = parentNamespace;
159-
specifier += `${specifier.includes('?') ? '&' : '?'}${namespaceQuery}${parentNamespace}`;
141+
if (data.namespace && data.namespace !== requestNamespace) {
142+
return nextResolve(specifier, context);
160143
}
161-
}
162-
163-
if (data.namespace && data.namespace !== requestNamespace) {
164-
return nextResolve(specifier, context);
165-
}
166144

167-
// If directory, can be index.js, index.ts, etc.
168-
if (isDirectoryPattern.test(specifier)) {
169-
return await tryDirectory(specifier, context, nextResolve);
145+
// If directory, can be index.js, index.ts, etc.
146+
if (isDirectoryPattern.test(specifier)) {
147+
return await tryDirectory(specifier, context, nextResolve);
148+
}
149+
} else if ( // Bare specifier
150+
// TS path alias
151+
tsconfigPathsMatcher
152+
&& !context.parentURL?.includes('/node_modules/')
153+
) {
154+
const possiblePaths = tsconfigPathsMatcher(specifier);
155+
for (const possiblePath of possiblePaths) {
156+
try {
157+
return await resolve(
158+
pathToFileURL(possiblePath).toString(),
159+
context,
160+
nextResolve,
161+
);
162+
} catch {}
163+
}
170164
}
171165

172166
// Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
173167
//
174168
// If `allowJs` is set in `tsconfig.json`, then we'll apply the same resolution logic
175169
// to files without a TypeScript extension.
176-
if (tsExtensionsPattern.test(context.parentURL!) || allowJs) {
170+
if (
171+
tsExtensionsPattern.test(context.parentURL!)
172+
|| allowJs
173+
) {
177174
const tsPaths = resolveTsPath(specifier);
178175
if (tsPaths) {
179176
for (const tsPath of tsPaths) {

‎tests/specs/smoke.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ const files = {
238238
'pkg-module': {
239239
'package.json': JSON.stringify({
240240
type: 'module',
241-
exports: './index.js',
241+
main: './index.js',
242242
}),
243-
'index.js': `${syntaxLowering}\nexport * from "./empty-export.js"`,
244-
'empty-export.js': 'export {}',
243+
'index.js': `${syntaxLowering}\nexport * from "./empty-export"`,
244+
'empty-export/index.js': 'export {}',
245245
},
246246
},
247247

@@ -357,6 +357,7 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
357357
358358
import * as pkgCommonjs from 'pkg-commonjs';
359359
import * as pkgModule from 'pkg-module';
360+
import 'pkg-module/empty-export'; // implicit directory & extension
360361
361362
// .js
362363
import * as js from './js/index.js';

0 commit comments

Comments
 (0)
Please sign in to comment.