Skip to content

Commit

Permalink
Merge pull request #107 from rollup/gh-105
Browse files Browse the repository at this point in the history
fix duplicated helper warnings with runtime helpers
  • Loading branch information
Rich-Harris authored and Andarist committed Dec 25, 2019
1 parent 36135d1 commit 1297d16
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
14 changes: 9 additions & 5 deletions packages/rollup-plugin-babel/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand Down Expand Up @@ -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 ] ) {
Expand Down
2 changes: 1 addition & 1 deletion packages/rollup-plugin-babel/src/preflightCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": [ "es2015-rollup" ],
"plugins": "transform-runtime"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class Bar {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Bar from './Bar.js';

class Foo {}

export { Foo, Bar };
16 changes: 16 additions & 0 deletions packages/rollup-plugin-babel/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
}
});
});
});

0 comments on commit 1297d16

Please sign in to comment.