meteor-node-stubs
Version:
Stub implementations of Node built-in modules, a la Browserify
42 lines (38 loc) • 1.38 kB
JavaScript
var map = require("./map.json");
var meteorAliases = {};
Object.keys(map).forEach(function (id) {
if (typeof map[id] === "string") {
var aliasParts = module.id.split("/");
aliasParts.pop();
aliasParts.push("node_modules", map[id]);
exports[id] = meteorAliases[id + ".js"] = meteorAliases["node:" + id] =
aliasParts.join("/");
} else {
exports[id] = map[id];
meteorAliases[id + ".js"] = meteorAliases["node:" + id] = function(){};
}
});
if (typeof meteorInstall === "function") {
meteorInstall({
// Install the aliases into a node_modules directory one level up from
// the root directory, so that they do not clutter the namespace
// available to apps and packages.
"..": {
node_modules: meteorAliases
}
});
}
// If Buffer is not defined globally, but the "buffer" built-in stub is
// installed and can be imported, use it to define global.Buffer so that
// modules like core-util-is/lib/util.js can refer to Buffer without
// crashing application startup.
if (typeof global.Buffer !== "function") {
try {
// Use (0, require)(...) to avoid registering a dependency on the
// "buffer" stub, in case it is not otherwise bundled.
global.Buffer = (0, require)("buffer").Buffer;
} catch (ok) {
// Failure to import "buffer" is fine as long as the Buffer global
// variable is not used.
}
}