0.1.6 • Published 7 years ago
web-module-loader v0.1.6
Web module loader
This package loads commonjs modules at runtime. It's intended to immitate node's module loading in the browser. It will also work within node.
Installation
npm install --save web-module-loaderUsage
There are two APIs:
loader- A higher-level API that allows providing top-level modules by name (e.g.moment), and registers exports as scripts are evaluated.evaluate- A lower-level API that allows passing arequirefunction and then evaluating a script that can look up modules by calling the providedrequire.
Loader
Requiring top-level modules
If moment is installed, for example:
import loader from "web-module-loader";
import moment from "moment";
const context = loader({ moment });
const script = `
const moment = require('moment');
module.exports = moment(1111111111111).format('MM/DD/YY');
`;
const result = context()(script);
console.log(result); // => 03/17/05Multiple scripts
Scripts can require one another by relative path:
import loader from "web-module-loader";
const context = loader();
const scriptA = "module.exports = 1;";
const scriptB = "module.exports = 2 + require('./a');";
const scriptC = "module.exports = 3 + require('../b');";
context("a.js")(scriptA);
context("b.js")(scriptB);
const result = context("foo/c.js")(scriptC);
console.log(result); // => 6;Evaluate
Exporting values
import { evaluate } from "web-module-loader";
const result = evaluate()("module.exports = 3;");
console.log(result); // => 3Requiring modules
If moment is installed, for example:
import { evaluate } from "web-module-loader";
import moment from "moment";
const require = name => {
switch (name) {
case "moment":
return moment;
default:
throw new Error(`Module ${name} not found!`);
}
};
const script = `
module.exports = moment(1111111111111).format('MM/DD/YY');
`;
const result = evaluate(require)(script);
console.log(result); // => 03/17/05Storing exported modules
If you want to evaluate multiple files that can require one another, consider creating an object to store exports by name:
import { evaluate } from "web-module-loader";
const moduleMap = {};
const require = name => {
if (name in moduleMap) {
return moduleMap[name];
}
throw new Error(`Module ${name} not found!`);
};
const scriptA = "module.exports = 3;";
const scriptB = "module.exports = 4 + require('a')";
const bundler = evaluate(require);
moduleMap.a = bundler(scriptA);
moduleMap.b = bundler(scriptB);
console.log(moduleMap.b); // => 7