Files
meteor/packages/modules-runtime/server.js
Ben Newman 924c748ecc Avoid bailing out with module.useNode() for ESM modules.
On the server, Meteor attempts to avoid bundling node_modules code by
replacing entry point modules with a stub that calls module.useNode() (see
packages/modules-runtime/server.js). This trick allows evaluating server
node_modules natively in Node.js, faithfully preserving all Node-specific
behaviors, such as module.id being an absolute file system path, the
__dirname and __filename variables, the ability to import binary .node
modules, and so on.

However, starting in Node.js 12.16.0 (Meteor 1.9.1+), modules evaluated
natively by Node are considered ECMAScript modules (ESM) if the closest
package.json file has "type": "module" (or has an .mjs file extension).
This poses a problem for the module.useNode() trick, because ESM modules
cannot be imported synchronously using require (which is currently how
module.useNode() works).

To work around this new error, this commit checks package.json for "type":
"module" in ImportScanner#shouldUseNode to determine whether it's safe to
use the module.useNode() trick.

The good news is that ESM modules don't have access to nearly as many
Node.js-specific quirks: no module, require, or exports variables; no
__dirname, no __filename; no ability to import JSON or other non-ESM file
types (at least right now). So it seems somewhat less important for ESM
code (compared to CommonJS code) to bail out into native Node.js execution
using module.useNode(). In other words, bundling server code should not
affect its execution in nearly as many cases, if that code is ESM rather
than legacy CommonJS.

If this good news turns out to be overly optimistic, we can consider using
a different kind of bailout stub that's capable of importing ESM using
dynamic import(). For now, making sure we avoid bailing out for ESM code
like @babel/runtime/helpers/esm/* is the priority.
2020-02-19 15:24:42 -04:00

82 lines
2.6 KiB
JavaScript

// Options that will be populated below and then passed to makeInstaller.
var makeInstallerOptions = {};
// 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.
makeInstallerOptions.fallback = function (id, parentId, 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)) {
if (id && id.startsWith('meteor/')) {
const [meteorPrefix, packageName] = id.split('/', 2);
throw new Error(
`Cannot find package "${packageName}". ` +
`Try "meteor add ${packageName}".`
);
}
if (typeof Npm === "object" &&
typeof Npm.require === "function") {
return Npm.require(id, error);
}
}
throw error;
};
makeInstallerOptions.fallback.resolve = function (id, parentId, error) {
if (topLevelIdPattern.test(id)) {
// Allow any top-level identifier to resolve to itself on the server,
// so that makeInstallerOptions.fallback has a chance to handle it.
return id;
}
throw error;
};
meteorInstall = makeInstaller(makeInstallerOptions);
var Module = meteorInstall.Module;
Module.prototype.useNode = function () {
if (typeof npmRequire !== "function") {
// Can't use Node if npmRequire is not defined.
return false;
}
var parts = this.id.split("/");
var start = 0;
if (parts[start] === "") ++start;
if (parts[start] === "node_modules" &&
parts[start + 1] === "meteor") {
start += 2;
}
if (parts.indexOf("node_modules", start) < 0) {
// Don't try to use Node for modules that aren't in node_modules
// directories.
return false;
}
try {
npmRequire.resolve(this.id);
} catch (e) {
return false;
}
// See tools/static-assets/server/npm-require.js for the implementation
// of npmRequire. Note that this strategy fails when importing ESM
// modules (typically, a .js file in a package with "type": "module" in
// its package.json), as of Node 12.16.0 (Meteor 1.9.1).
this.exports = npmRequire(this.id);
return true;
};