Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
rewrite typeof module.exports etc as "object" (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 29, 2016
1 parent 16c4a9c commit 5ef1e6e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, cus
if ( node.scope ) scope = node.scope;
if ( /^Function/.test( node.type ) ) lexicalDepth += 1;

// rewrite `typeof module`, `typeof module.exports` and `typeof exports` (https://github.com/rollup/rollup-plugin-commonjs/issues/151)
if ( node.type === 'UnaryExpression' && node.operator === 'typeof' ) {
const flattened = flatten( node.argument );
if ( !flattened ) return;

if ( scope.contains( flattened.name ) ) return;

if ( flattened.keypath === 'module.exports' || flattened.keypath === 'module' || flattened.keypath === 'exports' ) {
magicString.overwrite( node.start, node.end, `'object'`, false );
}
}

// Is this an assignment to exports or module.exports?
if ( node.type === 'AssignmentExpression' ) {
if ( node.left.type !== 'MemberExpression' ) return;
Expand Down
9 changes: 9 additions & 0 deletions test/form/typeof-module-exports/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var foo = 42;

if ( typeof exports === 'object' && typeof module === 'object' ) {
module.exports = foo;
} else if ( typeof define === 'function' && define.amd ) {
define([], function () { return foo; });
} else {
window.foo = foo;
}
16 changes: 16 additions & 0 deletions test/form/typeof-module-exports/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as commonjsHelpers from 'commonjsHelpers';

var input = commonjsHelpers.createCommonjsModule(function (module, exports) {
var foo = 42;

if ( 'object' === 'object' && 'object' === 'object' ) {
module.exports = foo;
} else if ( typeof undefined === 'function' && undefined.amd ) {
undefined([], function () { return foo; });
} else {
window.foo = foo;
}
});

export default input;
export { input as __moduleExports };
7 changes: 4 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ describe( 'rollup-plugin-commonjs', () => {

( config.solo ? it.only : it )( dir, () => {
const input = fs.readFileSync( `form/${dir}/input.js`, 'utf-8' );
const expected = fs.readFileSync( `form/${dir}/output.js`, 'utf-8' );
const expected = fs.readFileSync( `form/${dir}/output.js`, 'utf-8' ).trim();

return transform( input, 'input.js' ).then( transformed => {
assert.equal( ( transformed ? transformed.code : input ).trim(), expected.trim() );
const actual = ( transformed ? transformed.code : input ).trim().replace( /\0/g, '' );
assert.equal( actual, expected );
});
});
});
Expand Down Expand Up @@ -298,7 +299,7 @@ describe( 'rollup-plugin-commonjs', () => {
const code = bundle.generate().code;

assert.equal( code.indexOf( 'typeof require' ), -1, code );
assert.notEqual( code.indexOf( 'typeof module' ), -1, code );
// assert.notEqual( code.indexOf( 'typeof module' ), -1, code ); // #151 breaks this test
// assert.notEqual( code.indexOf( 'typeof define' ), -1, code ); // #144 breaks this test
});
});
Expand Down

0 comments on commit 5ef1e6e

Please sign in to comment.