Skip to content

Commit 0789c21

Browse files
committed
module: add module.customConditions
Add module.customConditions which exposes the custom resolution conditions specified by the user. Fixes: #55824
1 parent 8456a12 commit 0789c21

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

doc/api/module.md

+36
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,42 @@ const require = createRequire(import.meta.url);
6666
const siblingModule = require('./sibling-module');
6767
```
6868
69+
### `module.customConditions`
70+
71+
<!-- YAML
72+
added: REPLACEME
73+
-->
74+
75+
* Type: {Set<string>}
76+
77+
The custom resolution conditions specified by the user. The default Node.js conditions are not included.
78+
79+
For example, assuming the following script for `module-customconditions.js`:
80+
81+
```mjs
82+
import module from 'node:module';
83+
84+
console.log('conditions:', module.customConditions);
85+
```
86+
87+
```cjs
88+
const module = require('node:module');
89+
90+
console.log('conditions:', module.customConditions);
91+
```
92+
93+
Launching the Node.js process as:
94+
95+
```console
96+
$ node -C additional module-customconditions.js
97+
```
98+
99+
Would generate the output:
100+
101+
```text
102+
conditions: Set(1) { 'additional' }
103+
```
104+
69105
### `module.findPackageJSON(specifier[, base])`
70106
71107
<!-- YAML

lib/module.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
'use strict';
22

3+
const {
4+
ObjectDefineProperty,
5+
SafeArrayIterator,
6+
Set,
7+
} = primordials;
8+
const { getOptionValue } = require('internal/options');
39
const {
410
findSourceMap,
511
getSourceMapsSupport,
@@ -23,6 +29,16 @@ const { stripTypeScriptTypes } = require('internal/modules/typescript');
2329

2430
Module.register = register;
2531
Module.constants = constants;
32+
ObjectDefineProperty(Module, 'customConditions', {
33+
__proto__: null,
34+
get() {
35+
const value = new Set(new SafeArrayIterator(getOptionValue('--conditions')));
36+
ObjectDefineProperty(this, 'customConditions', { __proto__: null, value, configurable: false });
37+
return value;
38+
},
39+
enumerable: true,
40+
configurable: true,
41+
});
2642
Module.enableCompileCache = enableCompileCache;
2743
Module.findPackageJSON = findPackageJSON;
2844
Module.flushCompileCache = flushCompileCache;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
// Flags: -C additional
4+
5+
require('../common');
6+
const assert = require('node:assert');
7+
const { customConditions } = require('module');
8+
9+
assert.deepStrictEqual(customConditions, new Set(['additional']));

0 commit comments

Comments
 (0)