Browse Source

Add options as second arg to .transformBundle

Currently only piping through `format`, could certainly pass others if
necessary. Maybe all should be brought through?
rewrite
Pat Cavit 8 years ago
parent
commit
a27ba30aa6
  1. 2
      src/Bundle.js
  2. 4
      src/utils/transformBundle.js
  3. 12
      test/form/transform-bundle-plugin-options/_config.js
  4. 1
      test/form/transform-bundle-plugin-options/_expected/amd.js
  5. 1
      test/form/transform-bundle-plugin-options/_expected/cjs.js
  6. 1
      test/form/transform-bundle-plugin-options/_expected/es.js
  7. 1
      test/form/transform-bundle-plugin-options/_expected/iife.js
  8. 1
      test/form/transform-bundle-plugin-options/_expected/umd.js
  9. 1
      test/form/transform-bundle-plugin-options/main.js

2
src/Bundle.js

@ -347,7 +347,7 @@ export default class Bundle {
let map = null;
const bundleSourcemapChain = [];
code = transformBundle( code, this.plugins, bundleSourcemapChain )
code = transformBundle( code, this.plugins, bundleSourcemapChain, options )
.replace( new RegExp( `\\/\\/#\\s+${SOURCEMAPPING_URL}=.+\\n?`, 'g' ), '' );
if ( options.sourceMap ) {

4
src/utils/transformBundle.js

@ -1,13 +1,13 @@
import { decode } from 'sourcemap-codec';
export default function transformBundle ( code, plugins, sourceMapChain ) {
export default function transformBundle ( code, plugins, sourceMapChain, options ) {
return plugins.reduce( ( code, plugin ) => {
if ( !plugin.transformBundle ) return code;
let result;
try {
result = plugin.transformBundle( code );
result = plugin.transformBundle( code, { format : options.format } );
} catch ( err ) {
err.plugin = plugin.name;
err.message = `Error transforming bundle${plugin.name ? ` with '${plugin.name}' plugin` : ''}: ${err.message}`;

12
test/form/transform-bundle-plugin-options/_config.js

@ -0,0 +1,12 @@
module.exports = {
description: 'plugin .transformBundle gets passed options',
options: {
plugins: [
{
transformBundle: function (code, options) {
return JSON.stringify(options);
}
}
]
}
};

1
test/form/transform-bundle-plugin-options/_expected/amd.js

@ -0,0 +1 @@
{"format":"amd"}

1
test/form/transform-bundle-plugin-options/_expected/cjs.js

@ -0,0 +1 @@
{"format":"cjs"}

1
test/form/transform-bundle-plugin-options/_expected/es.js

@ -0,0 +1 @@
{"format":"es"}

1
test/form/transform-bundle-plugin-options/_expected/iife.js

@ -0,0 +1 @@
{"format":"iife"}

1
test/form/transform-bundle-plugin-options/_expected/umd.js

@ -0,0 +1 @@
{"format":"umd"}

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

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