From 430041420631db17ff2babd926aaf234577f98b0 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Fri, 30 Oct 2015 18:52:11 -0400 Subject: [PATCH] support banner and footer options in plugins --- src/Bundle.js | 17 +++++++++++++++-- src/utils/callIfFunction.js | 3 +++ test/form/banner-and-footer-plugin/_config.js | 15 +++++++++++++++ .../banner-and-footer-plugin/_expected/amd.js | 9 +++++++++ .../banner-and-footer-plugin/_expected/cjs.js | 7 +++++++ .../banner-and-footer-plugin/_expected/es6.js | 5 +++++ .../banner-and-footer-plugin/_expected/iife.js | 9 +++++++++ .../banner-and-footer-plugin/_expected/umd.js | 13 +++++++++++++ test/form/banner-and-footer-plugin/main.js | 1 + 9 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/utils/callIfFunction.js create mode 100644 test/form/banner-and-footer-plugin/_config.js create mode 100644 test/form/banner-and-footer-plugin/_expected/amd.js create mode 100644 test/form/banner-and-footer-plugin/_expected/cjs.js create mode 100644 test/form/banner-and-footer-plugin/_expected/es6.js create mode 100644 test/form/banner-and-footer-plugin/_expected/iife.js create mode 100644 test/form/banner-and-footer-plugin/_expected/umd.js create mode 100644 test/form/banner-and-footer-plugin/main.js diff --git a/src/Bundle.js b/src/Bundle.js index d5163d0..c4c5edf 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -12,6 +12,7 @@ import getIndentString from './utils/getIndentString.js'; import { unixizePath } from './utils/normalizePlatform.js'; import transform from './utils/transform.js'; import collapseSourcemaps from './utils/collapseSourcemaps.js'; +import callIfFunction from './utils/callIfFunction.js'; export default class Bundle { constructor ( options ) { @@ -207,8 +208,20 @@ export default class Bundle { magicString = finalise( this, magicString.trim(), { exportMode, indentString }, options ); - if ( options.banner ) magicString.prepend( options.banner + '\n' ); - if ( options.footer ) magicString.append( '\n' + options.footer ); + const banner = [ options.banner ] + .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(); let map = null; diff --git a/src/utils/callIfFunction.js b/src/utils/callIfFunction.js new file mode 100644 index 0000000..1dd194b --- /dev/null +++ b/src/utils/callIfFunction.js @@ -0,0 +1,3 @@ +export default function callIfFunction ( thing ) { + return typeof thing === 'function' ? thing() : thing; +} diff --git a/test/form/banner-and-footer-plugin/_config.js b/test/form/banner-and-footer-plugin/_config.js new file mode 100644 index 0000000..1432418 --- /dev/null +++ b/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 */' + } + ] + } +} diff --git a/test/form/banner-and-footer-plugin/_expected/amd.js b/test/form/banner-and-footer-plugin/_expected/amd.js new file mode 100644 index 0000000..2fb2e17 --- /dev/null +++ b/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 */ diff --git a/test/form/banner-and-footer-plugin/_expected/cjs.js b/test/form/banner-and-footer-plugin/_expected/cjs.js new file mode 100644 index 0000000..b5f8583 --- /dev/null +++ b/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 */ diff --git a/test/form/banner-and-footer-plugin/_expected/es6.js b/test/form/banner-and-footer-plugin/_expected/es6.js new file mode 100644 index 0000000..cb611a5 --- /dev/null +++ b/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 */ diff --git a/test/form/banner-and-footer-plugin/_expected/iife.js b/test/form/banner-and-footer-plugin/_expected/iife.js new file mode 100644 index 0000000..03dc18b --- /dev/null +++ b/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 */ diff --git a/test/form/banner-and-footer-plugin/_expected/umd.js b/test/form/banner-and-footer-plugin/_expected/umd.js new file mode 100644 index 0000000..f650793 --- /dev/null +++ b/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 */ diff --git a/test/form/banner-and-footer-plugin/main.js b/test/form/banner-and-footer-plugin/main.js new file mode 100644 index 0000000..934dee7 --- /dev/null +++ b/test/form/banner-and-footer-plugin/main.js @@ -0,0 +1 @@ +console.log( 1 + 1 );