Install stubs for Node built-in modules automatically.

The implementation of these stubs is controlled by an optional npm package
called meteor-node-stubs, so we can keep iterating on them after releasing
Meteor 1.3, if we need to.

Fixes #6056.
This commit is contained in:
Ben Newman
2016-02-27 17:35:36 -05:00
parent c51b8cf7ff
commit 2fa65685d6
7 changed files with 60 additions and 20 deletions

View File

@@ -1,7 +1,7 @@
{
"dependencies": {
"install": {
"version": "0.5.1"
"version": "0.5.2"
}
}
}

View File

@@ -7,7 +7,7 @@ Package.describe({
});
Npm.depends({
install: "0.5.1"
install: "0.5.2"
});
Package.onUse(function(api) {

View File

@@ -1,3 +1,4 @@
require("./stubs.js");
require("./buffer.js");
require("./process.js");

View File

@@ -0,0 +1,6 @@
try {
// When meteor-node-stubs is installed in the application's root
// node_modules directory, requiring it here installs aliases for stubs
// for all Node built-in modules, such as fs, util, and http.
require("meteor-node-stubs");
} catch (noStubs) {}

View File

@@ -25,7 +25,12 @@ Object.keys(process.binding("natives")).forEach(id => {
return;
}
nativeModulesMap[id] = true;
// When a native Node module is imported, we register a dependency on a
// meteor-node-stubs/deps/* module of the same name, so that the
// necessary stub modules will be included in the bundle. This alternate
// identifier will not be imported at runtime, but the modules it
// depends on are necessary for the original import to succeed.
nativeModulesMap[id] = "meteor-node-stubs/deps/" + id;
});
// Default handlers for well-known file extensions.
@@ -539,6 +544,10 @@ export default class ImportScanner {
}
if (! resolved) {
if (isNative && archMatches(this.bundleArch, "web")) {
id = nativeModulesMap[id];
}
// If the imported identifier is neither absolute nor relative, but
// top-level, then it might be satisfied by a package installed in
// the top-level node_modules directory, and we should record the

View File

@@ -4,15 +4,9 @@
"description": "Test app exercising many aspects of the Meteor module system.",
"private": true,
"dependencies": {
"buffer": "^4.4.0",
"events": "^1.1.0",
"meteor-node-stubs": "^0.1.0",
"moment": "2.11.1",
"path": "^0.12.7",
"regenerator": "^0.8.42",
"stream-browserify": "^2.0.1",
"stream-http": "^2.1.1",
"url": "^0.11.0",
"util": "^0.10.3"
"regenerator": "^0.8.42"
},
"scripts": {
"test": "METEOR_PROFILE=100 ../../../../meteor test-app --driver-package practicalmeteor:mocha"

View File

@@ -196,15 +196,45 @@ describe("native node_modules", () => {
assert.strictEqual(typeof Stream.Readable, "function");
});
Meteor.isClient &&
it("can be installed with aliases", () => {
meteorInstall({
node_modules: {
http: "stream-http"
}
});
assert.strictEqual(require("http"), require("stream-http"));
it("can all be imported", () => {
require("_stream_duplex");
require("_stream_passthrough");
require("_stream_readable");
require("_stream_transform");
require("_stream_writable");
require("assert");
require("buffer");
require("child_process");
require("cluster");
require("console");
require("constants");
require("crypto");
require("dgram");
require("dns");
require("domain");
require("events");
require("fs");
require("http");
require("https");
require("module");
require("net");
require("os");
require("path");
require("process");
require("punycode");
require("querystring");
require("readline");
require("repl");
require("stream");
require("string_decoder");
require("sys");
require("timers");
require("tls");
require("tty");
require("url");
require("util");
require("vm");
require("zlib");
});
});