From 36343a489c9c9c4103a362ac1c9336010dc186a1 Mon Sep 17 00:00:00 2001 From: Jesse McCarthy Date: Mon, 2 Mar 2015 15:25:40 -0500 Subject: [PATCH 1/2] Add ability to expose require() with standalone. --- index.js | 50 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 1814347..022b861 100644 --- a/index.js +++ b/index.js @@ -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 + '({')); @@ -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) { From 41681a599dacc2016cee5c870750a9c531905d67 Mon Sep 17 00:00:00 2001 From: Jesse McCarthy Date: Tue, 3 Mar 2015 08:56:25 -0500 Subject: [PATCH 2/2] Eliminate pointless boolean coercion. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 022b861..33afb2d 100644 --- a/index.js +++ b/index.js @@ -117,7 +117,7 @@ module.exports = function (opts) { // Bundle was not wrapped with UMD because of hasExports. Now output // UMD prelude to wrap a require(standaloneModuleName) call. - if (!!stream.hasExports) { + if (stream.hasExports) { umdContent.src += ';\n' + umdContent.prelude +