Skip to content

Commit c9c690b

Browse files
committedJun 29, 2024
fix(esm): resolve implicit extension in files containing .
fixes #604
1 parent ae2a1bc commit c9c690b

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed
 

‎src/utils/map-ts-extensions.ts

+13-16
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,26 @@ export const mapTsExtensions = (
1212
filePath: string,
1313
handleMissingExtension?: boolean,
1414
) => {
15-
const [pathname, search] = filePath.split('?');
15+
const splitPath = filePath.split('?');
16+
let [pathname] = splitPath;
1617
const extension = path.extname(pathname);
17-
const tryExtensions = (
18-
extension
19-
? tsExtensions[extension]
20-
: (handleMissingExtension ? noExtension : undefined)
21-
);
2218

23-
if (!tryExtensions) {
24-
return;
25-
}
19+
let tryExtensions = tsExtensions[extension];
20+
if (tryExtensions) {
21+
pathname = pathname.slice(0, -extension.length);
22+
} else {
23+
if (!handleMissingExtension) {
24+
return;
25+
}
2626

27-
const extensionlessPath = (
28-
extension
29-
? pathname.slice(0, -extension.length)
30-
: pathname
31-
);
27+
tryExtensions = noExtension;
28+
}
3229

3330
return tryExtensions.map(
3431
tsExtension => (
35-
extensionlessPath
32+
pathname
3633
+ tsExtension
37-
+ (search ? `?${search}` : '')
34+
+ (splitPath[1] ? `?${splitPath[1]}` : '')
3835
),
3936
);
4037
};

‎tests/fixtures.ts

+29-25
Original file line numberDiff line numberDiff line change
@@ -174,41 +174,45 @@ export const files = {
174174
'value.mjs': 'export default 1',
175175
},
176176

177-
'ts/index.ts': sourcemap.tag`
178-
import assert from 'assert';
179-
import type {Type} from 'resolved-by-tsc'
177+
ts: {
178+
'index.ts': sourcemap.tag`
179+
import assert from 'assert';
180+
import type {Type} from 'resolved-by-tsc'
180181
181-
interface Foo {}
182+
interface Foo {}
182183
183-
type Foo = number
184+
type Foo = number
184185
185-
declare module 'foo' {}
186+
declare module 'foo' {}
186187
187-
enum BasicEnum {
188-
Left,
189-
Right,
190-
}
188+
enum BasicEnum {
189+
Left,
190+
Right,
191+
}
191192
192-
enum NamedEnum {
193-
SomeEnum = 'some-value',
194-
}
193+
enum NamedEnum {
194+
SomeEnum = 'some-value',
195+
}
195196
196-
export const a = BasicEnum.Left;
197+
export const a = BasicEnum.Left;
197198
198-
export const b = NamedEnum.SomeEnum;
199+
export const b = NamedEnum.SomeEnum;
199200
200-
export default function foo(): string {
201-
return 'foo'
202-
}
201+
export default function foo(): string {
202+
return 'foo'
203+
}
203204
204-
// For "ts as tsx" test
205-
const bar = <T>(value: T) => fn<T>();
205+
// For "ts as tsx" test
206+
const bar = <T>(value: T) => fn<T>();
206207
207-
${preserveName}
208-
${sourcemap.test('ts')}
209-
export const cjsContext = ${cjsContextCheck};
210-
${tsCheck};
211-
`,
208+
${preserveName}
209+
${sourcemap.test('ts')}
210+
export const cjsContext = ${cjsContextCheck};
211+
${tsCheck};
212+
`,
213+
214+
'period.in.name.ts': 'export const periodInName = true as const',
215+
},
212216

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

‎tests/specs/smoke.ts

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ export default testSuite(async ({ describe }, { tsx, supports, version }: NodeAp
268268
import './ts/index.jsx';
269269
import './ts/index';
270270
import './ts/';
271+
import './ts/period.in.name';
271272
272273
// .jsx
273274
import * as jsx from './jsx/index.jsx';

0 commit comments

Comments
 (0)
Please sign in to comment.