New package implementing the runtime API for a CommonJS module system.

When used, the 'modules' package provides a CommonJS require/exports
module system via the exported meteorInstall function. Once that
underlying system is available, ECMAScript 2015 import/export syntax can
be easily compiled into require/exports.
This commit is contained in:
Ben Newman
2015-11-03 09:43:11 -05:00
parent fff2e46661
commit f727bb3c56
7 changed files with 79 additions and 0 deletions

View File

@@ -0,0 +1 @@
node_modules

View File

@@ -0,0 +1,7 @@
This directory and the files immediately inside it are automatically generated
when you change this package's NPM dependencies. Commit the files in this
directory (npm-shrinkwrap.json, .gitignore, and this README) to source control
so that others run the same versions of sub-dependencies.
You should NOT check in the node_modules directory that Meteor automatically
creates; if you are using git, the .gitignore file tells git to ignore it.

View File

@@ -0,0 +1,7 @@
{
"dependencies": {
"install": {
"version": "0.4.0"
}
}
}

View File

View File

@@ -0,0 +1,6 @@
Tinytest.add('modules', function (test) {
test.equal(typeof meteorInstall, "function");
var require = meteorInstall();
test.equal(typeof require, "function");
test.equal(typeof require.ensure, "function");
});

View File

@@ -0,0 +1,25 @@
var options = {};
// RegExp matching strings that don't start with a `.` or a `/`.
var topLevelIdPattern = /^[^./]/;
// This function will be called whenever a module identifier that hasn't
// been installed is required. For backwards compatibility, and so that we
// can require binary dependencies on the server, we implement the
// fallback in terms of Npm.require.
options.fallback = function (id, dir, error) {
// For simplicity, we honor only top-level module identifiers here.
// We could try to honor relative and absolute module identifiers by
// somehow combining `id` with `dir`, but we'd have to be really careful
// that the resulting modules were located in a known directory (not
// some arbitrary location on the file system), and we only really need
// the fallback for dependencies installed in node_modules directories.
if (topLevelIdPattern.test(id) &&
typeof Npm === "object" &&
typeof Npm.require === "function") {
return Npm.require(id);
}
throw error;
};
meteorInstall = makeInstaller(options);

View File

@@ -0,0 +1,33 @@
Package.describe({
name: "modules",
version: "0.4.0",
summary: "CommonJS module system",
git: "https://github.com/benjamn/install",
documentation: "README.md"
});
Npm.depends({
install: "0.4.0"
});
Package.onUse(function(api) {
api.use("meteor", {
unordered: true
});
api.addFiles(".npm/package/node_modules/install/install.js", [
"client",
"server"
], {
bare: true
});
api.addFiles("modules.js");
api.export("meteorInstall");
});
Package.onTest(function(api) {
api.use("tinytest");
api.use("modules");
api.addFiles("modules-tests.js");
});