Browse Source

Add transformBundle plugin hook

gh-438-b
Bogdan Chadkin 9 years ago
parent
commit
8e42a7cd24
  1. 7
      src/Bundle.js
  2. 53
      src/utils/transformBundle.js
  3. 21
      test/form/transform-bundle-plugin/_config.js
  4. 2
      test/form/transform-bundle-plugin/_expected/amd.js
  5. 2
      test/form/transform-bundle-plugin/_expected/cjs.js
  6. 2
      test/form/transform-bundle-plugin/_expected/es6.js
  7. 2
      test/form/transform-bundle-plugin/_expected/iife.js
  8. 2
      test/form/transform-bundle-plugin/_expected/umd.js
  9. 1
      test/form/transform-bundle-plugin/main.js

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

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

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

2
test/form/transform-bundle-plugin/_expected/amd.js

@ -0,0 +1,2 @@
/* first plugin */
/* second plugin */

2
test/form/transform-bundle-plugin/_expected/cjs.js

@ -0,0 +1,2 @@
/* first plugin */
/* second plugin */

2
test/form/transform-bundle-plugin/_expected/es6.js

@ -0,0 +1,2 @@
/* first plugin */
/* second plugin */

2
test/form/transform-bundle-plugin/_expected/iife.js

@ -0,0 +1,2 @@
/* first plugin */
/* second plugin */

2
test/form/transform-bundle-plugin/_expected/umd.js

@ -0,0 +1,2 @@
/* first plugin */
/* second plugin */

1
test/form/transform-bundle-plugin/main.js

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