Skip to content

Commit

Permalink
Cache preflight check results per plugin's instance (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Apr 11, 2020
1 parent d5465fd commit 1bd586a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
3 changes: 2 additions & 1 deletion packages/rollup-plugin-babel/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dirname } from 'path';
import { buildExternalHelpers, transform } from '@babel/core';
import { createFilter } from 'rollup-pluginutils';
import preflightCheck from './preflightCheck.js';
import createPreflightCheck from './preflightCheck.js';
import helperPlugin from './helperPlugin.js';
import { warnOnce } from './utils.js';
import { RUNTIME, EXTERNAL, HELPERS } from './constants.js';
Expand Down Expand Up @@ -30,6 +30,7 @@ export default function babel ( options ) {
} = unpackOptions(options);

const filter = createFilter( include, exclude );
const preflightCheck = createPreflightCheck();

return {
name: 'babel',
Expand Down
62 changes: 32 additions & 30 deletions packages/rollup-plugin-babel/src/preflightCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,45 @@ function fallbackClassTransform () {
};
}

let preflightCheckResults = {};
export default function createPreflightCheck () {
let preflightCheckResults = {};

export default function preflightCheck ( ctx, options, dir ) {
if ( !preflightCheckResults[ dir ] ) {
let helpers;
return ( ctx, options, dir ) => {
if ( !preflightCheckResults[ dir ] ) {
let helpers;

options = Object.assign( {}, options );
delete options.only;
delete options.ignore;
options = Object.assign( {}, options );
delete options.only;
delete options.ignore;

options.filename = join( dir, 'x.js' );
options.filename = join( dir, 'x.js' );

const inputCode = 'class Foo extends Bar {};\nexport default Foo;';
let check = transform( inputCode, options ).code;
const inputCode = 'class Foo extends Bar {};\nexport default Foo;';
let check = transform( inputCode, options ).code;

if ( ~check.indexOf('class ') ) {
options.plugins = (options.plugins || []).concat( fallbackClassTransform );
check = transform( inputCode, options ).code;
}
if ( ~check.indexOf('class ') ) {
options.plugins = (options.plugins || []).concat( fallbackClassTransform );
check = transform( inputCode, options ).code;
}

if ( ~check.indexOf( 'import _inherits' ) ) helpers = RUNTIME;
else if ( ~check.indexOf( 'function _inherits' ) ) helpers = INLINE;
else if ( ~check.indexOf( 'babelHelpers' ) ) helpers = EXTERNAL;
else {
ctx.error( 'An unexpected situation arose. Please raise an issue at https://github.com/rollup/rollup-plugin-babel/issues. Thanks!' );
}
if ( ~check.indexOf( 'import _inherits' ) ) helpers = RUNTIME;
else if ( ~check.indexOf( 'function _inherits' ) ) helpers = INLINE;
else if ( ~check.indexOf( 'babelHelpers' ) ) helpers = EXTERNAL;
else {
ctx.error( 'An unexpected situation arose. Please raise an issue at https://github.com/rollup/rollup-plugin-babel/issues. Thanks!' );
}

if (
!~check.indexOf( 'export default' ) &&
!~check.indexOf( 'export default Foo' ) &&
!~check.indexOf( 'export { Foo as default }' )
) {
ctx.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( 'export default' ) &&
!~check.indexOf( 'export default Foo' ) &&
!~check.indexOf( 'export { Foo as default }' )
) {
ctx.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' );
}

preflightCheckResults[ dir ] = helpers;
}
preflightCheckResults[ dir ] = helpers;
}

return preflightCheckResults[ dir ];
return preflightCheckResults[ dir ];
};
}

0 comments on commit 1bd586a

Please sign in to comment.