Skip to content

Commit e818131

Browse files
authored
Merge branch 'master' into node-resolve-refactor-builtins
2 parents fbf8807 + 23b0bf7 commit e818131

File tree

26 files changed

+403
-206
lines changed

26 files changed

+403
-206
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = {
3333
'import/no-namespace': 'off',
3434
'import/no-named-export': 'off',
3535
'no-unused-vars': 'off',
36+
'spaced-comment': 'off',
3637
'prettier/prettier': [
3738
'error',
3839
{

packages/dynamic-import-vars/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export default {
4040
plugins: [
4141
dynamicImportVars({
4242
// options
43-
})
44-
]
43+
}),
44+
],
4545
};
4646
```
4747

packages/node-resolve/README.md

+8-14
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,12 @@ Default: `false`
6262

6363
If `true`, instructs the plugin to use the `"browser"` property in `package.json` files to specify alternative files to load for bundling. This is useful when bundling for a browser environment. Alternatively, a value of `'browser'` can be added to the `mainFields` option. If `false`, any `"browser"` properties in package files will be ignored. This option takes precedence over `mainFields`.
6464

65-
### `customResolveOptions`
65+
### `moduleDirectories`
6666

67-
Type: `Object`<br>
68-
Default: `null`
69-
70-
An `Object` that specifies additional options that should be passed through to [`resolve`](https://www.npmjs.com/package/resolve).
67+
Type: `Array[...String]`<br>
68+
Default: `['node_modules']`
7169

72-
```
73-
customResolveOptions: {
74-
moduleDirectory: 'js_modules'
75-
}
76-
```
70+
One or more directories in which to recursively look for modules.
7771

7872
### `dedupe`
7973

@@ -122,10 +116,6 @@ Valid values: `['browser', 'jsnext:main', 'module', 'main']`
122116

123117
Specifies the properties to scan within a `package.json`, used to determine the bundle entry point. The order of property names is significant, as the first-found property is used as the resolved entry point. If the array contains `'browser'`, key/values specified in the `package.json` `browser` property will be used.
124118

125-
### `only`
126-
127-
DEPRECATED: use "resolveOnly" instead
128-
129119
### `preferBuiltins`
130120

131121
Type: `Boolean`<br>
@@ -161,6 +151,10 @@ Specifies the root directory from which to resolve modules. Typically used when
161151
rootDir: path.join(process.cwd(), '..')
162152
```
163153

154+
## Preserving symlinks
155+
156+
This plugin honours the rollup [`preserveSymlinks`](https://rollupjs.org/guide/en/#preservesymlinks) option.
157+
164158
## Using with @rollup/plugin-commonjs
165159

166160
Since most packages in your node_modules folder are probably legacy CommonJS rather than JavaScript modules, you may need to use [@rollup/plugin-commonjs](https://github.com/rollup/plugins/tree/master/packages/commonjs):

packages/node-resolve/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"builtin-modules": "^3.1.0",
6060
"deepmerge": "^4.2.2",
6161
"is-module": "^1.0.0",
62-
"resolve": "^1.17.0"
62+
"resolve": "^1.19.0"
6363
},
6464
"devDependencies": {
6565
"@babel/core": "^7.10.5",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export default function handleDeprecatedOptions(opts) {
2+
const warnings = [];
3+
4+
if (opts.customResolveOptions) {
5+
const { customResolveOptions } = opts;
6+
if (customResolveOptions.moduleDirectory) {
7+
// eslint-disable-next-line no-param-reassign
8+
opts.moduleDirectories = Array.isArray(customResolveOptions.moduleDirectory)
9+
? customResolveOptions.moduleDirectory
10+
: [customResolveOptions.moduleDirectory];
11+
12+
warnings.push(
13+
'node-resolve: The `customResolveOptions.moduleDirectory` option has been deprecated. Use `moduleDirectories`, which must be an array.'
14+
);
15+
}
16+
17+
if (customResolveOptions.preserveSymlinks) {
18+
throw new Error(
19+
'node-resolve: `customResolveOptions.preserveSymlinks` is no longer an option. We now always use the rollup `preserveSymlinks` option.'
20+
);
21+
}
22+
23+
[
24+
'basedir',
25+
'package',
26+
'extensions',
27+
'includeCoreModules',
28+
'readFile',
29+
'isFile',
30+
'isDirectory',
31+
'realpath',
32+
'packageFilter',
33+
'pathFilter',
34+
'paths',
35+
'packageIterator'
36+
].forEach((resolveOption) => {
37+
if (customResolveOptions[resolveOption]) {
38+
throw new Error(
39+
`node-resolve: \`customResolveOptions.${resolveOption}\` is no longer an option. If you need this, please open an issue.`
40+
);
41+
}
42+
});
43+
}
44+
45+
return { warnings };
46+
}

packages/node-resolve/src/index.js

+51-71
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import isModule from 'is-module';
88
import { isDirCached, isFileCached, readCachedFile } from './cache';
99
import { exists, readFile, realpath } from './fs';
1010
import { resolveImportSpecifiers } from './resolveImportSpecifiers';
11-
import { getMainFields, getPackageInfo, getPackageName, normalizeInput } from './util';
11+
import { getMainFields, getPackageName, normalizeInput } from './util';
12+
import handleDeprecatedOptions from './deprecated-options';
1213

1314
const builtins = new Set(builtinList);
1415
const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
15-
const nullFn = () => null;
1616
const deepFreeze = (object) => {
1717
Object.freeze(object);
1818

@@ -29,21 +29,22 @@ const baseConditions = ['default', 'module'];
2929
const baseConditionsEsm = [...baseConditions, 'import'];
3030
const baseConditionsCjs = [...baseConditions, 'require'];
3131
const defaults = {
32-
customResolveOptions: {},
3332
dedupe: [],
3433
// It's important that .mjs is listed before .js so that Rollup will interpret npm modules
3534
// which deploy both ESM .mjs and CommonJS .js files as ESM.
3635
extensions: ['.mjs', '.js', '.json', '.node'],
37-
resolveOnly: []
36+
resolveOnly: [],
37+
moduleDirectories: ['node_modules']
3838
};
3939
export const DEFAULTS = deepFreeze(deepMerge({}, defaults));
4040

4141
export function nodeResolve(opts = {}) {
42-
const options = Object.assign({}, defaults, opts);
43-
const { customResolveOptions, extensions, jail } = options;
42+
const { warnings } = handleDeprecatedOptions(opts);
43+
44+
const options = { ...defaults, ...opts };
45+
const { extensions, jail, moduleDirectories } = options;
4446
const conditionsEsm = [...baseConditionsEsm, ...(options.exportConditions || [])];
4547
const conditionsCjs = [...baseConditionsCjs, ...(options.exportConditions || [])];
46-
const warnings = [];
4748
const packageInfoCache = new Map();
4849
const idToPackageInfo = new Map();
4950
const mainFields = getMainFields(options);
@@ -54,11 +55,6 @@ export function nodeResolve(opts = {}) {
5455
let { dedupe } = options;
5556
let rollupOptions;
5657

57-
if (options.only) {
58-
warnings.push('node-resolve: The `only` options is deprecated, please use `resolveOnly`');
59-
options.resolveOnly = options.only;
60-
}
61-
6258
if (typeof dedupe !== 'function') {
6359
dedupe = (importee) =>
6460
options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee));
@@ -110,12 +106,12 @@ export function nodeResolve(opts = {}) {
110106
const importSuffix = `${params ? `?${params}` : ''}`;
111107
importee = importPath;
112108

113-
const basedir = !importer || dedupe(importee) ? rootDir : dirname(importer);
109+
const baseDir = !importer || dedupe(importee) ? rootDir : dirname(importer);
114110

115111
// https://github.com/defunctzombie/package-browser-field-spec
116112
const browser = browserMapCache.get(importer);
117113
if (useBrowserOverrides && browser) {
118-
const resolvedImportee = resolve(basedir, importee);
114+
const resolvedImportee = resolve(baseDir, importee);
119115
if (browser[importee] === false || browser[resolvedImportee] === false) {
120116
return ES6_BROWSER_EMPTY;
121117
}
@@ -138,7 +134,7 @@ export function nodeResolve(opts = {}) {
138134
id += `/${parts.shift()}`;
139135
} else if (id[0] === '.') {
140136
// an import relative to the parent dir of the importer
141-
id = resolve(basedir, importee);
137+
id = resolve(baseDir, importee);
142138
isRelativeImport = true;
143139
}
144140

@@ -153,40 +149,6 @@ export function nodeResolve(opts = {}) {
153149
return false;
154150
}
155151

156-
let hasModuleSideEffects = nullFn;
157-
let hasPackageEntry = true;
158-
let packageBrowserField = false;
159-
let packageInfo;
160-
161-
const filter = (pkg, pkgPath) => {
162-
const info = getPackageInfo({
163-
cache: packageInfoCache,
164-
extensions,
165-
pkg,
166-
pkgPath,
167-
mainFields,
168-
preserveSymlinks,
169-
useBrowserOverrides
170-
});
171-
172-
({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
173-
174-
return info.cachedPkg;
175-
};
176-
177-
let resolveOptions = {
178-
basedir,
179-
packageFilter: filter,
180-
readFile: readCachedFile,
181-
isFile: isFileCached,
182-
isDirectory: isDirCached,
183-
extensions
184-
};
185-
186-
if (preserveSymlinks !== undefined) {
187-
resolveOptions.preserveSymlinks = preserveSymlinks;
188-
}
189-
190152
const importSpecifierList = [];
191153

192154
if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
@@ -221,68 +183,86 @@ export function nodeResolve(opts = {}) {
221183
}
222184

223185
importSpecifierList.push(importee);
224-
resolveOptions = Object.assign(resolveOptions, customResolveOptions);
225186

226187
const warn = (...args) => this.warn(...args);
227188
const isRequire =
228189
opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
229190
const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
230-
let resolved = await resolveImportSpecifiers(
191+
192+
const resolvedWithoutBuiltins = await resolveImportSpecifiers({
231193
importSpecifierList,
232-
resolveOptions,
233194
exportConditions,
234-
warn
235-
);
195+
warn,
196+
packageInfoCache,
197+
extensions,
198+
mainFields,
199+
preserveSymlinks,
200+
useBrowserOverrides,
201+
baseDir,
202+
moduleDirectories
203+
});
204+
205+
const resolved =
206+
importeeIsBuiltin && preferBuiltins
207+
? {
208+
packageInfo: undefined,
209+
hasModuleSideEffects: () => null,
210+
hasPackageEntry: true,
211+
packageBrowserField: false
212+
}
213+
: resolvedWithoutBuiltins;
236214
if (!resolved) {
237215
return null;
238216
}
239217

218+
const { packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = resolved;
219+
let { location } = resolved;
240220
if (packageBrowserField) {
241-
if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) {
242-
if (!packageBrowserField[resolved]) {
243-
browserMapCache.set(resolved, packageBrowserField);
221+
if (Object.prototype.hasOwnProperty.call(packageBrowserField, location)) {
222+
if (!packageBrowserField[location]) {
223+
browserMapCache.set(location, packageBrowserField);
244224
return ES6_BROWSER_EMPTY;
245225
}
246-
resolved = packageBrowserField[resolved];
226+
location = packageBrowserField[location];
247227
}
248-
browserMapCache.set(resolved, packageBrowserField);
228+
browserMapCache.set(location, packageBrowserField);
249229
}
250230

251231
if (hasPackageEntry && !preserveSymlinks) {
252-
const fileExists = await exists(resolved);
232+
const fileExists = await exists(location);
253233
if (fileExists) {
254-
resolved = await realpath(resolved);
234+
location = await realpath(location);
255235
}
256236
}
257237

258-
idToPackageInfo.set(resolved, packageInfo);
238+
idToPackageInfo.set(location, packageInfo);
259239

260240
if (hasPackageEntry) {
261241
if (importeeIsBuiltin && preferBuiltins) {
262-
if (!isPreferBuiltinsSet && resolved !== importee) {
242+
if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
263243
this.warn(
264-
`preferring built-in module '${importee}' over local alternative at '${resolved}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`
244+
`preferring built-in module '${importee}' over local alternative at '${resolvedWithoutBuiltins.location}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`
265245
);
266246
}
267247
return false;
268-
} else if (jail && resolved.indexOf(normalize(jail.trim(sep))) !== 0) {
248+
} else if (jail && location.indexOf(normalize(jail.trim(sep))) !== 0) {
269249
return null;
270250
}
271251
}
272252

273-
if (options.modulesOnly && (await exists(resolved))) {
274-
const code = await readFile(resolved, 'utf-8');
253+
if (options.modulesOnly && (await exists(location))) {
254+
const code = await readFile(location, 'utf-8');
275255
if (isModule(code)) {
276256
return {
277-
id: `${resolved}${importSuffix}`,
278-
moduleSideEffects: hasModuleSideEffects(resolved)
257+
id: `${location}${importSuffix}`,
258+
moduleSideEffects: hasModuleSideEffects(location)
279259
};
280260
}
281261
return null;
282262
}
283263
const result = {
284-
id: `${resolved}${importSuffix}`,
285-
moduleSideEffects: hasModuleSideEffects(resolved)
264+
id: `${location}${importSuffix}`,
265+
moduleSideEffects: hasModuleSideEffects(location)
286266
};
287267
return result;
288268
},

0 commit comments

Comments
 (0)