diff --git a/packages/rollup-plugin-babel/src/index.js b/packages/rollup-plugin-babel/src/index.js index f93572f09..315e69b6f 100644 --- a/packages/rollup-plugin-babel/src/index.js +++ b/packages/rollup-plugin-babel/src/index.js @@ -29,9 +29,9 @@ export default function babel ( options ) { let externalHelpersWhitelist = null; if ( options.externalHelpersWhitelist ) externalHelpersWhitelist = options.externalHelpersWhitelist; delete options.externalHelpersWhitelist; - + let warn = msg => console.warn(msg); // eslint-disable-line no-console - + return { name: 'babel', @@ -68,9 +68,13 @@ export default function babel ( options ) { if ( usedHelpers.length ) { if ( helpers === BUNDLED ) { - if ( !externalHelpers ) transformed.code += `\n\nimport * as babelHelpers from '${HELPERS}';`; - } else if ( helpers === RUNTIME && !runtimeHelpers ) { - throw new Error( 'Runtime helpers are not enabled. Either exclude the transform-runtime Babel plugin or pass the `runtimeHelpers: true` option. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more information' ); + if ( !externalHelpers ) { + transformed.code += `\n\nimport * as babelHelpers from '${HELPERS}';`; + } + } else if ( helpers === RUNTIME ) { + if ( !runtimeHelpers ) { + throw new Error( 'Runtime helpers are not enabled. Either exclude the transform-runtime Babel plugin or pass the `runtimeHelpers: true` option. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more information' ); + } } else { usedHelpers.forEach( helper => { if ( inlineHelpers[ helper ] ) { diff --git a/packages/rollup-plugin-babel/src/preflightCheck.js b/packages/rollup-plugin-babel/src/preflightCheck.js index 3abfeed0f..e0f90b869 100644 --- a/packages/rollup-plugin-babel/src/preflightCheck.js +++ b/packages/rollup-plugin-babel/src/preflightCheck.js @@ -22,7 +22,7 @@ export default function preflightCheck ( options, dir ) { if ( !~check.indexOf( 'export default' ) && !~check.indexOf( 'export { Foo as default }' ) ) throw new Error( 'It looks like your Babel configuration specifies a module transformer. Please disable it. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more information' ); - if ( ~check.indexOf( 'import _classCallCheck from "babel-runtime' ) ) helpers = RUNTIME; + if ( /import _classCallCheck from ["']babel-runtime/.test( check ) ) helpers = RUNTIME; else if ( ~check.indexOf( 'function _classCallCheck' ) ) helpers = INLINE; else if ( ~check.indexOf( 'babelHelpers' ) ) helpers = BUNDLED; diff --git a/packages/rollup-plugin-babel/test/samples/runtime-helpers-duplicated/.babelrc b/packages/rollup-plugin-babel/test/samples/runtime-helpers-duplicated/.babelrc new file mode 100644 index 000000000..30d8df05e --- /dev/null +++ b/packages/rollup-plugin-babel/test/samples/runtime-helpers-duplicated/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": [ "es2015-rollup" ], + "plugins": "transform-runtime" +} diff --git a/packages/rollup-plugin-babel/test/samples/runtime-helpers-duplicated/Bar.js b/packages/rollup-plugin-babel/test/samples/runtime-helpers-duplicated/Bar.js new file mode 100644 index 000000000..87c1ff593 --- /dev/null +++ b/packages/rollup-plugin-babel/test/samples/runtime-helpers-duplicated/Bar.js @@ -0,0 +1 @@ +export default class Bar {} diff --git a/packages/rollup-plugin-babel/test/samples/runtime-helpers-duplicated/main.js b/packages/rollup-plugin-babel/test/samples/runtime-helpers-duplicated/main.js new file mode 100644 index 000000000..e1cd92dd2 --- /dev/null +++ b/packages/rollup-plugin-babel/test/samples/runtime-helpers-duplicated/main.js @@ -0,0 +1,5 @@ +import Bar from './Bar.js'; + +class Foo {} + +export { Foo, Bar }; diff --git a/packages/rollup-plugin-babel/test/test.js b/packages/rollup-plugin-babel/test/test.js index 45def6001..07d21ae62 100644 --- a/packages/rollup-plugin-babel/test/test.js +++ b/packages/rollup-plugin-babel/test/test.js @@ -213,4 +213,20 @@ describe( 'rollup-plugin-babel', function () { assert.equal( generated.code.indexOf( 'var typeof' ), -1, generated.code ); }); }); + + it( 'does not warn about duplicated helpers with transform-runtime', () => { + return rollup.rollup({ + entry: 'samples/runtime-helpers-duplicated/main.js', + plugins: [ + babelPlugin({ + runtimeHelpers: true + }) + ], + onwarn ( msg ) { + if ( /helper is used more than once in your code/.test( msg ) ) { + throw new Error( 'no warning about duplicated helpers should be given' ); + } + } + }); + }); });