diff --git a/resolvers/webpack/CHANGELOG.md b/resolvers/webpack/CHANGELOG.md index 204e0224a..45a89bf0a 100644 --- a/resolvers/webpack/CHANGELOG.md +++ b/resolvers/webpack/CHANGELOG.md @@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel ## Unreleased +### Added +- [New] enable passing cwd as an option to `eslint-import-resolver-webpack` ([#1503], thanks [@Aghassi]) + ## 0.11.1 - 2019-04-13 ### Fixed @@ -117,6 +120,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel - `interpret` configs (such as `.babel.js`). Thanks to [@gausie] for the initial PR ([#164], ages ago! 😅) and [@jquense] for tests ([#278]). +[#1503]: https://github.com/benmosher/eslint-plugin-import/pull/1503 [#1297]: https://github.com/benmosher/eslint-plugin-import/pull/1297 [#1261]: https://github.com/benmosher/eslint-plugin-import/pull/1261 [#1220]: https://github.com/benmosher/eslint-plugin-import/pull/1220 @@ -166,3 +170,4 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel [@mattkrick]: https://github.com/mattkrick [@idudinov]: https://github.com/idudinov [@keann]: https://github.com/keann +[@Aghassi]: https://github.com/Aghassi diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index 0f75a2840..dd3fc7a38 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -20,7 +20,15 @@ exports.interfaceVersion = 2 * resolveImport('./foo', '/Users/ben/bar.js') => '/Users/ben/foo.js' * @param {string} source - the module to resolve; i.e './some-module' * @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js' - * TODO: take options as a third param, with webpack config file name + * @param {object} settings - the webpack config file name, as well as cwd + * @example + * options: { + * // Path to the webpack config + * config: 'webpack.config.js', + * // Path to be used to determine where to resolve webpack from + * // (may differ from the cwd in some cases) + * cwd: process.cwd() + * } * @return {string?} the resolved path to source, undefined if not resolved, or null * if resolved to a non-FS resource (i.e. script tag at page load) */ @@ -41,6 +49,11 @@ exports.resolve = function (source, file, settings) { var webpackConfig var configPath = get(settings, 'config') + /** + * Attempt to set the current working directory. + * If none is passed, default to the `cwd` where the config is located. + */ + , cwd = get(settings, 'cwd') , configIndex = get(settings, 'config-index') , env = get(settings, 'env') , argv = get(settings, 'argv', {}) @@ -114,7 +127,7 @@ exports.resolve = function (source, file, settings) { } // otherwise, resolve "normally" - var resolveSync = getResolveSync(configPath, webpackConfig) + var resolveSync = getResolveSync(configPath, webpackConfig, cwd) try { return { found: true, path: resolveSync(path.dirname(file), source) } @@ -130,13 +143,13 @@ exports.resolve = function (source, file, settings) { var MAX_CACHE = 10 var _cache = [] -function getResolveSync(configPath, webpackConfig) { +function getResolveSync(configPath, webpackConfig, cwd) { var cacheKey = { configPath: configPath, webpackConfig: webpackConfig } var cached = find(_cache, function (entry) { return isEqual(entry.key, cacheKey) }) if (!cached) { cached = { key: cacheKey, - value: createResolveSync(configPath, webpackConfig), + value: createResolveSync(configPath, webpackConfig, cwd), } // put in front and pop last item if (_cache.unshift(cached) > MAX_CACHE) { @@ -146,15 +159,18 @@ function getResolveSync(configPath, webpackConfig) { return cached.value } -function createResolveSync(configPath, webpackConfig) { +function createResolveSync(configPath, webpackConfig, cwd) { var webpackRequire , basedir = null if (typeof configPath === 'string') { - basedir = path.dirname(configPath) + // This can be changed via the settings passed in when defining the resolver + basedir = cwd || configPath + log(`Attempting to load webpack path from ${basedir}`) } try { + // Attempt to resolve webpack from the given `basedir` var webpackFilename = resolve.sync('webpack', { basedir, preserveSymlinks: false }) var webpackResolveOpts = { basedir: path.dirname(webpackFilename), preserveSymlinks: false } diff --git a/resolvers/webpack/test/example.js b/resolvers/webpack/test/example.js new file mode 100644 index 000000000..375f6b5a1 --- /dev/null +++ b/resolvers/webpack/test/example.js @@ -0,0 +1,9 @@ +var path = require('path') + +var resolve = require('../index').resolve + +var file = path.join(__dirname, 'files', 'src', 'dummy.js') + +var webpackDir = path.join(__dirname, "different-package-location") + +console.log(resolve('main-module', file, { config: "webpack.config.js", cwd: webpackDir})) diff --git a/resolvers/webpack/test/root.js b/resolvers/webpack/test/root.js index 4839f3b89..436572009 100644 --- a/resolvers/webpack/test/root.js +++ b/resolvers/webpack/test/root.js @@ -6,6 +6,7 @@ var resolve = require('../index').resolve var file = path.join(__dirname, 'files', 'src', 'dummy.js') +var webpackDir = path.join(__dirname, "different-package-location") describe("root", function () { it("works", function () { @@ -32,5 +33,13 @@ describe("root", function () { .property('path') .to.equal(path.join(__dirname, 'files', 'bower_components', 'typeahead.js')) }) - + it("supports passing a different directory to load webpack from", function () { + // Webpack should still be able to resolve the config here + expect(resolve('main-module', file, { config: "webpack.config.js", cwd: webpackDir})) + .property('path') + .to.equal(path.join(__dirname, 'files', 'src', 'main-module.js')) + expect(resolve('typeahead', file, { config: "webpack.config.js", cwd: webpackDir})) + .property('path') + .to.equal(path.join(__dirname, 'files', 'bower_components', 'typeahead.js')) + }) })