diff --git a/src/index.js b/src/index.js index 96cdec0..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 ); @@ -142,10 +147,22 @@ 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' ); + 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/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 231dd7c..bb9d501 100644 --- a/test/test.js +++ b/test/test.js @@ -172,35 +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( 'does not export __esModule', function () { + return rollup.rollup({ + entry: 'samples/__esModule/main.js', + plugins: [ commonjs() ] + }).then( function ( bundle ) { + var generated = bundle.generate({ + format: 'cjs' + }); + + var fn = new Function ( 'module', 'exports', generated.code ); + var module = { exports: {} }; + + fn( module, module.exports ); + + assert.ok( !module.exports.__esModule ); }); }); });