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

Commit ed56c9b

Browse files
committed
fix typeof require === "function" && require (#77, #83)
1 parent 88e3966 commit ed56c9b

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

src/helpers.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ export const HELPERS_ID = '\0commonjsHelpers';
33
export const HELPERS = `
44
export var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
55
6+
export function commonjsRequire () {
7+
throw new Error('Dynamic requires are not currently supported by rollup-plugin-commonjs');
8+
}
9+
610
export function unwrapExports (x) {
711
return x && x.__esModule ? x['default'] : x;
812
}

src/transform.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
5959
let uid = 0;
6060

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

6464
let lexicalDepth = 0;
6565
let programDepth = 0;
@@ -126,16 +126,6 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
126126
return;
127127
}
128128

129-
// To allow consumption of UMD modules, transform `typeof require` to `'function'`
130-
if ( node.type === 'UnaryExpression' && node.operator === 'typeof' && node.argument.type === 'Identifier' ) {
131-
const name = node.argument.name;
132-
133-
if ( name === 'require' && !scope.contains( name ) ) {
134-
magicString.overwrite( node.start, node.end, `'function'` );
135-
return;
136-
}
137-
}
138-
139129
if ( node.type === 'Identifier' ) {
140130
if ( isReference( node, parent ) && !scope.contains( node.name ) ) {
141131
if ( node.name in uses ) {
@@ -144,6 +134,10 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
144134
magicString.overwrite( node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, true );
145135
}
146136

137+
if ( node.name === 'require' ) {
138+
magicString.overwrite( node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, true );
139+
}
140+
147141
// if module or exports are used outside the context of an assignment
148142
// expression, we need to wrap the module
149143
if ( node.name === 'module' || node.name === 'exports' ) {
@@ -189,6 +183,8 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
189183
// is a bare import, e.g. `require('foo');`
190184
magicString.remove( parent.start, parent.end );
191185
}
186+
187+
node.callee._skip = true;
192188
},
193189

194190
leave ( node ) {

test/function/typeof-require/foo.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if ( typeof require === 'function' && require ) {
2+
module.exports = 1;
3+
} else {
4+
module.exports = 2;
5+
}

test/function/typeof-require/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import foo from './foo.js';
2+
3+
assert.equal( foo, 1 );

0 commit comments

Comments
 (0)