1
1
const path = require ( 'path' )
2
- const qs = require ( 'querystring' )
3
2
const Joi = require ( 'joi' )
4
3
const micromatch = require ( 'micromatch' )
5
4
const union = require ( 'lodash.union' )
@@ -28,7 +27,8 @@ module.exports = class Config {
28
27
// merges API options into app.js options
29
28
let allOpts = merge ( this . parseAppJs ( opts ) , opts )
30
29
this . transformSpikeOptionsToWebpack ( this . validateOpts ( allOpts ) )
31
- this . project = project
30
+ const sp = this . plugins . find ( ( p ) => p . name === 'spikePlugin' )
31
+ sp . options . project = project
32
32
}
33
33
34
34
/**
@@ -56,13 +56,14 @@ module.exports = class Config {
56
56
value : Joi . array ( ) . items ( Joi . string ( ) ) . single ( )
57
57
} ) . default ( { 'js/main' : [ './assets/js/index.js' ] } ) ,
58
58
vendor : Joi . array ( ) . single ( ) ,
59
- modulesDirectories : Joi . array ( ) . default ( [ 'node_modules' , 'bower_components' ] ) ,
60
59
outputDir : Joi . string ( ) . default ( 'public' ) ,
60
+ outputPublicPath : Joi . string ( ) ,
61
61
plugins : Joi . array ( ) . default ( [ ] ) ,
62
62
afterSpikePlugins : Joi . array ( ) . default ( [ ] ) ,
63
63
module : Joi . object ( ) . default ( ) . keys ( {
64
- loaders : Joi . array ( ) . default ( [ ] )
64
+ rules : Joi . array ( ) . default ( [ ] )
65
65
} ) ,
66
+ devServer : Joi . func ( ) ,
66
67
server : Joi . object ( ) . default ( ) . keys ( {
67
68
watchOptions : Joi . object ( ) . default ( ) . keys ( {
68
69
ignored : Joi . array ( ) . default ( 'node_modules' )
@@ -136,22 +137,15 @@ module.exports = class Config {
136
137
return `!${ path . join ( p , i ) } `
137
138
} ) )
138
139
139
- // parse any extra postcss options out to be passed as querystrings
140
- const postcssDirectKeys = [ 'plugins' , 'parser' , 'stringifier' , 'syntax' ]
141
- res . postcssQuery = { }
142
- for ( const k in res . postcss ) {
143
- if ( postcssDirectKeys . indexOf ( k ) < 0 ) res . postcssQuery [ k ] = res . postcss [ k ]
144
- }
145
-
146
140
// catch newly added files, put through the pipeline
147
141
res . server . files = [ {
148
142
match : allWatchedFiles ,
149
143
fn : ( event , file ) => {
150
144
const util = new SpikeUtils ( this )
151
145
const f = path . join ( this . context , file . replace ( p , '' ) )
152
- const files = this . spike . files . all
153
- if ( files . indexOf ( f ) < 0 && ! util . isFileIgnored ( f ) && event !== 'addDir' ) {
154
- this . project . watcher . watch ( [ ] , [ ] , [ f ] )
146
+ const opts = util . getSpikeOptions ( )
147
+ if ( opts . files . all . indexOf ( f ) < 0 && ! util . isFileIgnored ( f ) && event !== 'addDir' ) {
148
+ opts . project . watcher . watch ( [ ] , [ ] , [ f ] )
155
149
}
156
150
}
157
151
} ]
@@ -167,20 +161,20 @@ module.exports = class Config {
167
161
*/
168
162
transformSpikeOptionsToWebpack ( opts ) {
169
163
// `disallow` options would break spike if modified.
170
- const disallow = [ 'output' , 'resolveLoader' , 'spike' , 'plugins' , 'context' ]
164
+ const disallow = [ 'output' , 'resolveLoader' , 'spike' , 'plugins' , 'afterSpikePlugins' , ' context' , 'outputPublicPath ']
171
165
172
166
// `noCopy` options are spike-specific and shouldn't be directly added to
173
167
// webpack's config
174
- const noCopy = [ 'root' , 'matchers' , 'env' , 'server' , 'cleanUrls' , 'dumpDirs' , 'ignore' , 'vendor' , 'outputDir' , 'css' , 'postcssQuery ' ]
168
+ const noCopy = [ 'root' , 'matchers' , 'env' , 'server' , 'cleanUrls' , 'dumpDirs' , 'ignore' , 'vendor' , 'outputDir' , 'css' , 'postcss' , 'reshape' , 'babel ']
175
169
176
170
// All options other than `disallow` or `noCopy` are added directly to
177
171
// webpack's config object
178
172
const filteredOpts = removeKeys ( opts , disallow . concat ( noCopy ) )
179
173
Object . assign ( this , filteredOpts )
180
174
181
175
// `noCopy` options are added under the `spike` property
182
- this . spike = { files : { } }
183
- Object . assign ( this . spike , filterKeys ( opts , noCopy ) )
176
+ const spike = { files : { } }
177
+ Object . assign ( spike , filterKeys ( opts , noCopy ) )
184
178
185
179
// Now we run some spike-specific config transforms
186
180
this . context = opts . root
@@ -190,9 +184,15 @@ module.exports = class Config {
190
184
filename : '[name].js'
191
185
}
192
186
187
+ // this is sometimes necessary for webpackjsonp loads in old browsers
188
+ if ( opts . outputPublicPath ) {
189
+ this . output . publicPath = opts . outputPublicPath
190
+ }
191
+
193
192
this . resolveLoader = {
194
- root : [
193
+ modules : [
195
194
path . join ( opts . root ) , // the project root
195
+ path . join ( opts . root , 'node_modules' ) , // the project node_modules
196
196
path . join ( __dirname , '../node_modules' ) , // spike/node_modules
197
197
path . join ( __dirname , '../../../node_modules' ) // spike's flattened deps, via npm 3+
198
198
]
@@ -208,34 +208,45 @@ module.exports = class Config {
208
208
const spikeLoaders = [
209
209
{
210
210
exclude : reIgnores ,
211
- loader : `source-loader!postcss-loader?${ qs . stringify ( opts . postcssQuery ) } ` ,
212
- _core : 'css'
211
+ test : '/core!css' ,
212
+ use : [
213
+ { loader : 'source-loader' } ,
214
+ { loader : 'postcss-loader' , options : opts . postcss }
215
+ ]
213
216
} , {
214
217
exclude : reIgnores ,
215
- loader : 'babel-loader' ,
216
- _core : 'js'
218
+ test : '/core!js' ,
219
+ use : [
220
+ { loader : 'babel-loader' , options : opts . babel }
221
+ ]
217
222
} , {
218
223
exclude : reIgnores ,
219
- loader : 'source-loader!reshape-loader' ,
220
- _core : 'html'
224
+ test : '/core!html' ,
225
+ use : [
226
+ { loader : 'source-loader' } ,
227
+ { loader : 'reshape-loader' , options : opts . reshape }
228
+ ]
221
229
} , {
222
230
exclude : reIgnores ,
223
- loader : 'source-loader' ,
224
- _core : 'static'
231
+ test : '/core!static' ,
232
+ use : [
233
+ { loader : 'source-loader' }
234
+ ]
225
235
}
226
236
]
227
237
228
- this . module . loaders = spikeLoaders . concat ( opts . module . loaders )
238
+ this . module . rules = spikeLoaders . concat ( opts . module . rules )
229
239
230
240
const util = new SpikeUtils ( this )
241
+ const spikePlugin = new SpikePlugin ( util , spike )
231
242
232
243
this . plugins = [
233
244
...opts . plugins ,
234
- new SpikePlugin ( util ) ,
245
+ spikePlugin ,
235
246
...opts . afterSpikePlugins ,
236
247
new BrowserSyncPlugin ( opts . server , { callback : ( _ , bs ) => {
237
248
if ( bs . utils . devIp . length ) {
238
- this . project . emit ( 'info' , `External IP: http://${ bs . utils . devIp [ 0 ] } :${ this . spike . server . port } ` )
249
+ spike . project . emit ( 'info' , `External IP: http://${ bs . utils . devIp [ 0 ] } :${ spike . server . port } ` )
239
250
}
240
251
} } )
241
252
]
0 commit comments