From 0f5a9048aafd628f349b294f2e0474e4afd7ffef Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 7 Mar 2018 09:49:46 -0500 Subject: [PATCH] Allow global exports to run in Node Fixes #952 --- lib/bundle/add_global_shim.js | 2 +- test/export_standalone_test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/bundle/add_global_shim.js b/lib/bundle/add_global_shim.js index 2b8be8ed..0dd7cbbc 100644 --- a/lib/bundle/add_global_shim.js +++ b/lib/bundle/add_global_shim.js @@ -13,7 +13,7 @@ module.exports = function(bundle, options){ var shimEval = fs.readFileSync(path.join(__dirname, "shim-eval.js")); // target global variable, self (Web Workers) or window - var g = `typeof self == "object" && self.Object == Object ? self : window`; + var g = `typeof self == "object" && self.Object == Object ? self : (typeof process === "object" && Object.prototype.toString.call(process) === "[object process]") ? global : window`; var source = prettier.format( `(${shim.toString()})( diff --git a/test/export_standalone_test.js b/test/export_standalone_test.js index 0bccc1d8..cba43470 100644 --- a/test/export_standalone_test.js +++ b/test/export_standalone_test.js @@ -62,4 +62,32 @@ describe("+standalone", function(){ }, done); }); }); + + it("Can be used for node.js projects", function(done){ + this.timeout(10000); + + var outPath = __dirname + "/exports_basics/out.js"; + + stealExport({ + steal: { + config: __dirname + "/exports_basics/package.json!npm" + }, + options: { quiet: true }, + outputs: { + "+standalone": { + exports: { "foo": "FOO.foo" }, + dest: function(){ + return outPath; + } + } + } + }) + .then(function(){ + require(outPath); + assert.ok(true, "Was able to require the created module"); + done(); + }) + .catch(done); + + }); });