Skip to content
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

Add ability to expose require() with standalone. #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
50 changes: 39 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,31 @@ module.exports = function (opts) {

var lineno = 1 + newlinesIn(prelude);
var sourcemap;


var externalRequireName = opts.externalRequireName || 'require';

if (opts.standalone) {
var umdContent = {
prelude: umd.prelude(opts.standalone).trim() + 'return (',
postlude: ');' + umd.postlude(opts.standalone)
};
}

return stream;

function write (row, enc, next) {
if (first && opts.standalone) {
var pre = umd.prelude(opts.standalone).trim();
stream.push(Buffer(pre + 'return '));
// If hasExports, output the bundle with an exposed require and no UMD
// wrapper, regardless of opts.standalone. See end() for UMD
// integration in this case.
if (first && stream.hasExports) {
stream.push(Buffer(externalRequireName + '='));
if (opts.standalone && row.expose !== undefined) {
stream.standaloneModule = row.expose;
}
}
else if (first && stream.hasExports) {
var pre = opts.externalRequireName || 'require';
stream.push(Buffer(pre + '='));
// If opts.standalone && !hasExports, wrap the bundle with UMD.
else if (first && opts.standalone) {
stream.push(Buffer(umdContent.prelude));
}
if (first) stream.push(Buffer(prelude + '({'));

Expand Down Expand Up @@ -99,10 +113,24 @@ module.exports = function (opts) {
stream.push(Buffer('},{},' + JSON.stringify(entries) + ')'));

if (opts.standalone) {
stream.push(Buffer(
'(' + JSON.stringify(stream.standaloneModule) + ')'
+ umd.postlude(opts.standalone)
));
umdContent.src = '';

// Bundle was not wrapped with UMD because of hasExports. Now output
// UMD prelude to wrap a require(standaloneModuleName) call.
if (stream.hasExports) {
umdContent.src +=
';\n' +
umdContent.prelude +
externalRequireName
;
}

umdContent.src +=
'(' + JSON.stringify(stream.standaloneModule) + ')' +
umdContent.postlude
;

stream.push(Buffer(umdContent.src));
}

if (sourcemap) {
Expand Down