mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00

This PR updates the current `--experimental-modules` implementation based on the work of the modules team and reflects Phase 2 of our new modules plan. The largest differences from the current implementation include * `packge.type` which can be either `module` or `commonjs` - `type: "commonjs"`: - `.js` is parsed as commonjs - default for entry point without an extension is commonjs - `type: "module"`: - `.js` is parsed as esm - does not support loading JSON or Native Module by default - default for entry point without an extension is esm * `--entry-type=[mode]` - allows you set the type on entry point. * A new file extension `.cjs`. - this is specifically to support importing commonjs in the `module` mode. - this is only in the esm loader, the commonjs loader remains untouched, but the extension will work in the old loader if you use the full file path. * `--es-module-specifier-resolution=[type]` - options are `explicit` (default) and `node` - by default our loader will not allow for optional extensions in the import, the path for a module must include the extension if there is one - by default our loader will not allow for importing directories that have an index file - developers can use `--es-module-specifier-resolution=node` to enable the commonjs specifier resolution algorithm - This is not a “feature” but rather an implementation for experimentation. It is expected to change before the flag is removed * `--experimental-json-loader` - the only way to import json when `"type": "module"` - when enable all `import 'thing.json'` will go through the experimental loader independent of mode - based on https://github.com/whatwg/html/issues/4315 * You can use `package.main` to set an entry point for a module - the file extensions used in main will be resolved based on the `type` of the module Refs: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md Refs: https://github.com/GeoffreyBooth/node-import-file-specifier-resolution-proposal Refs: https://github.com/nodejs/modules/pull/180 Refs: https://github.com/nodejs/ecmascript-modules/pull/6 Refs: https://github.com/nodejs/ecmascript-modules/pull/12 Refs: https://github.com/nodejs/ecmascript-modules/pull/28 Refs: https://github.com/nodejs/modules/issues/255 Refs: https://github.com/whatwg/html/issues/4315 Refs: https://github.com/w3c/webcomponents/issues/770 Co-authored-by: Myles Borins <MylesBorins@google.com> Co-authored-by: John-David Dalton <john.david.dalton@gmail.com> Co-authored-by: Evan Plaice <evanplaice@gmail.com> Co-authored-by: Geoffrey Booth <webmaster@geoffreybooth.com> Co-authored-by: Michaël Zasso <targos@protonmail.com> PR-URL: https://github.com/nodejs/node/pull/26745 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Ben Coe <bencoe@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
110 lines
3.5 KiB
JavaScript
110 lines
3.5 KiB
JavaScript
'use strict';
|
|
|
|
const { ModuleWrap } = internalBinding('module_wrap');
|
|
const {
|
|
SafeSet,
|
|
SafePromise
|
|
} = primordials;
|
|
|
|
const { decorateErrorStack } = require('internal/util');
|
|
const assert = require('internal/assert');
|
|
const resolvedPromise = SafePromise.resolve();
|
|
|
|
function noop() {}
|
|
|
|
/* A ModuleJob tracks the loading of a single Module, and the ModuleJobs of
|
|
* its dependencies, over time. */
|
|
class ModuleJob {
|
|
// `loader` is the Loader instance used for loading dependencies.
|
|
// `moduleProvider` is a function
|
|
constructor(loader, url, moduleProvider, isMain) {
|
|
this.loader = loader;
|
|
this.isMain = isMain;
|
|
|
|
// This is a Promise<{ module, reflect }>, whose fields will be copied
|
|
// onto `this` by `link()` below once it has been resolved.
|
|
this.modulePromise = moduleProvider.call(loader, url, isMain);
|
|
this.module = undefined;
|
|
this.reflect = undefined;
|
|
|
|
// Wait for the ModuleWrap instance being linked with all dependencies.
|
|
const link = async () => {
|
|
({ module: this.module,
|
|
reflect: this.reflect } = await this.modulePromise);
|
|
assert(this.module instanceof ModuleWrap);
|
|
|
|
const dependencyJobs = [];
|
|
const promises = this.module.link(async (specifier) => {
|
|
const jobPromise = this.loader.getModuleJob(specifier, url);
|
|
dependencyJobs.push(jobPromise);
|
|
return (await (await jobPromise).modulePromise).module;
|
|
});
|
|
|
|
if (promises !== undefined)
|
|
await SafePromise.all(promises);
|
|
|
|
return SafePromise.all(dependencyJobs);
|
|
};
|
|
// Promise for the list of all dependencyJobs.
|
|
this.linked = link();
|
|
// This promise is awaited later anyway, so silence
|
|
// 'unhandled rejection' warnings.
|
|
this.linked.catch(noop);
|
|
|
|
// instantiated == deep dependency jobs wrappers instantiated,
|
|
// module wrapper instantiated
|
|
this.instantiated = undefined;
|
|
}
|
|
|
|
async instantiate() {
|
|
if (!this.instantiated) {
|
|
return this.instantiated = this._instantiate();
|
|
}
|
|
await this.instantiated;
|
|
return this.module;
|
|
}
|
|
|
|
// This method instantiates the module associated with this job and its
|
|
// entire dependency graph, i.e. creates all the module namespaces and the
|
|
// exported/imported variables.
|
|
async _instantiate() {
|
|
const jobsInGraph = new SafeSet();
|
|
|
|
const addJobsToDependencyGraph = async (moduleJob) => {
|
|
if (jobsInGraph.has(moduleJob)) {
|
|
return;
|
|
}
|
|
jobsInGraph.add(moduleJob);
|
|
const dependencyJobs = await moduleJob.linked;
|
|
return Promise.all(dependencyJobs.map(addJobsToDependencyGraph));
|
|
};
|
|
await addJobsToDependencyGraph(this);
|
|
try {
|
|
if (this.isMain && process._breakFirstLine) {
|
|
delete process._breakFirstLine;
|
|
const initWrapper = internalBinding('inspector').callAndPauseOnStart;
|
|
initWrapper(this.module.instantiate, this.module);
|
|
} else {
|
|
this.module.instantiate();
|
|
}
|
|
} catch (e) {
|
|
decorateErrorStack(e);
|
|
throw e;
|
|
}
|
|
for (const dependencyJob of jobsInGraph) {
|
|
// Calling `this.module.instantiate()` instantiates not only the
|
|
// ModuleWrap in this module, but all modules in the graph.
|
|
dependencyJob.instantiated = resolvedPromise;
|
|
}
|
|
return this.module;
|
|
}
|
|
|
|
async run() {
|
|
const module = await this.instantiate();
|
|
const timeout = -1;
|
|
const breakOnSigint = false;
|
|
return { module, result: module.evaluate(timeout, breakOnSigint) };
|
|
}
|
|
}
|
|
Object.setPrototypeOf(ModuleJob.prototype, null);
|
|
module.exports = ModuleJob;
|