On Github matthewp / modules-in-the-browser
By Matthew Phillips of Bitovi
<script src="jquery.js"><script/>
<script src="jquery.datepicker.js"><script/>
<script>
$(function(){
$("#date").datePicker();
});
</script>
window.app = {};
app.math = {
add: function(a, b) { return a + b; },
subtract: function(a, b) { return b - a; }
};
var math = app.math;
app.dates = {
compare: function(a, b) { return math.subtract(a, b) > 0; }
};
<script src="jquery.js"></script>
<script src="jquery.datepicker.js"></script>
<script src="app.js"></script>
<script src="math.js"></script>
<script src="dates.js"></script>
<script src="main.js"></script>
<script src="jquery.js"></script>
<script src="jquery.datepicker.js"></script>
<script src="app.js"></script>
<script src="math.js"></script>
<script src="utils.js"></script>
<script src="dates.js"></script>
<script src="tabs.js"></script>
<script src="main.js"></script>
math.js
dates.js
math.js utils.js dates.js dates.js math.js main.js tabs.jsutils.js
jquery.js
main.js main.jsdates.js
jquery.js
jquery.datepicker.js
tabs.js
define([
"jquery",
"math",
"utils",
"jquery-datepicker"
], function($, math, utils){
...
});
var fs = require("fs");
module.exports = function(){
var json = fs.readFileSync("config.json", "utf8");
return JSON.parse(json);
};
var config = require("config");
// Yay I have config!
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['exports', 'b'], function (exports, b) {
factory((root.commonJsStrictGlobal = exports), b);
});
} else if (typeof exports === 'object') {
// CommonJS
factory(exports, require('b'));
} else {
// Browser globals
factory((root.commonJsStrictGlobal = {}), root.b);
}
}(this, function (exports, b) {
//use b in some fashion.
// attach properties to the exports object to define
// the exported module properties.
exports.action = function () {};
}));
export default function(a, b){
a + b;
};
import add from "math";
add(1, 2); // -> 3
export default function(){
...
};
export function add(a, b){
return a + b;
};
export function subtract(a, b){
return b - a;
};
import { add, subtract } from "math";
add(1, 2); // -> 3
subtract(1, 3); // -> 2
import * as math from "math";
math.add(1, 2); // -> 3
math.subtract(1, 3); // -> 2
Loading modules on-demand.
var pageNum = getPageNumber();
System.import("page-" + pageNum).then(page => {
// do something with page
});
const $ = await import.default("jquery");
const { ajax } = await import.namespace("jquery");
import math from "./math";
// Step 1
loader.normalize("./math", "app/main", "http://example.com/app/main.js");
// Step 2
loader.locate({ name: "utils/math" });
// Step 3
loader.fetch({
name: "utils/math",
address: "http://example.com/utils/math.js"
});
// Step 4
loader.translate({
name: "utils/math",
address: "http://example.com/utils/math.js",
"source": "export function add(){ ..."
});
// Step 5
loader.instantiate({
name: "utils/math",
address: "http://example.com/utils/math.js",
"source": "export function add(){ ..."
});