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

Transform typeof require to 'function' #62

Merged
merged 1 commit into from
Jun 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ export default function commonjs ( options = {} ) {
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 ( ( node.name in uses && !uses[ node.name ] ) && isReference( node, parent ) && !scope.contains( node.name ) ) uses[ node.name ] = true;
return;
Expand Down
5 changes: 5 additions & 0 deletions test/samples/umd/correct-scoping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if ( typeof require === 'function' ) {
module.exports = function ( require ) {
return typeof require;
}( {} );
}
11 changes: 11 additions & 0 deletions test/samples/umd/protobuf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// From https://github.com/rollup/rollup-plugin-commonjs/issues/38
(function(global, factory) {
/* AMD */ if (typeof define === 'function' && define["amd"])
define(["bytebuffer"], factory);
/* CommonJS */ else if (typeof require === "function" && typeof module === "object" && module && module["exports"])
module["exports"] = factory(require("bytebuffer"), true);
/* Global */ else
(global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = factory(global["dcodeIO"]["ByteBuffer"]);
})(this, function(ByteBuffer, isCommonJS) {
return isCommonJS;
})
40 changes: 40 additions & 0 deletions test/samples/umd/sinon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// From https://github.com/rollup/rollup-plugin-commonjs/issues/38
var sinon = (function () { // eslint-disable-line no-unused-vars
"use strict";

var sinonModule;
var isNode = typeof module !== "undefined" && module.exports && typeof require === "function";
var isAMD = typeof define === "function" && typeof define.amd === "object" && define.amd;

function loadDependencies(require, exports, module) {
sinonModule = module.exports = require("./sinon/util/core");
require("./sinon/extend");
require("./sinon/walk");
require("./sinon/typeOf");
require("./sinon/times_in_words");
require("./sinon/spy");
require("./sinon/call");
require("./sinon/behavior");
require("./sinon/stub");
require("./sinon/mock");
require("./sinon/collection");
require("./sinon/assert");
require("./sinon/sandbox");
require("./sinon/test");
require("./sinon/test_case");
require("./sinon/match");
require("./sinon/format");
require("./sinon/log_error");
}

if (isAMD) {
define(loadDependencies);
} else if (isNode) {
loadDependencies(require, module.exports, module);
sinonModule = module.exports;
} else {
sinonModule = {};
}

return sinonModule;
}());
53 changes: 41 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ function executeBundle ( bundle ) {
format: 'cjs'
});

const fn = new Function ( 'module', 'assert', generated.code );
let module = {};
const fn = new Function( 'module', 'exports', 'require', 'assert', generated.code );

fn( module, assert );
const module = { exports: {} };

return module;
fn( module, module.exports, () => {}, assert );

return module.exports;
}

describe( 'rollup-plugin-commonjs', () => {
Expand All @@ -26,7 +27,7 @@ describe( 'rollup-plugin-commonjs', () => {
entry: 'samples/basic/main.js',
plugins: [ commonjs() ]
}).then( bundle => {
assert.equal( executeBundle( bundle ).exports, 42 );
assert.equal( executeBundle( bundle ), 42 );
});
});

Expand All @@ -35,7 +36,7 @@ describe( 'rollup-plugin-commonjs', () => {
entry: 'samples/exports/main.js',
plugins: [ commonjs() ]
}).then( bundle => {
assert.equal( executeBundle( bundle ).exports, 'BARBAZ' );
assert.equal( executeBundle( bundle ), 'BARBAZ' );
});
});

Expand All @@ -44,7 +45,7 @@ describe( 'rollup-plugin-commonjs', () => {
entry: 'samples/inline/main.js',
plugins: [ commonjs() ]
}).then( bundle => {
assert.equal( executeBundle( bundle ).exports(), 2 );
assert.equal( executeBundle( bundle )(), 2 );
});
});

Expand Down Expand Up @@ -224,7 +225,7 @@ describe( 'rollup-plugin-commonjs', () => {
entry: 'samples/extension/main.coffee',
plugins: [ commonjs({ extensions: ['.coffee' ]}) ]
}).then( bundle => {
assert.equal( executeBundle( bundle ).exports, 42 );
assert.equal( executeBundle( bundle ), 42 );
});
});

Expand All @@ -246,12 +247,40 @@ describe( 'rollup-plugin-commonjs', () => {
format: 'cjs'
});

let mod = {};
assert.equal( global.setImmediate, executeBundle( bundle ).immediate, generated.code );
});
});

describe( 'typeof transforms', () => {
it( 'correct-scoping', () => {
return rollup({
entry: 'samples/umd/correct-scoping.js',
plugins: [ commonjs() ]
}).then( bundle => {
assert.equal( executeBundle( bundle ), 'object' );
});
});

it( 'protobuf', () => {
return rollup({
entry: 'samples/umd/protobuf.js',
plugins: [ commonjs() ]
}).then( bundle => {
assert.equal( executeBundle( bundle ), true );
});
});

const fn = new Function ( 'exports', generated.code );
fn( mod );
it( 'sinon', () => {
return rollup({
entry: 'samples/umd/sinon.js',
plugins: [ commonjs() ]
}).then( bundle => {
const code = bundle.generate().code;

assert.equal( global.setImmediate, mod.immediate, generated.code );
assert.equal( code.indexOf( 'typeof require' ), -1, code );
assert.notEqual( code.indexOf( 'typeof module' ), -1, code );
assert.notEqual( code.indexOf( 'typeof define' ), -1, code );
});
});
});
});