From 509502b17016edc27af5808624986557197e57e7 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Fri, 1 Jan 2016 16:12:09 -0500 Subject: [PATCH] fix failing preflight check in absence of class transformer (closes #23) --- packages/rollup-plugin-babel/package.json | 1 + packages/rollup-plugin-babel/src/index.js | 33 ++++++++++++++----- .../samples/no-class-transformer/.babelrc | 1 + .../test/samples/no-class-transformer/main.js | 1 + packages/rollup-plugin-babel/test/test.js | 7 ++++ 5 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 packages/rollup-plugin-babel/test/samples/no-class-transformer/.babelrc create mode 100644 packages/rollup-plugin-babel/test/samples/no-class-transformer/main.js diff --git a/packages/rollup-plugin-babel/package.json b/packages/rollup-plugin-babel/package.json index d60bedba7..05fb68a3a 100644 --- a/packages/rollup-plugin-babel/package.json +++ b/packages/rollup-plugin-babel/package.json @@ -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" }, diff --git a/packages/rollup-plugin-babel/src/index.js b/packages/rollup-plugin-babel/src/index.js index ab61ca2e9..8a71ef0ac 100644 --- a/packages/rollup-plugin-babel/src/index.js +++ b/packages/rollup-plugin-babel/src/index.js @@ -1,3 +1,4 @@ +import { dirname, join } from 'path'; import { buildExternalHelpers, transform } from 'babel-core'; import { createFilter } from 'rollup-pluginutils'; @@ -5,16 +6,32 @@ 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 ) { @@ -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; diff --git a/packages/rollup-plugin-babel/test/samples/no-class-transformer/.babelrc b/packages/rollup-plugin-babel/test/samples/no-class-transformer/.babelrc new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/packages/rollup-plugin-babel/test/samples/no-class-transformer/.babelrc @@ -0,0 +1 @@ +{} diff --git a/packages/rollup-plugin-babel/test/samples/no-class-transformer/main.js b/packages/rollup-plugin-babel/test/samples/no-class-transformer/main.js new file mode 100644 index 000000000..17eee8f1f --- /dev/null +++ b/packages/rollup-plugin-babel/test/samples/no-class-transformer/main.js @@ -0,0 +1 @@ +assert.equal( 1 + 1, 2 ); diff --git a/packages/rollup-plugin-babel/test/test.js b/packages/rollup-plugin-babel/test/test.js index 8d4ff1a6e..c2e518a23 100644 --- a/packages/rollup-plugin-babel/test/test.js +++ b/packages/rollup-plugin-babel/test/test.js @@ -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() ] + }); + }); });