Skip to content

Commit b7f498e

Browse files
committedMar 15, 2019
fix: plugin order
1 parent 28b1a19 commit b7f498e

File tree

11 files changed

+233
-228
lines changed

11 files changed

+233
-228
lines changed
 

‎docs/plugins.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212

1313
The name should be the package name of the plugin, without the `rollup-plugin-` prefix.
1414

15-
The value will be used as its options, passing `true` is equivalent to an empty object, `false` is used to disable <a href="/api/globals.html#builtin_plugins">built-in plugins</a>.
15+
The value will be used as its options, passing `true` is equivalent to an empty object, `false` is used to disable built-in plugins.
1616

1717
To add plugin via CLI flags, you can do this:
1818

‎src/constants.ts

-9
This file was deleted.

‎src/index.ts

+171-139
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import nodeResolvePlugin from './plugins/node-resolve'
2424
import configLoader from './config-loader'
2525
import isExternal from './utils/is-external'
2626
import getBanner from './utils/get-banner'
27-
import { BUILTIN_PLUGINS } from './constants'
2827
import {
2928
Options,
3029
Config,
@@ -82,6 +81,9 @@ interface RollupConfigInput {
8281
config: NormalizedConfig
8382
}
8483

84+
type PluginFactory = (opts: any) => RollupPlugin
85+
type GetPlugin = (name: string) => PluginFactory | Promise<PluginFactory>
86+
8587
export class Bundler {
8688
rootDir: string
8789
config: NormalizedConfig
@@ -170,20 +172,6 @@ export class Bundler {
170172
assets,
171173
config
172174
}: 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-
187175
// Always minify if config.minify is truthy
188176
// Otherwise infer by format
189177
const minify =
@@ -205,145 +193,189 @@ export class Bundler {
205193
rollupFormat === 'iife' ||
206194
config.bundleNodeModules
207195

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+
})
228297
}
229298

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)
237300

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+
}
254310
}
255311

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
274314
}
275315

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+
}
288328
}
289329

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+
}
301334
}
302335

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+
}
319340

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+
}
332358

333-
const banner = getBanner(config.banner, this.pkg.data)
359+
return plugin.default || plugin
360+
}
334361

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)
345370
})
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+
}
347379
}
348380

349381
// Add bundle to out assets Map

‎src/logger.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Logger {
4444

4545
debug(...args: any[]) {
4646
if (!this.isDebug) return
47-
this.log(...args)
47+
this.log(colors.magenta('verbose'), ...args)
4848
}
4949

5050
progress(text: string) {

‎src/types.ts

+18
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ export interface Config {
192192
plugins?: {
193193
[name: string]: any
194194
}
195+
/**
196+
* Defines how to resolve a plugin by name
197+
* This will override the default behavior
198+
* e.g.
199+
* ```js
200+
* {
201+
* resolvePlugins: {
202+
* replace: require('./my-fork-of-rollup-plugin-replace')
203+
* }
204+
* }
205+
* ```
206+
*/
207+
resolvePlugins?: {
208+
[name: string]: any
209+
}
195210
/**
196211
* Include node modules in the bundle. Note that this is always `true` for UMD bundle.
197212
* @cli `--bundle-node-modules`
@@ -251,6 +266,9 @@ export interface NormalizedConfig {
251266
plugins: {
252267
[name: string]: any
253268
}
269+
resolvePlugins?: {
270+
[name: string]: any
271+
}
254272
externals: Externals
255273
globals?: {
256274
[k: string]: string

‎test/__snapshots__/index.test.ts.snap

+35-42
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,7 @@ module.exports = _default;
100100
"
101101
`;
102102

103-
exports[`banner:true with date: banner:true with date dist/index.js 1`] = `
104-
"/*!
105-
* foo v0.0.0
106-
* (c) name <email@email.com>
107-
* Released under the MIT License.
108-
*/
109-
'use strict';
110-
111-
var index = 42;
112-
113-
module.exports = index;
114-
"
115-
`;
116-
117-
exports[`banner:true without any date: banner:true without any date dist/index.js 1`] = `
103+
exports[`banner:true default: banner:true default dist/index.js 1`] = `
118104
"/*!
119105
* foo v0.0.0
120106
* (c) name <email@email.com>
@@ -298,37 +284,44 @@ var script = {
298284
};
299285
300286
/* script */
301-
const __vue_script__ = script;
302-
// For security concerns, we use only base name in production mode. See https://github.com/vuejs/rollup-plugin-vue/issues/258
287+
const __vue_script__ = script; // For security concerns, we use only base name in production mode. See https://github.com/vuejs/rollup-plugin-vue/issues/258
288+
303289
script.__file = \\"component.vue\\";
304290
/* template */
305-
var __vue_render__ = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('div',{staticClass:\\"main-content\\"},[_c('h1',[_vm._v(\\"This is your first component in Vue\\")]),_vm._v(\\" \\"),_c('h3',[_vm._v(_vm._s(_vm.name))])])};
291+
292+
var __vue_render__ = function () {
293+
var _vm = this;
294+
295+
var _h = _vm.$createElement;
296+
297+
var _c = _vm._self._c || _h;
298+
299+
return _c('div', {
300+
staticClass: \\"main-content\\"
301+
}, [_c('h1', [_vm._v(\\"This is your first component in Vue\\")]), _vm._v(\\" \\"), _c('h3', [_vm._v(_vm._s(_vm.name))])]);
302+
};
303+
306304
var __vue_staticRenderFns__ = [];
305+
/* style */
306+
307+
const __vue_inject_styles__ = undefined;
308+
/* scoped */
309+
310+
const __vue_scope_id__ = \\"data-v-5fccef39\\";
311+
/* module identifier */
312+
313+
const __vue_module_identifier__ = undefined;
314+
/* functional template */
315+
316+
const __vue_is_functional_template__ = false;
317+
/* style inject */
318+
319+
/* style inject SSR */
307320
308-
/* style */
309-
const __vue_inject_styles__ = undefined;
310-
/* scoped */
311-
const __vue_scope_id__ = \\"data-v-5fccef39\\";
312-
/* module identifier */
313-
const __vue_module_identifier__ = undefined;
314-
/* functional template */
315-
const __vue_is_functional_template__ = false;
316-
/* style inject */
317-
318-
/* style inject SSR */
319-
320-
321-
322-
var component = __vue_normalize__(
323-
{ render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ },
324-
__vue_inject_styles__,
325-
__vue_script__,
326-
__vue_scope_id__,
327-
__vue_is_functional_template__,
328-
__vue_module_identifier__,
329-
undefined,
330-
undefined
331-
);
321+
var component = __vue_normalize__({
322+
render: __vue_render__,
323+
staticRenderFns: __vue_staticRenderFns__
324+
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);
332325
333326
module.exports = component;
334327
"

‎test/fixtures/banner/with-date/package.json

-10
This file was deleted.

‎test/fixtures/banner/without-date/index.js

-1
This file was deleted.

‎test/index.test.ts

+7-25
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,9 @@ snapshot({
5151

5252
snapshot(
5353
{
54-
title: 'banner:true with date',
54+
title: 'banner:true default',
5555
input: 'index.js',
56-
cwd: fixture('banner/with-date')
57-
},
58-
{
59-
banner: true
60-
}
61-
)
62-
63-
snapshot(
64-
{
65-
title: 'banner:true without any date',
66-
input: 'index.js',
67-
cwd: fixture('banner/without-date')
56+
cwd: fixture('banner/default')
6857
},
6958
{
7059
banner: true
@@ -197,18 +186,11 @@ snapshot(
197186
}
198187
)
199188

200-
snapshot(
201-
{
202-
title: 'vue plugin',
203-
input: 'component.vue',
204-
cwd: fixture('vue')
205-
},
206-
{
207-
plugins: {
208-
vue: true
209-
}
210-
}
211-
)
189+
snapshot({
190+
title: 'vue plugin',
191+
input: 'component.vue',
192+
cwd: fixture('vue')
193+
})
212194

213195
snapshot({
214196
title: 'Typescript',

0 commit comments

Comments
 (0)
Please sign in to comment.