Skip to content

Commit

Permalink
Expose modules as UMD (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
eXon authored and devongovett committed May 1, 2018
1 parent fb6912d commit 2af3fe3
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"es6": true
},
"globals": {
"parcelRequire": true
"parcelRequire": true,
"define": true
}
}
1 change: 1 addition & 0 deletions src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion src/builtins/prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});

// <script>
} else if (globalName) {
this[globalName] = mainExports;
}
}

// Override the current require with this new one
return newRequire;
})
3 changes: 3 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ program
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--global <variable>', 'expose your module through a global variable')
.option('--no-hmr', 'disable hot module replacement')
.option('--no-cache', 'disable the filesystem cache')
.option('--no-source-maps', 'disable sourcemaps')
Expand Down Expand Up @@ -73,6 +74,7 @@ program
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--global <variable>', 'expose your module through a global variable')
.option(
'--hmr-port <port>',
'set the port to serve HMR websockets, defaults to random',
Expand Down Expand Up @@ -113,6 +115,7 @@ program
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--global <variable>', 'expose your module through a global variable')
.option('--no-minify', 'disable minification')
.option('--no-cache', 'disable the filesystem cache')
.option('--no-source-maps', 'disable sourcemaps')
Expand Down
8 changes: 7 additions & 1 deletion src/packagers/JSPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ class JSPackager extends Packager {
entry.push(this.bundle.entryAsset.id);
}

await this.write('},{},' + JSON.stringify(entry) + ')');
await this.dest.write(
'},{},' +
JSON.stringify(entry) +
', ' +
JSON.stringify(this.options.global || null) +
')'
);
if (this.options.sourceMaps) {
// Add source map url if a map bundle exists
let mapBundle = this.bundle.siblingBundlesMap.get('map');
Expand Down
5 changes: 5 additions & 0 deletions test/integration/entry-point/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function test() {
return 'Test!';
}

module.exports = test;
3 changes: 3 additions & 0 deletions test/integration/entry-point/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<html>
<script src="dist/index.js"></script>
</html>
29 changes: 29 additions & 0 deletions test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
});
});

0 comments on commit 2af3fe3

Please sign in to comment.