diff --git a/src/common/load-plugins.js b/src/common/load-plugins.js index caf6d44a2ba4..7cc20403c440 100644 --- a/src/common/load-plugins.js +++ b/src/common/load-plugins.js @@ -5,10 +5,10 @@ const partition = require("lodash/partition"); const fs = require("fs"); const globby = require("globby"); const path = require("path"); -const resolve = require("resolve"); const thirdParty = require("./third-party"); const internalPlugins = require("./internal-plugins"); const mem = require("mem"); +const resolve = require("./resolve"); const memoizedLoad = mem(load, { cacheKey: JSON.stringify }); const memoizedSearch = mem(findPluginsInNodeModules); @@ -43,10 +43,10 @@ function load(plugins, pluginSearchDirs) { let requirePath; try { // try local files - requirePath = resolve.sync(path.resolve(process.cwd(), pluginName)); + requirePath = resolve(path.resolve(process.cwd(), pluginName)); } catch (_) { // try node modules - requirePath = resolve.sync(pluginName, { basedir: process.cwd() }); + requirePath = resolve(pluginName, { paths: [process.cwd()] }); } return { @@ -82,9 +82,7 @@ function load(plugins, pluginSearchDirs) { return memoizedSearch(nodeModulesDir).map((pluginName) => ({ name: pluginName, - requirePath: resolve.sync(pluginName, { - basedir: resolvedPluginSearchDir, - }), + requirePath: resolve(pluginName, { paths: [resolvedPluginSearchDir] }), })); }) .reduce((a, b) => a.concat(b), []); diff --git a/src/common/resolve.js b/src/common/resolve.js new file mode 100644 index 000000000000..6d96754c90fa --- /dev/null +++ b/src/common/resolve.js @@ -0,0 +1,19 @@ +"use strict"; + +const resolve = require("resolve"); + +const { resolve: nativeResolve } = eval("require"); + +// In the VS Code extension `require` is overridden and `require.resolve` doesn't support the 2nd argument. +if (nativeResolve.length === 1) { + module.exports = (id, options) => { + let baseDir; + if (options && options.paths && options.paths.length === 1) { + baseDir = options.paths[0]; + } + + return resolve.sync(id, { baseDir }); + }; +} + +module.exports = nativeResolve; diff --git a/src/config/resolve-config.js b/src/config/resolve-config.js index f56c43de0800..c32067876d34 100644 --- a/src/config/resolve-config.js +++ b/src/config/resolve-config.js @@ -2,12 +2,12 @@ const thirdParty = require("../common/third-party"); const minimatch = require("minimatch"); -const resolve = require("resolve"); const path = require("path"); const mem = require("mem"); const resolveEditorConfig = require("./resolve-config-editorconfig"); const loadToml = require("../utils/load-toml"); +const resolve = require("../common/resolve"); const getExplorerMemoized = mem( (opts) => { @@ -17,9 +17,8 @@ const getExplorerMemoized = mem( transform: (result) => { if (result && result.config) { if (typeof result.config === "string") { - const modulePath = resolve.sync(result.config, { - basedir: path.dirname(result.filepath), - }); + const dir = path.dirname(result.filepath); + const modulePath = resolve(result.config, { paths: [dir] }); result.config = eval("require")(modulePath); }