Skip to content

Commit

Permalink
fix: import implicit extensions from packages
Browse files Browse the repository at this point in the history
fixes #542
  • Loading branch information
privatenumber committed May 6, 2024
1 parent 3a0ea18 commit 8022fcf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
69 changes: 33 additions & 36 deletions src/esm/hook/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,53 +127,50 @@ export const resolve: resolve = async (
|| isRelativePathPattern.test(specifier)
);

// bare specifier
if (!isPath) {
// TS path alias
if (
tsconfigPathsMatcher
&& !context.parentURL?.includes('/node_modules/')
) {
const possiblePaths = tsconfigPathsMatcher(specifier);
for (const possiblePath of possiblePaths) {
try {
return await resolve(
pathToFileURL(possiblePath).toString(),
context,
nextResolve,
);
} catch {}
if (isPath) {
// Inherit namespace from parent
let requestNamespace = getNamespace(specifier);
if (context.parentURL) {
const parentNamespace = getNamespace(context.parentURL);
if (parentNamespace && !requestNamespace) {
requestNamespace = parentNamespace;
specifier += `${specifier.includes('?') ? '&' : '?'}${namespaceQuery}${parentNamespace}`;
}
}

// npm package -- use default resolution
return nextResolve(specifier, context);
}

// Inherit namespace from parent
let requestNamespace = getNamespace(specifier);
if (context.parentURL) {
const parentNamespace = getNamespace(context.parentURL);
if (parentNamespace && !requestNamespace) {
requestNamespace = parentNamespace;
specifier += `${specifier.includes('?') ? '&' : '?'}${namespaceQuery}${parentNamespace}`;
if (data.namespace && data.namespace !== requestNamespace) {
return nextResolve(specifier, context);
}
}

if (data.namespace && data.namespace !== requestNamespace) {
return nextResolve(specifier, context);
}

// If directory, can be index.js, index.ts, etc.
if (isDirectoryPattern.test(specifier)) {
return await tryDirectory(specifier, context, nextResolve);
// If directory, can be index.js, index.ts, etc.
if (isDirectoryPattern.test(specifier)) {
return await tryDirectory(specifier, context, nextResolve);
}
} else if ( // Bare specifier
// TS path alias
tsconfigPathsMatcher
&& !context.parentURL?.includes('/node_modules/')
) {
const possiblePaths = tsconfigPathsMatcher(specifier);
for (const possiblePath of possiblePaths) {
try {
return await resolve(
pathToFileURL(possiblePath).toString(),
context,
nextResolve,
);
} catch {}
}
}

// Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
//
// If `allowJs` is set in `tsconfig.json`, then we'll apply the same resolution logic
// to files without a TypeScript extension.
if (tsExtensionsPattern.test(context.parentURL!) || allowJs) {
if (
tsExtensionsPattern.test(context.parentURL!)
|| allowJs
) {
const tsPaths = resolveTsPath(specifier);
if (tsPaths) {
for (const tsPath of tsPaths) {
Expand Down
7 changes: 4 additions & 3 deletions tests/specs/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ const files = {
'pkg-module': {
'package.json': JSON.stringify({
type: 'module',
exports: './index.js',
main: './index.js',
}),
'index.js': `${syntaxLowering}\nexport * from "./empty-export.js"`,
'empty-export.js': 'export {}',
'index.js': `${syntaxLowering}\nexport * from "./empty-export"`,
'empty-export/index.js': 'export {}',
},
},

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

0 comments on commit 8022fcf

Please sign in to comment.