Skip to content

Commit a6f8f9f

Browse files
committed
refactor: getFormat to handle all formats
1 parent 796053a commit a6f8f9f

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed

src/esm/hook/utils.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,17 @@ import type { ModuleFormat } from 'node:module';
33
import { tsExtensions } from '../../utils/path-utils.js';
44
import { getPackageType } from './package-json.js';
55

6-
const getFormatFromExtension = (fileUrl: string): ModuleFormat | undefined => {
7-
[fileUrl] = fileUrl.split('?');
8-
9-
const extension = path.extname(fileUrl);
10-
11-
if (extension === '.mts') {
6+
export const getFormatFromFileUrl = (fileUrl: string) => {
7+
const { pathname } = new URL(fileUrl);
8+
const extension = path.extname(pathname);
9+
if (extension === '.mts' || extension === '.mjs') {
1210
return 'module';
1311
}
14-
15-
if (extension === '.cts') {
12+
if (extension === '.cts' || extension === '.cjs') {
1613
return 'commonjs';
1714
}
18-
};
1915

20-
export const getFormatFromFileUrl = (fileUrl: string) => {
21-
const format = getFormatFromExtension(fileUrl);
22-
23-
if (format) {
24-
return format;
25-
}
26-
27-
// ts, tsx, jsx
28-
const { pathname } = new URL(fileUrl);
29-
const extension = path.extname(pathname);
30-
if (tsExtensions.includes(extension) || extension === '.js') {
16+
if (extension === '.js' || tsExtensions.includes(extension)) {
3117
return getPackageType(fileUrl);
3218
}
3319
};

0 commit comments

Comments
 (0)