-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathextension.ts
52 lines (44 loc) · 995 Bytes
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { Format, PackageJson } from '../types';
import { logger } from './logger';
export const getDefaultExtension = (options: {
format: Format;
pkgJson?: PackageJson;
autoExtension: boolean;
}): {
jsExtension: string;
dtsExtension: string;
isModule?: boolean;
} => {
const { format, pkgJson, autoExtension } = options;
let jsExtension = '.js';
let dtsExtension = '.d.ts';
if (!autoExtension) {
return {
jsExtension,
dtsExtension,
};
}
if (!pkgJson) {
logger.warn(
'autoExtension configuration will not be applied due to read package.json failed',
);
return {
jsExtension,
dtsExtension,
};
}
const isModule = pkgJson.type === 'module';
if (isModule && format === 'cjs') {
jsExtension = '.cjs';
dtsExtension = '.d.cts';
}
if (!isModule && format === 'esm') {
jsExtension = '.mjs';
dtsExtension = '.d.mts';
}
return {
jsExtension,
dtsExtension,
isModule,
};
};