Skip to content

Commit

Permalink
fix failing preflight check in absence of class transformer (closes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris authored and Andarist committed Apr 11, 2020
1 parent d033e29 commit 18c148e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/rollup-plugin-babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"dependencies": {
"babel-core": "6",
"babel-plugin-transform-es2015-classes": "^6.3.15",
"object-assign": "^4.0.1",
"rollup-pluginutils": "^1.1.0"
},
Expand Down
33 changes: 25 additions & 8 deletions packages/rollup-plugin-babel/src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import { dirname, join } from 'path';
import { buildExternalHelpers, transform } from 'babel-core';
import { createFilter } from 'rollup-pluginutils';

const INLINE = {};
const RUNTIME = {};
const BUNDLED = {};

function preflightCheck ( localOpts ) {
var check = transform( 'export default class Foo {}', localOpts ).code;
let preflightCheckResults = {};

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. If you\'re using the "es2015" preset, consider using "es2015-rollup" instead. See https://github.com/rollup/rollup-plugin-babel#TK for more information' );
function preflightCheck ( options, dir ) {
let helpers;

if ( ~check.indexOf( 'import _classCallCheck from "babel-runtime' ) ) return RUNTIME;
if ( ~check.indexOf( 'function _classCallCheck' ) ) return INLINE;
if ( ~check.indexOf( 'babelHelpers' ) ) return BUNDLED;
if ( !preflightCheckResults[ dir ] ) {
options = assign( {}, options );
options.filename = join( dir, 'x.js' );

throw new Error( 'An unexpected situation arose. Please raise an issue at https://github.com/rollup/rollup-plugin-babel/issues. Thanks!' );
if ( !options.plugins ) options.plugins = [];
options.plugins.push( 'transform-es2015-classes' );

const check = transform( 'export default class Foo {}', options ).code;

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. If you\'re using the "es2015" preset, consider using "es2015-rollup" instead. See https://github.com/rollup/rollup-plugin-babel#configuring-babel for more information' );

if ( ~check.indexOf( 'import _classCallCheck from "babel-runtime' ) ) helpers = RUNTIME;
else if ( ~check.indexOf( 'function _classCallCheck' ) ) helpers = INLINE;
else if ( ~check.indexOf( 'babelHelpers' ) ) helpers = BUNDLED;

else {
throw new Error( 'An unexpected situation arose. Please raise an issue at https://github.com/rollup/rollup-plugin-babel/issues. Thanks!' );
}
}

return preflightCheckResults[ dir ] = helpers;
}

function assign ( target, source ) {
Expand Down Expand Up @@ -51,8 +68,8 @@ export default function babel ( options ) {
transform ( code, id ) {
if ( !filter( id ) ) return null;

const helpers = preflightCheck( options, dirname( id ) );
var localOpts = assign({ filename: id }, options );
const helpers = preflightCheck( localOpts );

var transformed = transform( code, localOpts );
const { usedHelpers } = transformed.metadata;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert.equal( 1 + 1, 2 );
7 changes: 7 additions & 0 deletions packages/rollup-plugin-babel/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,11 @@ describe( 'rollup-plugin-babel', function () {
assert.ok( !~cjs.indexOf( 'babelHelpers_get get' ), 'helper was incorrectly renamed' );
});
});

it( 'runs preflight check correctly in absence of class transformer (#23)', () => {
return rollup.rollup({
entry: 'samples/no-class-transformer/main.js',
plugins: [ babelPlugin() ]
});
});
});

0 comments on commit 18c148e

Please sign in to comment.