From 95219a4be5e21a2820af44db3ad17d6abf72d3d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersva=CC=88rd?= Date: Tue, 1 Dec 2015 10:36:55 +0100 Subject: [PATCH 1/2] Add support for transpiled modules --- src/index.js | 13 +++++++++++-- test/samples/corejs/literal-with-default.js | 1 + test/test.js | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 test/samples/corejs/literal-with-default.js diff --git a/src/index.js b/src/index.js index 99adb11..99a2942 100644 --- a/src/index.js +++ b/src/index.js @@ -134,8 +134,17 @@ export default function commonjs ( options = {} ) { sources.map( source => `import ${required[ source ].name} from '${source}';` ).join( '\n' ) : ''; - const intro = `\n\nvar ${name} = (function (module${usesGlobal ? ', global' : ''}) {\nvar exports = module.exports;\n`; - let outro = `\nreturn module.exports;\n})({exports:{}}${usesGlobal ? ', __commonjs_global' : ''});\n\nexport default ${name};\n`; + const intro = ` + +var ${name} = (function (module${usesGlobal ? ', global' : ''}) { +var exports = module.exports; +`; + + let outro = ` +return module.exports; +})({exports:{}}${usesGlobal ? ', __commonjs_global' : ''}); + +export default (${name} && typeof ${name} === 'object' && 'default' in ${name} ? ${name}['default'] : ${name});\n`; outro += Object.keys( namedExports ).map( x => `export var ${x} = ${name}.${x};` ).join( '\n' ); diff --git a/test/samples/corejs/literal-with-default.js b/test/samples/corejs/literal-with-default.js new file mode 100644 index 0000000..6d4f280 --- /dev/null +++ b/test/samples/corejs/literal-with-default.js @@ -0,0 +1 @@ +module.exports = { default: 'foobar', __esModule: true }; diff --git a/test/test.js b/test/test.js index f09e218..932a975 100644 --- a/test/test.js +++ b/test/test.js @@ -189,4 +189,22 @@ describe( 'rollup-plugin-commonjs', function () { assert.equal( window.foo, 'bar', generated.code ); }); }); + + it( 'handles transpiled CommonJS modules', function () { + return rollup.rollup({ + entry: 'samples/corejs/literal-with-default.js', + plugins: [ commonjs() ] + }).then( function ( bundle ) { + var generated = bundle.generate({ + format: 'cjs' + }); + + var module = {}; + + var fn = new Function ( 'module', generated.code ); + fn( module ); + + assert.equal( module.exports, 'foobar', generated.code ); + }); + }); }); From c191a130190811f9e498e1805f16294a8a38e383 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 22 Dec 2015 18:42:39 -0500 Subject: [PATCH 2/2] dont export __esModule --- src/index.js | 10 ++++++++- test/samples/__esModule/answer.js | 2 ++ test/samples/__esModule/main.js | 2 ++ test/test.js | 36 +++++++++++++++++-------------- 4 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 test/samples/__esModule/answer.js create mode 100644 test/samples/__esModule/main.js diff --git a/src/index.js b/src/index.js index 01b156c..9959794 100644 --- a/src/index.js +++ b/src/index.js @@ -9,6 +9,11 @@ import { flatten, isReference } from './ast-utils.js'; var firstpass = /\b(?:require|module|exports|global)\b/; var exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/; +var blacklistedExports = { + __esModule: true, + default: true +}; + function getName ( id ) { const base = basename( id ); const ext = extname( base ); @@ -154,7 +159,10 @@ return module.exports; export default (${name} && typeof ${name} === 'object' && 'default' in ${name} ? ${name}['default'] : ${name});\n`; - outro += Object.keys( namedExports ).map( x => `export var ${x} = ${name}.${x};` ).join( '\n' ); + outro += Object.keys( namedExports ) + .filter( key => !blacklistedExports[ key ] ) + .map( x => `export var ${x} = ${name}.${x};` ) + .join( '\n' ); magicString.trim() .prepend( importBlock + intro ) diff --git a/test/samples/__esModule/answer.js b/test/samples/__esModule/answer.js new file mode 100644 index 0000000..135547a --- /dev/null +++ b/test/samples/__esModule/answer.js @@ -0,0 +1,2 @@ +exports.__esModule = true; +exports.answer = 42; diff --git a/test/samples/__esModule/main.js b/test/samples/__esModule/main.js new file mode 100644 index 0000000..135547a --- /dev/null +++ b/test/samples/__esModule/main.js @@ -0,0 +1,2 @@ +exports.__esModule = true; +exports.answer = 42; diff --git a/test/test.js b/test/test.js index d02f4cc..bb9d501 100644 --- a/test/test.js +++ b/test/test.js @@ -172,53 +172,57 @@ describe( 'rollup-plugin-commonjs', function () { }); }); - it( 'identifies named exports from object literals', function () { + it( 'handles references to `global`', function () { return rollup.rollup({ - entry: 'samples/named-exports-from-object-literal/main.js', + entry: 'samples/global/main.js', plugins: [ commonjs() ] }).then( function ( bundle ) { var generated = bundle.generate({ format: 'cjs' }); - var fn = new Function ( 'module', 'assert', generated.code ); - fn( {}, assert ); + var window = {}; + + var fn = new Function ( 'window', 'module', generated.code ); + fn( window, {} ); + + assert.equal( window.foo, 'bar', generated.code ); }); }); - it( 'handles references to `global`', function () { + it( 'handles transpiled CommonJS modules', function () { return rollup.rollup({ - entry: 'samples/global/main.js', + entry: 'samples/corejs/literal-with-default.js', plugins: [ commonjs() ] }).then( function ( bundle ) { var generated = bundle.generate({ format: 'cjs' }); - var window = {}; + var module = { exports: {} }; - var fn = new Function ( 'window', 'module', generated.code ); - fn( window, {} ); + var fn = new Function ( 'module', 'exports', generated.code ); + fn( module, module.exports ); - assert.equal( window.foo, 'bar', generated.code ); + assert.equal( module.exports, 'foobar', generated.code ); }); }); - it( 'handles transpiled CommonJS modules', function () { + it( 'does not export __esModule', function () { return rollup.rollup({ - entry: 'samples/corejs/literal-with-default.js', + entry: 'samples/__esModule/main.js', plugins: [ commonjs() ] }).then( function ( bundle ) { var generated = bundle.generate({ format: 'cjs' }); - var module = {}; + var fn = new Function ( 'module', 'exports', generated.code ); + var module = { exports: {} }; - var fn = new Function ( 'module', generated.code ); - fn( module ); + fn( module, module.exports ); - assert.equal( module.exports, 'foobar', generated.code ); + assert.ok( !module.exports.__esModule ); }); }); });