From fe1bf02ef27bf90949e75b19f3fc24ffce79af1d Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Sat, 28 Jun 2025 19:51:48 +0100 Subject: [PATCH] vm: expose import attributes on SourceTextModule.moduleRequests PR-URL: https://github.com/nodejs/node/pull/58829 Refs: https://github.com/nodejs/node/issues/37648 Reviewed-By: Joyee Cheung Reviewed-By: Marco Ippolito --- doc/api/vm.md | 93 +++++++++++++++++-- lib/internal/vm/module.js | 45 +++++---- src/module_wrap.cc | 10 +- test/parallel/test-vm-module-errors.js | 22 +++-- .../parallel/test-vm-module-modulerequests.js | 87 +++++++++++++++++ tools/doc/type-parser.mjs | 1 + 6 files changed, 221 insertions(+), 37 deletions(-) create mode 100644 test/parallel/test-vm-module-modulerequests.js diff --git a/doc/api/vm.md b/doc/api/vm.md index 309f24fabdce32..c3edcbbcb7b191 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -573,16 +573,6 @@ const contextifiedObject = vm.createContext({ })(); ``` -### `module.dependencySpecifiers` - -* {string\[]} - -The specifiers of all dependencies of this module. The returned array is frozen -to disallow any changes to it. - -Corresponds to the `[[RequestedModules]]` field of [Cyclic Module Record][]s in -the ECMAScript specification. - ### `module.error` * Type: {any} @@ -887,6 +877,72 @@ const cachedData = module.createCachedData(); const module2 = new vm.SourceTextModule('const a = 1;', { cachedData }); ``` +### `sourceTextModule.dependencySpecifiers` + + + +> Stability: 0 - Deprecated: Use [`sourceTextModule.moduleRequests`][] instead. + +* {string\[]} + +The specifiers of all dependencies of this module. The returned array is frozen +to disallow any changes to it. + +Corresponds to the `[[RequestedModules]]` field of [Cyclic Module Record][]s in +the ECMAScript specification. + +### `sourceTextModule.moduleRequests` + + + +* {ModuleRequest\[]} Dependencies of this module. + +The requested import dependencies of this module. The returned array is frozen +to disallow any changes to it. + +For example, given a source text: + + + +```mjs +import foo from 'foo'; +import fooAlias from 'foo'; +import bar from './bar.js'; +import withAttrs from '../with-attrs.ts' with { arbitraryAttr: 'attr-val' }; +``` + + + +The value of the `sourceTextModule.moduleRequests` will be: + +```js +[ + { + specifier: 'foo', + attributes: {}, + }, + { + specifier: 'foo', + attributes: {}, + }, + { + specifier: './bar.js', + attributes: {}, + }, + { + specifier: '../with-attrs.ts', + attributes: { arbitraryAttr: 'attr-val' }, + }, +]; +``` + ## Class: `vm.SyntheticModule` + +* {Object} + * `specifier` {string} The specifier of the requested module. + * `attributes` {Object} The `"with"` value passed to the + [WithClause][] in a [ImportDeclaration][], or an empty object if no value was + provided. + +A `ModuleRequest` represents the request to import a module with given import attributes and phase. + ## `vm.compileFunction(code[, params[, options]])`