From a27ba30aa6ab400d0dde858e0f086cc554c5cbff Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Wed, 31 Aug 2016 23:24:19 -0700 Subject: [PATCH] Add options as second arg to .transformBundle Currently only piping through `format`, could certainly pass others if necessary. Maybe all should be brought through? --- src/Bundle.js | 2 +- src/utils/transformBundle.js | 4 ++-- test/form/transform-bundle-plugin-options/_config.js | 12 ++++++++++++ .../transform-bundle-plugin-options/_expected/amd.js | 1 + .../transform-bundle-plugin-options/_expected/cjs.js | 1 + .../transform-bundle-plugin-options/_expected/es.js | 1 + .../_expected/iife.js | 1 + .../transform-bundle-plugin-options/_expected/umd.js | 1 + test/form/transform-bundle-plugin-options/main.js | 1 + 9 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 test/form/transform-bundle-plugin-options/_config.js create mode 100644 test/form/transform-bundle-plugin-options/_expected/amd.js create mode 100644 test/form/transform-bundle-plugin-options/_expected/cjs.js create mode 100644 test/form/transform-bundle-plugin-options/_expected/es.js create mode 100644 test/form/transform-bundle-plugin-options/_expected/iife.js create mode 100644 test/form/transform-bundle-plugin-options/_expected/umd.js create mode 100644 test/form/transform-bundle-plugin-options/main.js diff --git a/src/Bundle.js b/src/Bundle.js index e458975..36d1675 100644 --- a/src/Bundle.js +++ b/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 ) { diff --git a/src/utils/transformBundle.js b/src/utils/transformBundle.js index 027c7f0..8569ad5 100644 --- a/src/utils/transformBundle.js +++ b/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}`; diff --git a/test/form/transform-bundle-plugin-options/_config.js b/test/form/transform-bundle-plugin-options/_config.js new file mode 100644 index 0000000..417e38f --- /dev/null +++ b/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); + } + } + ] + } +}; diff --git a/test/form/transform-bundle-plugin-options/_expected/amd.js b/test/form/transform-bundle-plugin-options/_expected/amd.js new file mode 100644 index 0000000..69904ec --- /dev/null +++ b/test/form/transform-bundle-plugin-options/_expected/amd.js @@ -0,0 +1 @@ +{"format":"amd"} diff --git a/test/form/transform-bundle-plugin-options/_expected/cjs.js b/test/form/transform-bundle-plugin-options/_expected/cjs.js new file mode 100644 index 0000000..72dd9eb --- /dev/null +++ b/test/form/transform-bundle-plugin-options/_expected/cjs.js @@ -0,0 +1 @@ +{"format":"cjs"} diff --git a/test/form/transform-bundle-plugin-options/_expected/es.js b/test/form/transform-bundle-plugin-options/_expected/es.js new file mode 100644 index 0000000..3a0913b --- /dev/null +++ b/test/form/transform-bundle-plugin-options/_expected/es.js @@ -0,0 +1 @@ +{"format":"es"} diff --git a/test/form/transform-bundle-plugin-options/_expected/iife.js b/test/form/transform-bundle-plugin-options/_expected/iife.js new file mode 100644 index 0000000..9bdbe7b --- /dev/null +++ b/test/form/transform-bundle-plugin-options/_expected/iife.js @@ -0,0 +1 @@ +{"format":"iife"} diff --git a/test/form/transform-bundle-plugin-options/_expected/umd.js b/test/form/transform-bundle-plugin-options/_expected/umd.js new file mode 100644 index 0000000..c7dad30 --- /dev/null +++ b/test/form/transform-bundle-plugin-options/_expected/umd.js @@ -0,0 +1 @@ +{"format":"umd"} diff --git a/test/form/transform-bundle-plugin-options/main.js b/test/form/transform-bundle-plugin-options/main.js new file mode 100644 index 0000000..934dee7 --- /dev/null +++ b/test/form/transform-bundle-plugin-options/main.js @@ -0,0 +1 @@ +console.log( 1 + 1 );