/
resolver.js
36 lines (31 loc) · 1.19 KB
/
resolver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const {resolve: resolveExports} = require('resolve.exports');
module.exports = (path, options) => {
return options.defaultResolver(path, {
...options,
pathFilter: options.conditions
? createPathFilter(options.conditions)
: undefined,
});
};
function createPathFilter(conditions) {
return function pathFilter(pkg, _path, relativePath) {
// this `index` thing can backfire, but `resolve` adds it: https://github.com/browserify/resolve/blob/f1b51848ecb7f56f77bfb823511d032489a13eab/lib/sync.js#L192
const path = relativePath === 'index' ? '.' : relativePath;
return (
resolveExports(pkg, path, {
// `resolve.exports adds `node` unless `browser` is `false`, so let's add this ugly thing
browser: conditions.includes('browser'),
conditions,
// `resolve.exports adds `import` unless `require` is `false`, so let's add this ugly thing
require: conditions.includes('require'),
}) || relativePath
);
};
}