From 2af3fe3bb6d241d077f216c4bb711c59aa4069d0 Mon Sep 17 00:00:00 2001 From: Benoit Tremblay Date: Tue, 1 May 2018 10:36:43 -0400 Subject: [PATCH] Expose modules as UMD (#453) --- .eslintrc.json | 3 ++- src/Bundler.js | 1 + src/builtins/prelude.js | 23 +++++++++++++++++++- src/cli.js | 3 +++ src/packagers/JSPackager.js | 8 ++++++- test/integration/entry-point/index.js | 5 +++++ test/integration/entry-point/test.html | 3 +++ test/javascript.js | 29 ++++++++++++++++++++++++++ 8 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 test/integration/entry-point/index.js create mode 100644 test/integration/entry-point/test.html diff --git a/.eslintrc.json b/.eslintrc.json index 34abf94fb76..99af1fe5342 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -11,6 +11,7 @@ "es6": true }, "globals": { - "parcelRequire": true + "parcelRequire": true, + "define": true } } diff --git a/src/Bundler.js b/src/Bundler.js index 376955b658c..b650280c337 100644 --- a/src/Bundler.js +++ b/src/Bundler.js @@ -99,6 +99,7 @@ class Bundler extends EventEmitter { options.hmrHostname || (options.target === 'electron' ? 'localhost' : ''), detailedReport: options.detailedReport || false, + global: options.global, autoinstall: typeof options.autoinstall === 'boolean' ? options.autoinstall diff --git a/src/builtins/prelude.js b/src/builtins/prelude.js index 534e4bec1d2..d0026a7ece3 100644 --- a/src/builtins/prelude.js +++ b/src/builtins/prelude.js @@ -7,7 +7,7 @@ // orig method which is the require for previous bundles // eslint-disable-next-line no-global-assign -parcelRequire = (function (modules, cache, entry) { +parcelRequire = (function (modules, cache, entry, globalName) { // Save the require from previous bundle to this closure if any var previousRequire = typeof parcelRequire === 'function' && parcelRequire; var nodeRequire = typeof require === 'function' && require; @@ -75,6 +75,27 @@ parcelRequire = (function (modules, cache, entry) { newRequire(entry[i]); } + if (entry.length) { + // Expose entry point to Node, AMD or browser globals + // Based on https://github.com/ForbesLindesay/umd/blob/master/template.js + var mainExports = newRequire(entry[entry.length - 1]); + + // CommonJS + if (typeof exports === "object" && typeof module !== "undefined") { + module.exports = mainExports; + + // RequireJS + } else if (typeof define === "function" && define.amd) { + define(function () { + return mainExports; + }); + + // + diff --git a/test/javascript.js b/test/javascript.js index c9a9c8bb997..c05d3a704a4 100644 --- a/test/javascript.js +++ b/test/javascript.js @@ -834,4 +834,33 @@ describe('javascript', function() { assert.equal(failed, false); }); + + it('should expose to CommonJS entry point', async function() { + let b = await bundle(__dirname + '/integration/entry-point/index.js'); + + let module = {}; + run(b, {module, exports: {}}); + assert.equal(module.exports(), 'Test!'); + }); + + it('should expose to RequireJS entry point', async function() { + let b = await bundle(__dirname + '/integration/entry-point/index.js'); + let test; + const mockDefine = function(f) { + test = f(); + }; + mockDefine.amd = true; + + run(b, {define: mockDefine}); + assert.equal(test(), 'Test!'); + }); + + it('should expose variable with --browser-global', async function() { + let b = await bundle(__dirname + '/integration/entry-point/index.js', { + global: 'testing' + }); + + const ctx = run(b, null, {require: false}); + assert.equal(ctx.window.testing(), 'Test!'); + }); });