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 #62 from rollup/consume-umd
Browse files Browse the repository at this point in the history
Transform typeof require to 'function'
  • Loading branch information
Rich-Harris authored Jun 24, 2016
2 parents aa436f7 + df46ca6 commit 7a53b09
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,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 ) && isReference( node, parent ) && !scope.contains( node.name ) ) {
uses[ node.name ] = true;
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;
}

function getLocation ( source, charIndex ) {
Expand Down Expand Up @@ -47,7 +48,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 @@ -56,7 +57,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 @@ -65,7 +66,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 @@ -258,7 +259,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 @@ -280,12 +281,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 );
});
});
});

Expand Down

0 comments on commit 7a53b09

Please sign in to comment.