@@ -7,49 +7,78 @@ function startsWith(string: string, prefix: string): boolean {
7
7
return string . lastIndexOf ( prefix , 0 ) === 0 ;
8
8
}
9
9
10
+ type Preset = any ;
11
+
10
12
type BabelConfig = {
11
- filename ?: string ,
12
- filenameRelative ?: string ,
13
- presets ?: Array < string > ,
14
- plugins ?: Array < string > ,
15
- highlightCode ?: boolean ,
16
- only ?: ?( RegExp | string | Array < RegExp | string > ) ,
17
- ignore ?: ?( RegExp | string | Array < RegExp | string > ) ,
18
- auxiliaryCommentBefore ?: ?string ,
19
- auxiliaryCommentAfter ?: ?string ,
20
- sourceMaps ?: boolean ,
21
- inputSourceMap ?: ?Object ,
22
- sourceMapTarget ?: string ,
23
- sourceFileName ? : string ,
24
- sourceRoot ? : string ,
25
- moduleRoot ? : string ,
26
- moduleIds ? : boolean ,
27
- moduleId ? : string ,
28
- getModuleId ?: ( moduleName : string ) => ?string ,
29
- resolveModuleSource ?: ( source : string , filename : string ) => ?string ,
30
- code ?: boolean ,
31
- babelrc ?: boolean ,
32
- ast ?: boolean ,
33
- compact ?: boolean | 'auto' ,
34
- comments ?: boolean ,
35
- shouldPrintComment ?: ( commentContents : string ) => boolean ,
36
- env ?: { [ key : string ] : BabelConfig } ,
37
- retainLines ?: boolean ,
38
- extends ?: ?string ,
13
+ filename ?: string ;
14
+ filenameRelative ? : string ;
15
+ presets ?: Array < Preset > ;
16
+ plugins ?: Array < string > ;
17
+ highlightCode ? : boolean ;
18
+ only ?: ?( RegExp | string | Array < RegExp | string > ) ;
19
+ ignore ?: ?( RegExp | string | Array < RegExp | string > ) ;
20
+ auxiliaryCommentBefore ?: ?string ;
21
+ auxiliaryCommentAfter ?: ?string ;
22
+ sourceMaps ?: boolean ;
23
+ inputSourceMap ?: ?Object ;
24
+ sourceMapTarget ?: string ;
25
+ sourceFileName ?: string ;
26
+ sourceRoot ?: string ;
27
+ moduleRoot ?: string ;
28
+ moduleIds ?: boolean ;
29
+ moduleId ?: string ;
30
+ getModuleId ?: ( moduleName : string ) => ?string ;
31
+ resolveModuleSource ?: ( source : string , filename : string ) => ?string ;
32
+ code ?: boolean ;
33
+ babelrc ?: boolean ;
34
+ ast ?: boolean ;
35
+ compact ?: boolean | 'auto' ;
36
+ comments ?: boolean ;
37
+ shouldPrintComment ?: ( commentContents : string ) => boolean ;
38
+ env ?: { [ key : string ] : BabelConfig } ;
39
+ retainLines ?: boolean ;
40
+ extends ?: ?string ;
41
+ } ;
42
+
43
+ type Options = {
44
+ path ?: string ;
45
+ config ?: BabelConfig ;
46
+ findRollupPresets ?: boolean ;
47
+ addModuleOptions ?: boolean ;
48
+ addExternalHelpersPlugin ?: boolean ;
49
+ resolve ?: ( path : string ) => string ;
39
50
} ;
40
51
41
- export default function babelrc ( path : string = '.babelrc' ) : BabelConfig {
42
- return configWithoutModules ( JSON . parse ( readFileSync ( path , { encoding : 'utf8' } ) ) ) ;
52
+ const DEFAULT_OPTIONS = {
53
+ path : '.babelrc' ,
54
+ findRollupPresets : false ,
55
+ addModuleOptions : true ,
56
+ addExternalHelpersPlugin : true ,
57
+ resolve
58
+ } ;
59
+
60
+ export default function babelrc ( options : Options | string = { } ) : BabelConfig {
61
+ if ( typeof options === 'string' ) {
62
+ options = { path : options } ;
63
+ }
64
+
65
+ let resolvedOptions = { ...DEFAULT_OPTIONS , ...options } ;
66
+
67
+ if ( ! resolvedOptions . config && typeof resolvedOptions . path === 'string' ) {
68
+ resolvedOptions . config = JSON . parse ( readFileSync ( resolvedOptions . path , { encoding : 'utf8' } ) ) ;
69
+ }
70
+
71
+ return configWithoutModules ( resolvedOptions . config , resolvedOptions ) ;
43
72
}
44
73
45
- export function configWithoutModules ( config : BabelConfig ) : BabelConfig {
74
+ export function configWithoutModules ( config : BabelConfig , options : Options ) : BabelConfig {
46
75
let result = { } ;
47
76
48
77
for ( let key in config ) {
49
78
if ( Object . prototype . hasOwnProperty . call ( config , key ) ) {
50
79
if ( key === 'presets' && config . presets ) {
51
80
// Replace the es2015 preset with the es2015-rollup preset.
52
- result . presets = config . presets . map ( mapPreset ) ;
81
+ result . presets = config . presets . map ( preset => mapPreset ( preset , options ) ) ;
53
82
} else if ( key === 'plugins' && config . plugins ) {
54
83
// Remove any explicit module plugins, e.g. es2015-modules-commonjs.
55
84
result . plugins = config . plugins . filter (
@@ -61,18 +90,57 @@ export function configWithoutModules(config: BabelConfig): BabelConfig {
61
90
}
62
91
}
63
92
93
+ if ( options . addExternalHelpersPlugin ) {
94
+ if ( ! result . plugins ) {
95
+ result . plugins = [ 'external-helpers' ] ;
96
+ } else {
97
+ result . plugins = [ ...result . plugins , 'external-helpers' ] ;
98
+ }
99
+ }
100
+
64
101
// Make sure babel does not look for the babelrc file.
65
102
result . babelrc = false ;
66
103
67
104
return result ;
68
105
}
69
106
70
- function mapPreset ( preset : string ) : string {
107
+ function mapPreset ( preset : any , options : Options ) : any {
108
+ let info = getPresetInfo ( preset ) ;
109
+
110
+ if ( ! info ) {
111
+ return preset ;
112
+ }
113
+
114
+ if ( options . findRollupPresets && hasRollupVersionOfPreset ( info . name , options . resolve || resolve ) ) {
115
+ return [ `${ info . name } -rollup` , info . options ] ;
116
+ } else if ( options . addModuleOptions ) {
117
+ return [ info . name , { ...info . options , modules : false } ] ;
118
+ } else {
119
+ return preset ;
120
+ }
121
+ }
122
+
123
+ function getPresetInfo ( preset : any ) : ?{ name : string , options : Object } {
124
+ if ( typeof preset === 'string' ) {
125
+ return { name : preset , options : { } } ;
126
+ } else if ( Array . isArray ( preset ) ) {
127
+ let name = preset [ 0 ] ;
128
+ let options = preset [ 1 ] || { } ;
129
+
130
+ if ( typeof name === 'string' && typeof options === 'object' ) {
131
+ return { name , options } ;
132
+ }
133
+ }
134
+
135
+ return null ;
136
+ }
137
+
138
+ function hasRollupVersionOfPreset ( preset : string , resolve : ( path : string ) = > string ) : boolean {
71
139
try {
72
140
// this will throw if it can't resolve it
73
141
resolve ( `babel-preset-${ preset } -rollup` ) ;
74
- return ` ${ preset } -rollup` ;
142
+ return true ;
75
143
} catch ( err ) {
76
- return preset ;
144
+ return false ;
77
145
}
78
146
}
0 commit comments