@@ -9,6 +9,10 @@ const makeDir = require('make-dir');
9
9
const semver = require ( 'semver' ) ;
10
10
const colors = require ( './colors' ) ;
11
11
12
+ const stage4Path = require . resolve ( '../stage-4' ) ;
13
+ const syntaxObjectRestSpreadPath = require . resolve ( '@babel/plugin-syntax-object-rest-spread' ) ;
14
+ const transformTestFilesPath = require . resolve ( '@ava/babel-preset-transform-test-files' ) ;
15
+
12
16
function validate ( conf ) {
13
17
if ( conf === undefined || conf === null ) {
14
18
conf = 'default' ;
@@ -28,9 +32,6 @@ function validate(conf) {
28
32
return conf ;
29
33
}
30
34
31
- const SOURCE = '(AVA) Base Babel config' ;
32
- const AVA_DIR = path . join ( __dirname , '..' ) ;
33
-
34
35
function verifyExistingOptions ( verifierFile , baseConfig , cache , envName ) {
35
36
return new Promise ( ( resolve , reject ) => {
36
37
try {
@@ -108,48 +109,75 @@ function build(projectDir, cacheDir, userOptions, compileEnhancements) {
108
109
const baseOptions = {
109
110
babelrc : false ,
110
111
plugins : [ ] ,
111
- presets : [ ]
112
+ presets : [
113
+ stage4Path // Always apply the stage-4 preset.
114
+ ]
112
115
} ;
113
- if ( compileEnhancements ) {
114
- baseOptions . presets . push ( [ '@ava/transform-test-files' , { powerAssert : true } ] ) ;
116
+
117
+ // By default extend the project's Babel configuration, but allow this to be
118
+ // disabled in userOptions.
119
+ if ( userOptions === 'default' || userOptions === 'inherit' || userOptions . babelrc !== false ) {
120
+ baseOptions . babelrc = true ;
115
121
}
116
- if ( userOptions === 'default' ) {
117
- baseOptions . presets . unshift ( '@ava/stage-4' ) ;
122
+ if ( userOptions . extends ) {
123
+ baseOptions . extends = userOptions . extends ;
118
124
}
119
125
120
- // Include object rest spread support for node versions that support it
126
+ // Include object rest spread support for Node.js versions that support it
121
127
// natively.
122
- if ( userOptions === 'default' && semver . satisfies ( process . versions . node , '>= 8.3.0' ) ) {
123
- baseOptions . plugins . push ( '@babel/plugin-syntax-object-rest-spread' ) ;
128
+ if ( semver . satisfies ( process . versions . node , '>= 8.3.0' ) ) {
129
+ baseOptions . plugins . push ( syntaxObjectRestSpreadPath ) ;
124
130
}
125
131
126
132
const baseConfig = configManager . createConfig ( {
127
- dir : AVA_DIR , // Presets are resolved relative to this directory
133
+ dir : projectDir ,
128
134
fileType : 'JSON' ,
129
135
hash : md5Hex ( JSON . stringify ( baseOptions ) ) ,
130
136
options : baseOptions ,
131
- source : SOURCE
137
+ source : '(AVA) baseConfig'
132
138
} ) ;
133
139
134
- if ( userOptions !== 'default' ) {
135
- baseConfig . extend ( configManager . createConfig ( {
140
+ let intermediateConfig = baseConfig ;
141
+ if ( userOptions !== 'default' && userOptions !== 'inherit' ) {
142
+ // At this level, babelrc *must* be false.
143
+ const options = Object . assign ( { } , userOptions , { babelrc : false } ) ;
144
+ // Any extends option has been applied in baseConfig.
145
+ delete options . extends ;
146
+ intermediateConfig = configManager . createConfig ( {
147
+ dir : projectDir ,
148
+ fileType : 'JSON' ,
149
+ hash : md5Hex ( JSON . stringify ( options ) ) ,
150
+ options,
151
+ source : path . join ( projectDir , 'package.json' ) + '#ava.babel'
152
+ } ) ;
153
+ intermediateConfig . extend ( baseConfig ) ;
154
+ }
155
+
156
+ let finalConfig = intermediateConfig ;
157
+ if ( compileEnhancements ) {
158
+ finalConfig = configManager . createConfig ( {
136
159
dir : projectDir ,
137
- options : userOptions === 'inherit' ?
138
- { babelrc : true } :
139
- userOptions ,
140
- source : path . join ( projectDir , 'package.json' ) + '#ava.babel' ,
141
- hash : md5Hex ( JSON . stringify ( userOptions ) )
142
- } ) ) ;
160
+ fileType : 'JSON' ,
161
+ hash : '' , // This is deterministic, so no actual value necessary.
162
+ options : {
163
+ babelrc : false ,
164
+ presets : [
165
+ [ transformTestFilesPath , { powerAssert : true } ]
166
+ ]
167
+ } ,
168
+ source : '(AVA) compileEnhancements'
169
+ } ) ;
170
+ finalConfig . extend ( intermediateConfig ) ;
143
171
}
144
172
145
173
const cache = configManager . prepareCache ( ) ;
146
- return verifyExistingOptions ( verifierFile , baseConfig , cache , envName )
174
+ return verifyExistingOptions ( verifierFile , finalConfig , cache , envName )
147
175
. then ( cacheKeys => {
148
176
if ( cacheKeys ) {
149
177
return cacheKeys ;
150
178
}
151
179
152
- return resolveOptions ( baseConfig , cache , envName , optionsFile , verifierFile ) ;
180
+ return resolveOptions ( finalConfig , cache , envName , optionsFile , verifierFile ) ;
153
181
} )
154
182
. then ( cacheKeys => {
155
183
const getOptions = require ( optionsFile ) . getOptions ;
0 commit comments