Skip to content

Commit ed26038

Browse files
committed
[New] add moduleSystem option to select import or require conditions for exports resolution
Previously, `getCategoryInfo` was called with a hardcoded `'require'` module system. This adds an `opts.moduleSystem` option (`'import'` or `'require'`, defaulting to `'require'`) that is passed through instead, allowing callers to resolve using import conditions (e.g. `['import', 'node', 'default']`) rather than require conditions (e.g. `['node', 'require', 'default']`).
1 parent 586e099 commit ed26038

5 files changed

Lines changed: 1088 additions & 6 deletions

File tree

lib/async.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ module.exports = function resolve(x, options, callback) {
165165
}
166166
var packageIterator = opts.packageIterator;
167167

168+
if (typeof opts.moduleSystem !== 'undefined' && opts.moduleSystem !== 'require' && opts.moduleSystem !== 'import') {
169+
var msErr = new $TypeError('`moduleSystem` must be `\'require\'` or `\'import\'`.');
170+
return process.nextTick(function () {
171+
cb(msErr);
172+
});
173+
}
174+
168175
var extensions = opts.extensions || ['.js'];
169176
var includeCoreModules = opts.includeCoreModules !== false;
170177
var basedir = opts.basedir || path.dirname(caller());
@@ -237,7 +244,7 @@ module.exports = function resolve(x, options, callback) {
237244

238245
// If package has exports field, resolve via exports
239246
if (typeof pkg.exports !== 'undefined') {
240-
var categoryInfo = getCategoryInfo(exportsCategory, 'require');
247+
var categoryInfo = getCategoryInfo(exportsCategory, opts.moduleSystem || 'require');
241248
var conditions = opts.conditions || categoryInfo.conditions;
242249
var resolved;
243250
try {
@@ -542,7 +549,7 @@ module.exports = function resolve(x, options, callback) {
542549

543550
function loadNodeModulesWithExports(x, start, done) {
544551
var parsed = parsePackageSpecifier(x);
545-
var categoryInfo = getCategoryInfo(exportsCategory, 'require');
552+
var categoryInfo = getCategoryInfo(exportsCategory, opts.moduleSystem || 'require');
546553
var conditions = opts.conditions || categoryInfo.conditions;
547554

548555
var thunk = function () { return getPackageCandidates(parsed.name, start, opts); };

lib/sync.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ module.exports = function resolveSync(x, options) {
145145
}
146146
var packageIterator = opts.packageIterator;
147147

148+
if (typeof opts.moduleSystem !== 'undefined' && opts.moduleSystem !== 'require' && opts.moduleSystem !== 'import') {
149+
throw new $TypeError('`moduleSystem` must be `\'require\'` or `\'import\'`.');
150+
}
151+
148152
var extensions = opts.extensions || ['.js'];
149153
var includeCoreModules = opts.includeCoreModules !== false;
150154
var basedir = opts.basedir || path.dirname(caller());
@@ -215,7 +219,7 @@ module.exports = function resolveSync(x, options) {
215219

216220
// If package has exports field, resolve via exports
217221
if (typeof pkg.exports !== 'undefined') {
218-
var categoryInfo = getCategoryInfo(exportsCategory, 'require');
222+
var categoryInfo = getCategoryInfo(exportsCategory, opts.moduleSystem || 'require');
219223
var conditions = opts.conditions || categoryInfo.conditions;
220224
var resolved = resolveExports(pkg.exports, parsed.subpath, conditions, categoryInfo.flags);
221225
if (resolved) {
@@ -346,7 +350,7 @@ module.exports = function resolveSync(x, options) {
346350

347351
function loadNodeModulesWithExportsSync(x, start) {
348352
var parsed = parsePackageSpecifier(x);
349-
var categoryInfo = getCategoryInfo(exportsCategory, 'require');
353+
var categoryInfo = getCategoryInfo(exportsCategory, opts.moduleSystem || 'require');
350354
var conditions = opts.conditions || categoryInfo.conditions;
351355

352356
// Get candidate directories for the package name

readme.markdown

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ This also enables **self-reference** support, allowing a package to import itsel
126126

127127
**Note:** `exportsCategory` and `engines` are mutually exclusive - only one can be specified.
128128

129+
* opts.moduleSystem - either `'require'` or `'import'`. Determines which module system conditions are used when resolving the `exports` field. When `'require'` (the default), the conditions include `require`; when `'import'`, the conditions include `import` instead. This option only has effect when `exportsCategory` or `engines` is also set. For example, given a package with `exports: { ".": { "import": "./esm.js", "require": "./cjs.js" } }`, resolving with `moduleSystem: 'import'` returns `esm.js`, while `moduleSystem: 'require'` (or omitting it) returns `cjs.js`.
130+
129131
* opts.conditions - an array of condition strings (e.g. `['require', 'node']`) to use when resolving the `exports` field.
130-
If specified, this overrides the conditions that would otherwise be derived from the category.
132+
If specified, this overrides the conditions that would otherwise be derived from the category (including those from `moduleSystem`).
131133
This option only has effect when `exportsCategory` or `engines` is also set.
132134

133135
default `opts` values:
@@ -245,7 +247,9 @@ This is the way Node resolves dependencies when executed with the [--preserve-sy
245247

246248
**Note:** `exportsCategory`, `enginesRange`, and `engines` are mutually exclusive - only one can be specified.
247249

248-
* opts.conditions - an array of condition strings (e.g. `['require', 'node']`) to use when resolving the `exports` field. If specified, this overrides the conditions that would otherwise be derived from the category. This option only has effect when one of `exportsCategory`, `enginesRange`, or `engines` is also set.
250+
* opts.moduleSystem - either `'require'` or `'import'`. Determines which module system conditions are used when resolving the `exports` field. When `'require'` (the default), the conditions include `require`; when `'import'`, the conditions include `import` instead. This option only has effect when `exportsCategory` or `engines` is also set. For example, given a package with `exports: { ".": { "import": "./esm.js", "require": "./cjs.js" } }`, resolving with `moduleSystem: 'import'` returns `esm.js`, while `moduleSystem: 'require'` (or omitting it) returns `cjs.js`.
251+
252+
* opts.conditions - an array of condition strings (e.g. `['require', 'node']`) to use when resolving the `exports` field. If specified, this overrides the conditions that would otherwise be derived from the category (including those from `moduleSystem`). This option only has effect when one of `exportsCategory`, `enginesRange`, or `engines` is also set.
249253

250254
default `opts` values:
251255

0 commit comments

Comments
 (0)