Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer conditional exports #618

Closed
Closed
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
6 changes: 5 additions & 1 deletion src/utils/map-ts-extensions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import path from 'node:path';

const noExtension = ['.js', '.json', '.ts', '.tsx', '.jsx'];
// When the file extension is missing in the requested path, always try the
// file path verbatim before trying to resolve it with specific extensions.
// This ensures that we give Node a chance to take `"exports"` in `package.json`
// into account.
const noExtension = ['', '.js', '.json', '.ts', '.tsx', '.jsx'];

const tsExtensions: Record<string, string[]> = Object.create(null);
tsExtensions['.js'] = ['.ts', '.tsx', '.js', '.jsx'];
Expand Down
30 changes: 30 additions & 0 deletions tests/specs/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,36 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
expect(p.failed).toBe(false);
});

test('prefer conditional exports over specific extensions', async () => {
await using fixture = await createFixture({
'package.json': createPackageJson({ type: packageType }),
'index.ts': `
import A from 'pkg/conditional/export'
console.log(A)
`,
'node_modules/pkg': {
'package.json': createPackageJson({
name: 'pkg',
exports: {
'./*': './*',
'./conditional/export': {
require: './conditional/export.js',
default: './conditional/export.mjs',
},
},
}),
'conditional/export.js': 'module.exports = 1',
'conditional/export.mjs': 'export default 2',
},
});

const p = await tsx(['index.ts'], {
cwd: fixture.path,
});
expect(p.failed).toBe(false);
expect(p.stdout).toMatch(isCommonJs ? '1' : '2');
});

/**
* Node v18 has a bug:
* Error [ERR_INTERNAL_ASSERTION]:
Expand Down
Loading