@@ -24,7 +24,6 @@ import nodeResolvePlugin from './plugins/node-resolve'
24
24
import configLoader from './config-loader'
25
25
import isExternal from './utils/is-external'
26
26
import getBanner from './utils/get-banner'
27
- import { BUILTIN_PLUGINS } from './constants'
28
27
import {
29
28
Options ,
30
29
Config ,
@@ -82,6 +81,9 @@ interface RollupConfigInput {
82
81
config : NormalizedConfig
83
82
}
84
83
84
+ type PluginFactory = ( opts : any ) => RollupPlugin
85
+ type GetPlugin = ( name : string ) => PluginFactory | Promise < PluginFactory >
86
+
85
87
export class Bundler {
86
88
rootDir : string
87
89
config : NormalizedConfig
@@ -170,20 +172,6 @@ export class Bundler {
170
172
assets,
171
173
config
172
174
} : RollupConfigInput ) : Promise < RollupConfigOutput > {
173
- const plugins : RollupPlugin [ ] = [ ]
174
-
175
- plugins . push (
176
- progressPlugin ( {
177
- title
178
- } )
179
- )
180
-
181
- // Handle .json file
182
- plugins . push ( require ( 'rollup-plugin-json' ) ( ) )
183
-
184
- // Handle hashbang
185
- plugins . push ( require ( 'rollup-plugin-hashbang' ) ( ) )
186
-
187
175
// Always minify if config.minify is truthy
188
176
// Otherwise infer by format
189
177
const minify =
@@ -205,145 +193,189 @@ export class Bundler {
205
193
rollupFormat === 'iife' ||
206
194
config . bundleNodeModules
207
195
208
- plugins . push (
209
- nodeResolvePlugin ( {
210
- rootDir : this . rootDir ,
211
- bundleNodeModules,
212
- externals : config . externals ,
213
- browser : config . output . target === 'browser'
214
- } )
215
- )
216
-
217
- // Add user-supplied plugins
218
- // Excluded our builtin ones
219
- for ( const name of Object . keys ( config . plugins ) ) {
220
- if ( ! BUILTIN_PLUGINS . includes ( name ) ) {
221
- const options = config . plugins [ name ]
222
- if ( options ) {
223
- let plugin = this . localRequire ( `rollup-plugin-${ name } ` )
224
- plugin = plugin . default || plugin
225
- plugins . push ( plugin ( typeof options === 'object' ? options : { } ) )
226
- }
227
- }
196
+ const pluginsOptions : { [ key : string ] : any } = {
197
+ progress :
198
+ config . plugins . progress !== false &&
199
+ merge (
200
+ {
201
+ title
202
+ } ,
203
+ config . plugins . progress
204
+ ) ,
205
+
206
+ json : config . plugins . json !== false && merge ( { } , config . plugins . json ) ,
207
+
208
+ hashbang :
209
+ config . plugins . hashbang !== false && merge ( { } , config . plugins . hashbang ) ,
210
+
211
+ 'node-resolve' :
212
+ config . plugins [ 'node-resolve' ] !== false &&
213
+ merge (
214
+ { } ,
215
+ {
216
+ rootDir : this . rootDir ,
217
+ bundleNodeModules,
218
+ externals : config . externals ,
219
+ browser : config . output . target === 'browser'
220
+ } ,
221
+ config . plugins [ 'node-resolve' ]
222
+ ) ,
223
+
224
+ postcss :
225
+ config . plugins . postcss !== false &&
226
+ merge (
227
+ {
228
+ extract : config . output . extractCSS !== false
229
+ } ,
230
+ config . plugins . postcss
231
+ ) ,
232
+
233
+ vue :
234
+ ( source . hasVue || config . plugins . vue ) &&
235
+ merge (
236
+ {
237
+ css : false
238
+ } ,
239
+ config . plugins . vue
240
+ ) ,
241
+
242
+ typescript2 :
243
+ ( source . hasTs || config . plugins . typescript2 ) &&
244
+ merge (
245
+ {
246
+ objectHashIgnoreUnknownHack : true ,
247
+ tsconfigOverride : {
248
+ compilerOptions : {
249
+ module : 'esnext'
250
+ }
251
+ }
252
+ } ,
253
+ config . plugins . typescript2
254
+ ) ,
255
+
256
+ babel :
257
+ config . plugins . babel !== false &&
258
+ merge (
259
+ {
260
+ exclude : 'node_modules/**' ,
261
+ extensions : [ '.js' , '.jsx' , '.mjs' , '.ts' , '.tsx' , '.vue' ] ,
262
+ babelrc : config . babel . babelrc ,
263
+ configFile : config . babel . configFile ,
264
+ presetOptions : config . babel
265
+ } ,
266
+ config . plugins . babel
267
+ ) ,
268
+
269
+ buble :
270
+ ( config . plugins . buble || config . babel . minimal ) &&
271
+ merge (
272
+ {
273
+ exclude : 'node_modules/**' ,
274
+ include : '**/*.{js,mjs,jsx,vue}' ,
275
+ transforms : {
276
+ modules : false ,
277
+ dangerousForOf : true ,
278
+ dangerousTaggedTemplateString : true
279
+ }
280
+ } ,
281
+ config . plugins . buble
282
+ ) ,
283
+
284
+ commonjs :
285
+ config . plugins . commonjs !== false &&
286
+ merge ( { } , config . plugins . commonjs , {
287
+ // `ignore` is required to allow dynamic require
288
+ // See: https://github.com/rollup/rollup-plugin-commonjs/blob/4a22147456b1092dd565074dc33a63121675102a/src/index.js#L32
289
+ ignore : ( name : string ) => {
290
+ const { commonjs } = config . plugins
291
+ if ( commonjs && commonjs . ignore && commonjs . ignore ( name ) ) {
292
+ return true
293
+ }
294
+ return isExternal ( config . externals , name )
295
+ }
296
+ } )
228
297
}
229
298
230
- plugins . push (
231
- require ( 'rollup-plugin-postcss' ) (
232
- Object . assign ( { } , config . plugins . postcss , {
233
- extract : config . output . extractCSS !== false
234
- } )
235
- )
236
- )
299
+ const env = Object . assign ( { } , config . env )
237
300
238
- if ( source . hasTs && config . plugins . typescript2 !== false ) {
239
- plugins . push (
240
- this . localRequire ( 'rollup-plugin-typescript2' ) (
241
- merge (
242
- {
243
- objectHashIgnoreUnknownHack : true ,
244
- tsconfigOverride : {
245
- compilerOptions : {
246
- module : 'esnext'
247
- }
248
- }
249
- } ,
250
- config . plugins . typescript2
251
- )
252
- )
253
- )
301
+ pluginsOptions . replace = {
302
+ ...config . plugins . replace ,
303
+ values : {
304
+ ...Object . keys ( env ) . reduce ( ( res : Env , name ) => {
305
+ res [ name ] = JSON . stringify ( env [ name ] )
306
+ return res
307
+ } , { } ) ,
308
+ ...( config . plugins . replace && config . plugins . replace . values )
309
+ }
254
310
}
255
311
256
- if ( config . plugins . babel !== false ) {
257
- const pluginBabel = await import ( './plugins/babel' ) . then (
258
- res => res . default
259
- )
260
- plugins . push (
261
- pluginBabel (
262
- Object . assign (
263
- {
264
- exclude : 'node_modules/**' ,
265
- extensions : [ '.js' , '.jsx' , '.mjs' , '.ts' , '.tsx' ] ,
266
- babelrc : config . babel . babelrc ,
267
- configFile : config . babel . configFile ,
268
- presetOptions : config . babel
269
- } ,
270
- config . plugins . babel
271
- )
272
- )
273
- )
312
+ if ( Object . keys ( pluginsOptions . replace . values ) . length === 0 ) {
313
+ pluginsOptions . replace = false
274
314
}
275
315
276
- if ( config . babel . minimal ) {
277
- plugins . push (
278
- require ( 'rollup-plugin-buble' ) ( {
279
- exclude : 'node_modules/**' ,
280
- include : '**/*.{js,mjs,jsx}' ,
281
- transforms : {
282
- modules : false ,
283
- dangerousForOf : true ,
284
- dangerousTaggedTemplateString : true
285
- }
286
- } )
287
- )
316
+ const banner = getBanner ( config . banner , this . pkg . data )
317
+
318
+ if ( minify ) {
319
+ const terserOptions = config . plugins . terser || { }
320
+ pluginsOptions . terser = {
321
+ ... terserOptions ,
322
+ output : {
323
+ ... terserOptions . output ,
324
+ // Add banner (if there is)
325
+ preamble : banner
326
+ }
327
+ }
288
328
}
289
329
290
- if ( config . plugins . vue !== false && ( source . hasVue || config . plugins . vue ) ) {
291
- plugins . push (
292
- this . localRequire ( 'rollup-plugin-vue' ) (
293
- Object . assign (
294
- {
295
- css : false
296
- } ,
297
- config . plugins . vue
298
- )
299
- )
300
- )
330
+ for ( const name of Object . keys ( config . plugins ) ) {
331
+ if ( pluginsOptions [ name ] === undefined ) {
332
+ Object . assign ( pluginsOptions , { [ name ] : config . plugins [ name ] } )
333
+ }
301
334
}
302
335
303
- // Add commonjs plugin after babel and typescript
304
- // Since this plugin uses acorn to parse the source code
305
- const { commonjs } = config . plugins
306
- plugins . push (
307
- require ( 'rollup-plugin-commonjs' ) ( {
308
- ...commonjs ,
309
- // `ignore` is required to allow dynamic require
310
- // See: https://github.com/rollup/rollup-plugin-commonjs/blob/4a22147456b1092dd565074dc33a63121675102a/src/index.js#L32
311
- ignore : ( name : string ) => {
312
- if ( commonjs && commonjs . ignore && commonjs . ignore ( name ) ) {
313
- return true
314
- }
315
- return isExternal ( config . externals , name )
316
- }
317
- } )
318
- )
336
+ const getPlugin : GetPlugin = ( name : string ) => {
337
+ if ( config . resolvePlugins && config . resolvePlugins [ name ] ) {
338
+ return config . resolvePlugins [ name ]
339
+ }
319
340
320
- if ( config . env ) {
321
- const env = Object . assign ( { } , config . env )
322
- plugins . push (
323
- require ( 'rollup-plugin-replace' ) ( {
324
- ...Object . keys ( env ) . reduce ( ( res : Env , name ) => {
325
- res [ name ] = JSON . stringify ( env [ name ] )
326
- return res
327
- } , { } ) ,
328
- ...config . plugins . replace
329
- } )
330
- )
331
- }
341
+ const isBuiltIn = require ( '../package' ) . dependencies [
342
+ `rollup-plugin-${ name } `
343
+ ]
344
+ const plugin =
345
+ name === 'babel'
346
+ ? import ( './plugins/babel' ) . then ( res => res . default )
347
+ : name === 'node-resolve'
348
+ ? nodeResolvePlugin
349
+ : name === 'progress'
350
+ ? progressPlugin
351
+ : isBuiltIn
352
+ ? require ( `rollup-plugin-${ name } ` )
353
+ : this . localRequire ( `rollup-plugin-${ name } ` )
354
+
355
+ if ( name === 'terser' ) {
356
+ return plugin . terser
357
+ }
332
358
333
- const banner = getBanner ( config . banner , this . pkg . data )
359
+ return plugin . default || plugin
360
+ }
334
361
335
- if ( minify ) {
336
- const terserOptions = config . plugins . terser || { }
337
- plugins . push (
338
- require ( 'rollup-plugin-terser' ) . terser ( {
339
- ...terserOptions ,
340
- output : {
341
- ...terserOptions . output ,
342
- // Add banner (if there is)
343
- preamble : banner
344
- }
362
+ const plugins = await Promise . all (
363
+ Object . keys ( pluginsOptions )
364
+ . filter ( name => pluginsOptions [ name ] )
365
+ . map ( async name => {
366
+ const options =
367
+ pluginsOptions [ name ] === true ? { } : pluginsOptions [ name ]
368
+ const plugin = await getPlugin ( name )
369
+ return plugin ( options )
345
370
} )
346
- )
371
+ )
372
+
373
+ if ( logger . isDebug ) {
374
+ for ( const name of Object . keys ( pluginsOptions ) ) {
375
+ if ( pluginsOptions [ name ] ) {
376
+ logger . debug ( colors . dim ( format ) , `Using plugin: ${ name } ` )
377
+ }
378
+ }
347
379
}
348
380
349
381
// Add bundle to out assets Map
0 commit comments