Package your commonJS modules for browsers
// entry.js
var o = require("./o")
o.doIt()
// o.js
module.exports = {
doIt: function () {
console.log("you did it")
}
}
Compiles to
(function () {
var c={} // var cache = {}
function r(t) { // function require(token) {
return c[t] // return cache[token]
}
function d(t,f,m) { // function define(token, func, module) {
m={} // module = {}
f(r, m) // func(require, module)
c[t] = m.exports // cache[token] = module.exports
}
d("b", function (require, module) { // define("./o", ...
module.exports = {
doIt: function () {
console.log("you did it")
}
}
})
d("a", function (require, module) { // define("./entry", ...
var o = require("b") // var o = require("./o")
o.doIt()
})
}())
All handle this in a similar fashion but they give a large overhead in terms of extra bytes in the compiled file
To cut corners on byte size the following techniques are used
- require urls are normalized on the server and then mapped to minimized strings
- the require/define functions are as small as possible
- only module.exports is supported, the exports token doesn't exist
TBD
$ npm install tiny-require
$ make test