diff --git a/src/Bundle.js b/src/Bundle.js index 950ae9c..d224bef 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -11,6 +11,7 @@ import getExportMode from './utils/getExportMode.js'; import getIndentString from './utils/getIndentString.js'; import { unixizePath } from './utils/normalizePlatform.js'; import transform from './utils/transform.js'; +import transformBundle from './utils/transformBundle.js'; import collapseSourcemaps from './utils/collapseSourcemaps.js'; import callIfFunction from './utils/callIfFunction.js'; import { isRelative } from './utils/path.js'; @@ -46,6 +47,10 @@ export default class Bundle { .map( plugin => plugin.transform ) .filter( Boolean ); + this.bundleTransformers = this.plugins + .map( plugin => plugin.transformBundle ) + .filter( Boolean ); + this.moduleById = blank(); this.modules = []; @@ -257,7 +262,7 @@ export default class Bundle { map.sources = map.sources.map( unixizePath ); } - return { code, map }; + return transformBundle( { code, map }, this.bundleTransformers ); } sort () { diff --git a/src/utils/transformBundle.js b/src/utils/transformBundle.js new file mode 100644 index 0000000..188e777 --- /dev/null +++ b/src/utils/transformBundle.js @@ -0,0 +1,53 @@ +import Promise from 'es6-promise/lib/es6-promise/promise.js'; + +export default function transformBundle ( source, transformers ) { + if ( typeof source === 'string' ) { + source = { + code: source, + map: null + }; + } + + return transformers.reduce( ( previous, transformer ) => { + let result = transformer( previous ) + + if ( result == null ) return previous; + + if ( typeof result === 'string' ) { + result = { + code: result, + map: null + }; + } + // `result.map` can only be a string if `result` isn't + else if ( typeof result.map === 'string' ) { + result.map = JSON.parse( result.map ); + } + + return result; + + }, source ); + + // Promisified version + // return transformers.reduce( ( promise, transformer ) => { + // return promise.then( previous => { + // return Promise.resolve( transformer( previous ) ).then( result => { + // if ( result == null ) return previous; + + // if ( typeof result === 'string' ) { + // result = { + // code: result, + // map: null + // }; + // } + // // `result.map` can only be a string if `result` isn't + // else if ( typeof result.map === 'string' ) { + // result.map = JSON.parse( result.map ); + // } + + // return result; + // }); + // }); + + // }, Promise.resolve( source ) ); +} diff --git a/test/form/transform-bundle-plugin/_config.js b/test/form/transform-bundle-plugin/_config.js new file mode 100644 index 0000000..5e96ee6 --- /dev/null +++ b/test/form/transform-bundle-plugin/_config.js @@ -0,0 +1,21 @@ +module.exports = { + description: 'allows plugins to set banner and footer options', + options: { + plugins: [ + { + transformBundle: function (result) { + return { + code: '/* first plugin */' + }; + } + }, + { + transformBundle: function (result) { + return { + code: result.code + '\n/* second plugin */' + }; + } + } + ] + } +} diff --git a/test/form/transform-bundle-plugin/_expected/amd.js b/test/form/transform-bundle-plugin/_expected/amd.js new file mode 100644 index 0000000..f783236 --- /dev/null +++ b/test/form/transform-bundle-plugin/_expected/amd.js @@ -0,0 +1,2 @@ +/* first plugin */ +/* second plugin */ \ No newline at end of file diff --git a/test/form/transform-bundle-plugin/_expected/cjs.js b/test/form/transform-bundle-plugin/_expected/cjs.js new file mode 100644 index 0000000..f783236 --- /dev/null +++ b/test/form/transform-bundle-plugin/_expected/cjs.js @@ -0,0 +1,2 @@ +/* first plugin */ +/* second plugin */ \ No newline at end of file diff --git a/test/form/transform-bundle-plugin/_expected/es6.js b/test/form/transform-bundle-plugin/_expected/es6.js new file mode 100644 index 0000000..f783236 --- /dev/null +++ b/test/form/transform-bundle-plugin/_expected/es6.js @@ -0,0 +1,2 @@ +/* first plugin */ +/* second plugin */ \ No newline at end of file diff --git a/test/form/transform-bundle-plugin/_expected/iife.js b/test/form/transform-bundle-plugin/_expected/iife.js new file mode 100644 index 0000000..f783236 --- /dev/null +++ b/test/form/transform-bundle-plugin/_expected/iife.js @@ -0,0 +1,2 @@ +/* first plugin */ +/* second plugin */ \ No newline at end of file diff --git a/test/form/transform-bundle-plugin/_expected/umd.js b/test/form/transform-bundle-plugin/_expected/umd.js new file mode 100644 index 0000000..f783236 --- /dev/null +++ b/test/form/transform-bundle-plugin/_expected/umd.js @@ -0,0 +1,2 @@ +/* first plugin */ +/* second plugin */ \ No newline at end of file diff --git a/test/form/transform-bundle-plugin/main.js b/test/form/transform-bundle-plugin/main.js new file mode 100644 index 0000000..934dee7 --- /dev/null +++ b/test/form/transform-bundle-plugin/main.js @@ -0,0 +1 @@ +console.log( 1 + 1 );