Browse Source

Merge pull request #246 from rollup/gh-239

support banner and footer options in plugins
better-aggressive
Rich Harris 9 years ago
parent
commit
99af7516f5
  1. 17
      src/Bundle.js
  2. 3
      src/utils/callIfFunction.js
  3. 15
      test/form/banner-and-footer-plugin/_config.js
  4. 9
      test/form/banner-and-footer-plugin/_expected/amd.js
  5. 7
      test/form/banner-and-footer-plugin/_expected/cjs.js
  6. 5
      test/form/banner-and-footer-plugin/_expected/es6.js
  7. 9
      test/form/banner-and-footer-plugin/_expected/iife.js
  8. 13
      test/form/banner-and-footer-plugin/_expected/umd.js
  9. 1
      test/form/banner-and-footer-plugin/main.js

17
src/Bundle.js

@ -12,6 +12,7 @@ import getIndentString from './utils/getIndentString.js';
import { unixizePath } from './utils/normalizePlatform.js'; import { unixizePath } from './utils/normalizePlatform.js';
import transform from './utils/transform.js'; import transform from './utils/transform.js';
import collapseSourcemaps from './utils/collapseSourcemaps.js'; import collapseSourcemaps from './utils/collapseSourcemaps.js';
import callIfFunction from './utils/callIfFunction.js';
export default class Bundle { export default class Bundle {
constructor ( options ) { constructor ( options ) {
@ -207,8 +208,20 @@ export default class Bundle {
magicString = finalise( this, magicString.trim(), { exportMode, indentString }, options ); magicString = finalise( this, magicString.trim(), { exportMode, indentString }, options );
if ( options.banner ) magicString.prepend( options.banner + '\n' ); const banner = [ options.banner ]
if ( options.footer ) magicString.append( '\n' + options.footer ); .concat( this.plugins.map( plugin => plugin.banner ) )
.map( callIfFunction )
.filter( Boolean )
.join( '\n' );
const footer = [ options.footer ]
.concat( this.plugins.map( plugin => plugin.footer ) )
.map( callIfFunction )
.filter( Boolean )
.join( '\n' );
if ( banner ) magicString.prepend( banner + '\n' );
if ( footer ) magicString.append( '\n' + footer );
const code = magicString.toString(); const code = magicString.toString();
let map = null; let map = null;

3
src/utils/callIfFunction.js

@ -0,0 +1,3 @@
export default function callIfFunction ( thing ) {
return typeof thing === 'function' ? thing() : thing;
}

15
test/form/banner-and-footer-plugin/_config.js

@ -0,0 +1,15 @@
module.exports = {
description: 'allows plugins to set banner and footer options',
options: {
plugins: [
{
banner: '/* first banner */',
footer: function () { return '/* first footer */'; }
},
{
banner: function () { return '/* second banner */'; },
footer: '/* second footer */'
}
]
}
}

9
test/form/banner-and-footer-plugin/_expected/amd.js

@ -0,0 +1,9 @@
/* first banner */
/* second banner */
define(function () { 'use strict';
console.log( 1 + 1 );
});
/* first footer */
/* second footer */

7
test/form/banner-and-footer-plugin/_expected/cjs.js

@ -0,0 +1,7 @@
/* first banner */
/* second banner */
'use strict';
console.log( 1 + 1 );
/* first footer */
/* second footer */

5
test/form/banner-and-footer-plugin/_expected/es6.js

@ -0,0 +1,5 @@
/* first banner */
/* second banner */
console.log( 1 + 1 );
/* first footer */
/* second footer */

9
test/form/banner-and-footer-plugin/_expected/iife.js

@ -0,0 +1,9 @@
/* first banner */
/* second banner */
(function () { 'use strict';
console.log( 1 + 1 );
})();
/* first footer */
/* second footer */

13
test/form/banner-and-footer-plugin/_expected/umd.js

@ -0,0 +1,13 @@
/* first banner */
/* second banner */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
factory();
}(this, function () { 'use strict';
console.log( 1 + 1 );
}));
/* first footer */
/* second footer */

1
test/form/banner-and-footer-plugin/main.js

@ -0,0 +1 @@
console.log( 1 + 1 );
Loading…
Cancel
Save