Browse Source

support plugin outros

gh-1008
Rich-Harris 8 years ago
parent
commit
38750c41ec
  1. 13
      src/Bundle.js
  2. 4
      src/finalisers/amd.js
  3. 4
      src/finalisers/cjs.js
  4. 4
      src/finalisers/es.js
  5. 4
      src/finalisers/iife.js
  6. 4
      src/finalisers/umd.js
  7. 2
      test/cli/banner-intro-outro-footer/_expected.js
  8. 21
      test/form/intro-and-outro/_config.js
  9. 10
      test/form/intro-and-outro/_expected/amd.js
  10. 10
      test/form/intro-and-outro/_expected/cjs.js
  11. 10
      test/form/intro-and-outro/_expected/es.js
  12. 10
      test/form/intro-and-outro/_expected/iife.js
  13. 10
      test/form/intro-and-outro/_expected/umd.js

13
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' );

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

4
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;
}

4
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();
}

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

4
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()

2
test/cli/banner-intro-outro-footer/_expected.js

@ -3,7 +3,9 @@
'use strict';
// intro
console.log( 42 );
// outro
}());

21
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'
}
}
]
}
};

10
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
});

10
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

10
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

10
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));

10
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
})));

Loading…
Cancel
Save