Skip to content

Commit 9efaba7

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 9efaba7

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

doc/api/module.md

Lines changed: 36 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ 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([
72+
...getOptionValue('--conditions'),
73+
]);
74+
}
75+
return userConditions;
76+
}
77+
6678
/** @type {Set<string>} */
6779
let cjsConditions;
6880
/**
@@ -407,6 +419,7 @@ module.exports = {
407419
enableCompileCache,
408420
flushCompileCache,
409421
getBuiltinModule,
422+
getUserConditions,
410423
getCjsConditions,
411424
getCompileCacheDir,
412425
initializeCjsConditions,

lib/module.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
enableCompileCache,
1616
flushCompileCache,
1717
getCompileCacheDir,
18+
getUserConditions,
1819
} = require('internal/modules/helpers');
1920
const {
2021
findPackageJSON,
@@ -23,6 +24,12 @@ const { stripTypeScriptTypes } = require('internal/modules/typescript');
2324

2425
Module.register = register;
2526
Module.constants = constants;
27+
Object.defineProperty(Module, 'customConditions', {
28+
get() {
29+
return getUserConditions();
30+
},
31+
enumerable: true,
32+
});
2633
Module.enableCompileCache = enableCompileCache;
2734
Module.findPackageJSON = findPackageJSON;
2835
Module.flushCompileCache = flushCompileCache;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
// Flags: -C additional --expose-internals
4+
5+
require('../common');
6+
const { SafeSet } = require('internal/test/binding').primordials;
7+
const assert = require('node:assert');
8+
const { customConditions } = require('module');
9+
10+
assert.deepStrictEqual(customConditions, new SafeSet(['additional']));

0 commit comments

Comments
 (0)