diff --git a/src/Bundle.js b/src/Bundle.js index 9c6b44d..f05c60a 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -387,7 +387,16 @@ export default class Bundle { .filter( Boolean ) .join( '\n\n' ); - if ( intro ) intro += '\n'; + if ( intro ) intro += '\n\n'; + + let outro = [ options.outro ] + .concat( + this.plugins.map( plugin => plugin.outro && plugin.outro() ) + ) + .filter( Boolean ) + .join( '\n\n' ); + + if ( outro ) outro = `\n\n${outro}`; const indentString = getIndentString( magicString, options ); @@ -396,7 +405,7 @@ export default class Bundle { timeStart( 'render format' ); - magicString = finalise( this, magicString.trim(), { exportMode, indentString, intro }, options ); + magicString = finalise( this, magicString.trim(), { exportMode, indentString, intro, outro }, options ); timeEnd( 'render format' ); diff --git a/src/finalisers/amd.js b/src/finalisers/amd.js index 64adffd..70b404d 100644 --- a/src/finalisers/amd.js +++ b/src/finalisers/amd.js @@ -3,7 +3,7 @@ import getInteropBlock from './shared/getInteropBlock.js'; import getExportBlock from './shared/getExportBlock.js'; import esModuleExport from './shared/esModuleExport.js'; -export default function amd ( bundle, magicString, { exportMode, indentString, intro }, options ) { +export default function amd ( bundle, magicString, { exportMode, indentString, intro, outro }, options ) { const deps = bundle.externalModules.map( quotePath ); const args = bundle.externalModules.map( getName ); @@ -28,7 +28,7 @@ export default function amd ( bundle, magicString, { exportMode, indentString, i const exportBlock = getExportBlock( bundle.entryModule, exportMode ); if ( exportBlock ) magicString.append( '\n\n' + exportBlock ); if ( exportMode === 'named' && options.legacy !== true ) magicString.append( `\n\n${esModuleExport}` ); - if ( options.outro ) magicString.append( `\n${options.outro}` ); + if ( outro ) magicString.append( outro ); return magicString .indent( indentString ) diff --git a/src/finalisers/cjs.js b/src/finalisers/cjs.js index e89ef32..17f846f 100644 --- a/src/finalisers/cjs.js +++ b/src/finalisers/cjs.js @@ -1,7 +1,7 @@ import getExportBlock from './shared/getExportBlock.js'; import esModuleExport from './shared/esModuleExport.js'; -export default function cjs ( bundle, magicString, { exportMode, intro }, options ) { +export default function cjs ( bundle, magicString, { exportMode, intro, outro }, options ) { intro = ( options.useStrict === false ? intro : `'use strict';\n\n${intro}` ) + ( exportMode === 'named' && options.legacy !== true ? `${esModuleExport}\n\n` : '' ); @@ -45,7 +45,7 @@ export default function cjs ( bundle, magicString, { exportMode, intro }, option const exportBlock = getExportBlock( bundle.entryModule, exportMode, 'module.exports =' ); if ( exportBlock ) magicString.append( '\n\n' + exportBlock ); - if ( options.outro ) magicString.append( `\n${options.outro}` ); + if ( outro ) magicString.append( outro ); return magicString; } diff --git a/src/finalisers/es.js b/src/finalisers/es.js index 8251fe3..fb95375 100644 --- a/src/finalisers/es.js +++ b/src/finalisers/es.js @@ -4,7 +4,7 @@ function notDefault ( name ) { return name !== 'default'; } -export default function es ( bundle, magicString, { intro }, options ) { +export default function es ( bundle, magicString, { intro, outro }, options ) { const importBlock = bundle.externalModules .map( module => { const specifiers = []; @@ -71,7 +71,7 @@ export default function es ( bundle, magicString, { intro }, options ) { } if ( exportBlock ) magicString.append( '\n\n' + exportBlock.trim() ); - if ( options.outro ) magicString.append( `\n${options.outro}` ); + if ( outro ) magicString.append( outro ); return magicString.trim(); } diff --git a/src/finalisers/iife.js b/src/finalisers/iife.js index 01d1325..60105c1 100644 --- a/src/finalisers/iife.js +++ b/src/finalisers/iife.js @@ -23,7 +23,7 @@ function setupNamespace ( keypath ) { .join( '\n' ) + '\n'; } -export default function iife ( bundle, magicString, { exportMode, indentString, intro }, options ) { +export default function iife ( bundle, magicString, { exportMode, indentString, intro, outro }, options ) { const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle.onwarn ); const name = options.moduleName; @@ -63,7 +63,7 @@ export default function iife ( bundle, magicString, { exportMode, indentString, const exportBlock = getExportBlock( bundle.entryModule, exportMode ); if ( exportBlock ) magicString.append( '\n\n' + exportBlock ); - if ( options.outro ) magicString.append( `\n${options.outro}` ); + if ( outro ) magicString.append( outro ); return magicString .indent( indentString ) diff --git a/src/finalisers/umd.js b/src/finalisers/umd.js index dd8b9c6..279226d 100644 --- a/src/finalisers/umd.js +++ b/src/finalisers/umd.js @@ -25,7 +25,7 @@ function setupNamespace ( name ) { const wrapperOutro = '\n\n})));'; -export default function umd ( bundle, magicString, { exportMode, indentString, intro }, options ) { +export default function umd ( bundle, magicString, { exportMode, indentString, intro, outro }, options ) { if ( exportMode !== 'none' && !options.moduleName ) { throw new Error( 'You must supply options.moduleName for UMD bundles' ); } @@ -81,7 +81,7 @@ export default function umd ( bundle, magicString, { exportMode, indentString, i const exportBlock = getExportBlock( bundle.entryModule, exportMode ); if ( exportBlock ) magicString.append( '\n\n' + exportBlock ); if ( exportMode === 'named' && options.legacy !== true ) magicString.append( `\n\n${esModuleExport}` ); - if ( options.outro ) magicString.append( `\n${options.outro}` ); + if ( outro ) magicString.append( outro ); return magicString .trim() diff --git a/test/cli/banner-intro-outro-footer/_expected.js b/test/cli/banner-intro-outro-footer/_expected.js index 521af55..8493276 100644 --- a/test/cli/banner-intro-outro-footer/_expected.js +++ b/test/cli/banner-intro-outro-footer/_expected.js @@ -3,7 +3,9 @@ 'use strict'; // intro + console.log( 42 ); + // outro }()); diff --git a/test/form/intro-and-outro/_config.js b/test/form/intro-and-outro/_config.js index 410d586..232b008 100644 --- a/test/form/intro-and-outro/_config.js +++ b/test/form/intro-and-outro/_config.js @@ -4,6 +4,25 @@ module.exports = { intro: '/* this is an intro */', outro: '/* this is an outro */', moduleName: 'foo', - external: [ 'external' ] + external: [ 'external' ], + plugins: [ + { + intro () { + return '// intro 1' + }, + outro () { + return '// outro 1' + } + }, + + { + intro () { + return '// intro 2' + }, + outro () { + return '// outro 2' + } + } + ] } }; diff --git a/test/form/intro-and-outro/_expected/amd.js b/test/form/intro-and-outro/_expected/amd.js index ca940e4..74ad78d 100644 --- a/test/form/intro-and-outro/_expected/amd.js +++ b/test/form/intro-and-outro/_expected/amd.js @@ -1,6 +1,11 @@ define(['external'], function (a) { 'use strict'; /* this is an intro */ + + // intro 1 + + // intro 2 + var a__default = 'default' in a ? a['default'] : a; console.log( a__default ); @@ -9,6 +14,11 @@ define(['external'], function (a) { 'use strict'; var main = 42; return main; + /* this is an outro */ + // outro 1 + + // outro 2 + }); diff --git a/test/form/intro-and-outro/_expected/cjs.js b/test/form/intro-and-outro/_expected/cjs.js index 6c68e67..ba3c331 100644 --- a/test/form/intro-and-outro/_expected/cjs.js +++ b/test/form/intro-and-outro/_expected/cjs.js @@ -1,6 +1,11 @@ 'use strict'; /* this is an intro */ + +// intro 1 + +// intro 2 + function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var a = require('external'); @@ -12,4 +17,9 @@ console.log( a.b ); var main = 42; module.exports = main; + /* this is an outro */ + +// outro 1 + +// outro 2 diff --git a/test/form/intro-and-outro/_expected/es.js b/test/form/intro-and-outro/_expected/es.js index 2da2a74..7a9a073 100644 --- a/test/form/intro-and-outro/_expected/es.js +++ b/test/form/intro-and-outro/_expected/es.js @@ -1,4 +1,9 @@ /* this is an intro */ + +// intro 1 + +// intro 2 + import a, { b } from 'external'; console.log( a ); @@ -7,4 +12,9 @@ console.log( b ); var main = 42; export default main; + /* this is an outro */ + +// outro 1 + +// outro 2 diff --git a/test/form/intro-and-outro/_expected/iife.js b/test/form/intro-and-outro/_expected/iife.js index 2c716d3..4e04b79 100644 --- a/test/form/intro-and-outro/_expected/iife.js +++ b/test/form/intro-and-outro/_expected/iife.js @@ -2,6 +2,11 @@ var foo = (function (a) { 'use strict'; /* this is an intro */ + + // intro 1 + + // intro 2 + var a__default = 'default' in a ? a['default'] : a; console.log( a__default ); @@ -10,6 +15,11 @@ var foo = (function (a) { var main = 42; return main; + /* this is an outro */ + // outro 1 + + // outro 2 + }(a)); diff --git a/test/form/intro-and-outro/_expected/umd.js b/test/form/intro-and-outro/_expected/umd.js index 232db4e..a1de1a0 100644 --- a/test/form/intro-and-outro/_expected/umd.js +++ b/test/form/intro-and-outro/_expected/umd.js @@ -5,6 +5,11 @@ }(this, (function (a) { 'use strict'; /* this is an intro */ + + // intro 1 + + // intro 2 + var a__default = 'default' in a ? a['default'] : a; console.log( a__default ); @@ -13,6 +18,11 @@ var main = 42; return main; + /* this is an outro */ + // outro 1 + + // outro 2 + })));