This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
bundler_target.ts
58 lines (47 loc) · 1.61 KB
/
bundler_target.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
53
54
55
56
57
58
import { FeatureFlags } from '../../../../feature_flags'
import { ModuleFormat } from '../../utils/module_format'
import {
DEFAULT_NODE_VERSION,
getNodeSupportMatrix,
NodeVersionString,
ShortNodeVersionString,
} from '../../utils/node_version'
import { getClosestPackageJson } from '../../utils/package_json'
const versionMap = {
'8.x': 'node8',
'10.x': 'node10',
'12.x': 'node12',
'14.x': 'node14',
'16.x': 'node16',
} as const
type VersionValues = typeof versionMap[keyof typeof versionMap]
const getBundlerTarget = (suppliedVersion?: NodeVersionString): VersionValues => {
const version = normalizeVersion(suppliedVersion)
if (version && version in versionMap) {
return versionMap[version]
}
return versionMap[`${DEFAULT_NODE_VERSION}.x`]
}
const getModuleFormat = async (
srcDir: string,
featureFlags: FeatureFlags,
configVersion?: string,
): Promise<{ includedFiles: string[]; moduleFormat: ModuleFormat }> => {
const packageJsonFile = await getClosestPackageJson(srcDir)
const nodeSupport = getNodeSupportMatrix(configVersion)
if (featureFlags.zisi_pure_esm && packageJsonFile?.contents.type === 'module' && nodeSupport.esm) {
return {
includedFiles: [packageJsonFile.path],
moduleFormat: 'esm',
}
}
return {
includedFiles: [],
moduleFormat: 'cjs',
}
}
const normalizeVersion = (version?: NodeVersionString): ShortNodeVersionString | undefined => {
const match = version && (version.match(/^nodejs(.*)$/) as [string, ShortNodeVersionString])
return match ? match[1] : (version as ShortNodeVersionString)
}
export { getBundlerTarget, getModuleFormat }