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

Commit

Permalink
Merge pull request #108 from rollup/gh-77
Browse files Browse the repository at this point in the history
fix typeof require === "function" && require
  • Loading branch information
Rich-Harris committed Sep 17, 2016
2 parents 88e3966 + ed56c9b commit 0422ef9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export const HELPERS_ID = '\0commonjsHelpers';
export const HELPERS = `
export var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
export function commonjsRequire () {
throw new Error('Dynamic requires are not currently supported by rollup-plugin-commonjs');
}
export function unwrapExports (x) {
return x && x.__esModule ? x['default'] : x;
}
Expand Down
18 changes: 7 additions & 11 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
let uid = 0;

let scope = attachScopes( ast, 'scope' );
const uses = { module: false, exports: false, global: false };
const uses = { module: false, exports: false, global: false, require: false };

let lexicalDepth = 0;
let programDepth = 0;
Expand Down Expand Up @@ -126,16 +126,6 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
return;
}

// To allow consumption of UMD modules, transform `typeof require` to `'function'`
if ( node.type === 'UnaryExpression' && node.operator === 'typeof' && node.argument.type === 'Identifier' ) {
const name = node.argument.name;

if ( name === 'require' && !scope.contains( name ) ) {
magicString.overwrite( node.start, node.end, `'function'` );
return;
}
}

if ( node.type === 'Identifier' ) {
if ( isReference( node, parent ) && !scope.contains( node.name ) ) {
if ( node.name in uses ) {
Expand All @@ -144,6 +134,10 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
magicString.overwrite( node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, true );
}

if ( node.name === 'require' ) {
magicString.overwrite( node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, true );
}

// if module or exports are used outside the context of an assignment
// expression, we need to wrap the module
if ( node.name === 'module' || node.name === 'exports' ) {
Expand Down Expand Up @@ -189,6 +183,8 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
// is a bare import, e.g. `require('foo');`
magicString.remove( parent.start, parent.end );
}

node.callee._skip = true;
},

leave ( node ) {
Expand Down
5 changes: 5 additions & 0 deletions test/function/typeof-require/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if ( typeof require === 'function' && require ) {
module.exports = 1;
} else {
module.exports = 2;
}
3 changes: 3 additions & 0 deletions test/function/typeof-require/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import foo from './foo.js';

assert.equal( foo, 1 );

0 comments on commit 0422ef9

Please sign in to comment.