Skip to content

Expose require before initializing entries #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ module.exports = function (opts) {
var pre = umd.prelude(opts.standalone).trim();
stream.push(Buffer(pre + 'return '));
}
else if (first && stream.hasExports) {
var pre = opts.externalRequireName || 'require';
stream.push(Buffer(pre + '='));
}
if (first) stream.push(Buffer(prelude + '({'));

if (row.sourceFile && !row.nomap) {
Expand Down Expand Up @@ -96,7 +92,16 @@ module.exports = function (opts) {
if (first) stream.push(Buffer(prelude + '({'));
entries = entries.filter(function (x) { return x !== undefined });

stream.push(Buffer('},{},' + JSON.stringify(entries) + ')'));
var postlude = [
'}',
'{}',
JSON.stringify(entries),
'this', // this === window
stream.hasExports ? 'true' : 'false',
JSON.stringify(opts.externalRequireName || 'require')
].join(',');

stream.push(Buffer(postlude + ')'));

if (opts.standalone && !first) {
stream.push(Buffer(
Expand Down
12 changes: 8 additions & 4 deletions prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
// anything defined in a previous bundle is accessed via the
// orig method which is the requireuire for previous bundles

(function outer (modules, cache, entry) {
(function outer (modules, cache, entry, global, hasExports, erName) {
// Save the require from previous bundle to this closure if any
var previousRequire = typeof require == "function" && require;
var previousRequire = typeof global[erName] === "function" && global[erName];

function newRequire(name, jumped){
if(!cache[name]) {
if(!modules[name]) {
// if we cannot find the module within our internal map or
// cache jump to the current global require ie. the last bundle
// that was added to the page.
var currentRequire = typeof require == "function" && require;
var currentRequire = typeof global[erName] === "function" && global[erName];
if (!jumped && currentRequire) return currentRequire(name, true);

// If there are other bundles on this page the require from the
Expand All @@ -37,8 +37,12 @@
}
return cache[name].exports;
}

// Override the current require with this new one. This has to happend
// before we requires anything so cross bundle requiring works both ways.
if (hasExports) global[erName] = newRequire;

for(var i=0;i<entry.length;i++) newRequire(entry[i]);

// Override the current require with this new one
return newRequire;
})
43 changes: 43 additions & 0 deletions test/external.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var vm = require('vm');
var test = require('tap').test;
var pack = require('../');

test('external modules', function (t) {
t.plan(1);

var p1 = pack({ raw: true, hasExports: true });
var p2 = pack({ raw: true, hasExports: true });

var s1 = '';
var s2 = '';
p1.on('data', function (buf) { s1 += buf });
p2.on('data', function (buf) { s2 += buf });
p2.on('end', function () {
var context = vm.createContext({ });
vm.runInContext('require=' + s1, context);
vm.runInContext('require=' + s2, context);
t.equal(context.require('foo'), 'hello');
t.end();
});

p2.write({
id: 'foo',
source: 'module.exports = require("./bar")',
deps: { './bar': 'bar' },
entry: true
});

p1.write({
id: 'bar',
source: 'module.exports = require("./baz")',
deps: { './baz': 'baz' }
});

p2.write({
id: 'baz',
source: 'module.exports = "hello"'
});

p1.end();
p2.end();
});