1
1
import { FeatureFlags } from '../../../../feature_flags.js'
2
2
import { ModuleFileExtension , ModuleFormat } from '../../utils/module_format.js'
3
- import {
4
- DEFAULT_NODE_VERSION ,
5
- getNodeSupportMatrix ,
6
- NodeVersionString ,
7
- ShortNodeVersionString ,
8
- } from '../../utils/node_version.js'
3
+ import { DEFAULT_NODE_VERSION , getNodeSupportMatrix , parseVersion } from '../../utils/node_version.js'
9
4
import { getClosestPackageJson } from '../../utils/package_json.js'
10
5
11
6
const versionMap = {
12
- '8.x' : 'node8' ,
13
- '10.x' : 'node10' ,
14
- '12.x' : 'node12' ,
15
- '14.x' : 'node14' ,
16
- '16.x' : 'node16' ,
17
- '18.x' : 'node18' ,
7
+ 14 : 'node14' ,
8
+ 16 : 'node16' ,
9
+ 18 : 'node18' ,
18
10
} as const
19
11
20
12
type VersionValues = ( typeof versionMap ) [ keyof typeof versionMap ]
21
13
22
- const getBundlerTarget = ( suppliedVersion ?: NodeVersionString ) : VersionValues => {
23
- const version = normalizeVersion ( suppliedVersion )
14
+ const getBundlerTarget = ( suppliedVersion ?: string ) : VersionValues => {
15
+ const version = parseVersion ( suppliedVersion )
24
16
25
17
if ( version && version in versionMap ) {
26
- return versionMap [ version ]
18
+ return versionMap [ version as keyof typeof versionMap ]
27
19
}
28
20
29
- return versionMap [ ` ${ DEFAULT_NODE_VERSION } .x` ]
21
+ return versionMap [ DEFAULT_NODE_VERSION ]
30
22
}
31
23
32
24
const getModuleFormat = async (
@@ -35,20 +27,22 @@ const getModuleFormat = async (
35
27
extension : string ,
36
28
configVersion ?: string ,
37
29
) : Promise < { includedFiles : string [ ] ; moduleFormat : ModuleFormat } > => {
38
- if ( extension === ModuleFileExtension . MJS && featureFlags . zisi_pure_esm_mjs ) {
30
+ if ( featureFlags . zisi_pure_esm_mjs && extension === ModuleFileExtension . MJS ) {
39
31
return {
40
32
includedFiles : [ ] ,
41
33
moduleFormat : ModuleFormat . ESM ,
42
34
}
43
35
}
44
36
45
- const packageJsonFile = await getClosestPackageJson ( srcDir )
46
- const nodeSupport = getNodeSupportMatrix ( configVersion )
37
+ if ( featureFlags . zisi_pure_esm ) {
38
+ const packageJsonFile = await getClosestPackageJson ( srcDir )
39
+ const nodeSupport = getNodeSupportMatrix ( configVersion )
47
40
48
- if ( featureFlags . zisi_pure_esm && packageJsonFile ?. contents . type === 'module' && nodeSupport . esm ) {
49
- return {
50
- includedFiles : [ packageJsonFile . path ] ,
51
- moduleFormat : ModuleFormat . ESM ,
41
+ if ( packageJsonFile ?. contents . type === 'module' && nodeSupport . esm ) {
42
+ return {
43
+ includedFiles : [ packageJsonFile . path ] ,
44
+ moduleFormat : ModuleFormat . ESM ,
45
+ }
52
46
}
53
47
}
54
48
@@ -58,10 +52,4 @@ const getModuleFormat = async (
58
52
}
59
53
}
60
54
61
- const normalizeVersion = ( version ?: NodeVersionString ) : ShortNodeVersionString | undefined => {
62
- const match = version && ( version . match ( / ^ n o d e j s ( .* ) $ / ) as [ string , ShortNodeVersionString ] )
63
-
64
- return match ? match [ 1 ] : ( version as ShortNodeVersionString )
65
- }
66
-
67
55
export { getBundlerTarget , getModuleFormat }
1 commit comments
github-actions[bot] commentedon Apr 11, 2023
⏱ Benchmark results