Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for transpiled modules #16

Merged
merged 3 commits into from Dec 24, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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 );
});
});
});
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.