Skip to content

Commit cae9e94

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

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-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/internal/modules/helpers.js

+11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ function toRealPath(requestPath) {
6363
});
6464
}
6565

66+
/** @type {Set<string>} */
67+
let userConditions;
68+
69+
function getUserConditions() {
70+
if (userConditions === undefined) {
71+
userConditions = new SafeSet(getOptionValue('--conditions'));
72+
}
73+
return userConditions;
74+
}
75+
6676
/** @type {Set<string>} */
6777
let cjsConditions;
6878
/**
@@ -407,6 +417,7 @@ module.exports = {
407417
enableCompileCache,
408418
flushCompileCache,
409419
getBuiltinModule,
420+
getUserConditions,
410421
getCjsConditions,
411422
getCompileCacheDir,
412423
initializeCjsConditions,

lib/module.js

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

3+
const {
4+
ObjectDefineProperty,
5+
} = primordials;
36
const {
47
findSourceMap,
58
getSourceMapsSupport,
@@ -15,6 +18,7 @@ const {
1518
enableCompileCache,
1619
flushCompileCache,
1720
getCompileCacheDir,
21+
getUserConditions,
1822
} = require('internal/modules/helpers');
1923
const {
2024
findPackageJSON,
@@ -23,6 +27,16 @@ const { stripTypeScriptTypes } = require('internal/modules/typescript');
2327

2428
Module.register = register;
2529
Module.constants = constants;
30+
ObjectDefineProperty(Module, 'customConditions', {
31+
__proto__: null,
32+
get() {
33+
const value = new Set(getUserConditions());
34+
ObjectDefineProperty(this, 'customConditions', { __proto__: null, value, configurable: false });
35+
return value;
36+
},
37+
enumerable: true,
38+
configurable: true,
39+
});
2640
Module.enableCompileCache = enableCompileCache;
2741
Module.findPackageJSON = findPackageJSON;
2842
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)