Skip to content

Commit

Permalink
Use resolve as fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 17, 2020
1 parent c00689d commit 157e90e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/common/load-plugins.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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), []);
Expand Down
19 changes: 19 additions & 0 deletions 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 });
};
} else {
module.exports = nativeResolve;
}
7 changes: 3 additions & 4 deletions src/config/resolve-config.js
Expand Up @@ -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) => {
Expand All @@ -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);
}

Expand Down

0 comments on commit 157e90e

Please sign in to comment.