Skip to content
Permalink
Browse files

Merge pull request #16 from rollup/handle-transpiled-modules

Add support for transpiled modules
  • Loading branch information...
Rich-Harris committed Dec 24, 2015
2 parents a05e059 + c191a13 commit 25e4cf07d753c7015651f7ab6b506363bd54fbee
Showing with 57 additions and 13 deletions.
  1. +20 −3 src/index.js
  2. +2 −0 test/samples/__esModule/answer.js
  3. +2 −0 test/samples/__esModule/main.js
  4. +1 −0 test/samples/corejs/literal-with-default.js
  5. +32 −10 test/test.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 )
@@ -0,0 +1,2 @@
exports.__esModule = true;
exports.answer = 42;
@@ -0,0 +1,2 @@
exports.__esModule = true;
exports.answer = 42;
@@ -0,0 +1 @@
module.exports = { default: 'foobar', __esModule: true };
@@ -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 );
});
});
});

0 comments on commit 25e4cf0

Please sign in to comment.
You can’t perform that action at this time.