Skip to content

Commit

Permalink
fix(esm): resolve implicit extension in files containing .
Browse files Browse the repository at this point in the history
fixes #604
  • Loading branch information
privatenumber committed Jun 29, 2024
1 parent ae2a1bc commit c9c690b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
29 changes: 13 additions & 16 deletions src/utils/map-ts-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,26 @@ export const mapTsExtensions = (
filePath: string,
handleMissingExtension?: boolean,
) => {
const [pathname, search] = filePath.split('?');
const splitPath = filePath.split('?');
let [pathname] = splitPath;
const extension = path.extname(pathname);
const tryExtensions = (
extension
? tsExtensions[extension]
: (handleMissingExtension ? noExtension : undefined)
);

if (!tryExtensions) {
return;
}
let tryExtensions = tsExtensions[extension];
if (tryExtensions) {
pathname = pathname.slice(0, -extension.length);
} else {
if (!handleMissingExtension) {
return;
}

const extensionlessPath = (
extension
? pathname.slice(0, -extension.length)
: pathname
);
tryExtensions = noExtension;
}

return tryExtensions.map(
tsExtension => (
extensionlessPath
pathname
+ tsExtension
+ (search ? `?${search}` : '')
+ (splitPath[1] ? `?${splitPath[1]}` : '')
),
);
};
54 changes: 29 additions & 25 deletions tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,41 +174,45 @@ export const files = {
'value.mjs': 'export default 1',
},

'ts/index.ts': sourcemap.tag`
import assert from 'assert';
import type {Type} from 'resolved-by-tsc'
ts: {
'index.ts': sourcemap.tag`
import assert from 'assert';
import type {Type} from 'resolved-by-tsc'
interface Foo {}
interface Foo {}
type Foo = number
type Foo = number
declare module 'foo' {}
declare module 'foo' {}
enum BasicEnum {
Left,
Right,
}
enum BasicEnum {
Left,
Right,
}
enum NamedEnum {
SomeEnum = 'some-value',
}
enum NamedEnum {
SomeEnum = 'some-value',
}
export const a = BasicEnum.Left;
export const a = BasicEnum.Left;
export const b = NamedEnum.SomeEnum;
export const b = NamedEnum.SomeEnum;
export default function foo(): string {
return 'foo'
}
export default function foo(): string {
return 'foo'
}
// For "ts as tsx" test
const bar = <T>(value: T) => fn<T>();
// For "ts as tsx" test
const bar = <T>(value: T) => fn<T>();
${preserveName}
${sourcemap.test('ts')}
export const cjsContext = ${cjsContextCheck};
${tsCheck};
`,
${preserveName}
${sourcemap.test('ts')}
export const cjsContext = ${cjsContextCheck};
${tsCheck};
`,

'period.in.name.ts': 'export const periodInName = true as const',
},

// TODO: test resolution priority for files 'index.tsx` & 'index.tsx.ts` via 'index.tsx'

Expand Down
1 change: 1 addition & 0 deletions tests/specs/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
import './ts/index.jsx';
import './ts/index';
import './ts/';
import './ts/period.in.name';
// .jsx
import * as jsx from './jsx/index.jsx';
Expand Down

0 comments on commit c9c690b

Please sign in to comment.