Skip to content

Commit

Permalink
Merge pull request #85 from rollup/use-onwarn-if-available
Browse files Browse the repository at this point in the history
Use options.onwarn if available
  • Loading branch information
Rich-Harris authored and Andarist committed Dec 17, 2019
1 parent b91d7f3 commit 665ba1b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
8 changes: 7 additions & 1 deletion packages/rollup-plugin-babel/src/index.js
Expand Up @@ -26,9 +26,15 @@ export default function babel ( options ) {
if ( options.externalHelpers ) externalHelpers = true;
delete options.externalHelpers;

let warn = msg => console.warn(msg); // eslint-disable-line no-console

return {
name: 'babel',

options ( options ) {
warn = options.onwarn || warn;
},

resolveId ( id ) {
if ( id === HELPERS ) return id;
},
Expand Down Expand Up @@ -64,7 +70,7 @@ export default function babel ( options ) {
} else {
usedHelpers.forEach( helper => {
if ( inlineHelpers[ helper ] ) {
warnOnce( `The '${helper}' Babel helper is used more than once in your code. It's strongly recommended that you use the "external-helpers" plugin or the "es2015-rollup" preset. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more information` );
warnOnce( warn, `The '${helper}' Babel helper is used more than once in your code. It's strongly recommended that you use the "external-helpers" plugin or the "es2015-rollup" preset. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more information` );
}

inlineHelpers[ helper ] = true;
Expand Down
4 changes: 2 additions & 2 deletions packages/rollup-plugin-babel/src/utils.js
Expand Up @@ -6,8 +6,8 @@ export function assign ( target, source ) {
}

let warned = {};
export function warnOnce ( msg ) {
export function warnOnce ( warn, msg ) {
if ( warned[ msg ] ) return;
warned[ msg ] = true;
console.warn( msg ); // eslint-disable-line no-console
warn( msg );
}
5 changes: 2 additions & 3 deletions packages/rollup-plugin-babel/test/test.js
Expand Up @@ -179,13 +179,12 @@ describe( 'rollup-plugin-babel', function () {

it( 'warns on duplicated helpers', () => {
let messages = [];
console.warn = msg => messages.push( msg );

return rollup.rollup({
entry: 'samples/duplicated-helpers-warning/main.js',
plugins: [ babelPlugin() ]
plugins: [ babelPlugin() ],
onwarn: msg => messages.push( msg )
}).then( () => {
console.warn = consoleWarn;
assert.deepEqual( messages, [
'The \'classCallCheck\' Babel helper is used more than once in your code. It\'s strongly recommended that you use the "external-helpers" plugin or the "es2015-rollup" preset. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more information'
]);
Expand Down

0 comments on commit 665ba1b

Please sign in to comment.