From c15901b2deb70b414a25458ba6ae43b8e3825b82 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Tue, 17 May 2016 03:10:56 -0700 Subject: [PATCH] Adding handling for multiple Webpack configs (#335) * Added handling for multiple Webpack configs If the Webpack config is an array, then use the first config that includes a resolve section. * Adding unit test for multiple webpack configs * Fixed unit test for multiple webpack configs * Remove only call from unit test * Added 'config-index' setting for resolving multiple configs * Added documentation and release notes for multiple config support * Fixed documentation for config-index; Fixed JSON in YAML examples --- CHANGELOG.md | 4 ++++ resolvers/webpack/README.md | 16 ++++++++++++++- resolvers/webpack/index.js | 12 +++++++++++ resolvers/webpack/test/config.js | 19 ++++++++++++++++++ .../test/files/webpack.config.multiple.js | 20 +++++++++++++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 resolvers/webpack/test/files/webpack.config.multiple.js diff --git a/CHANGELOG.md b/CHANGELOG.md index bde24fd78..4e8748980 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com). +## [Unreleased] +### Added +- Added support for multiple webpack configs ([#181], thanks [@GreenGremlin]) + ## [Unreleased] ### Fixed - `export * from 'foo'` now properly ignores a `default` export from `foo`, if any. ([#328]/[#332], thanks [@jkimbo]) diff --git a/resolvers/webpack/README.md b/resolvers/webpack/README.md index c716e4289..f6542825e 100644 --- a/resolvers/webpack/README.md +++ b/resolvers/webpack/README.md @@ -11,6 +11,8 @@ Will look for `webpack.config.js` as a sibling of the first ancestral `package.j or a `config` parameter may be provided with another filename/path either relative to the `package.json`, or a complete, absolute path. +If multiple webpack configurations are found the first configuration containing a resolve section will be used. Optionally, the `config-index` (zero-based) setting can be used to select a specific configuration. + ```yaml --- settings: @@ -23,5 +25,17 @@ or with explicit config file name: --- settings: import/resolver: - webpack: { config: 'webpack.dev.config.js' } + webpack: + config: 'webpack.dev.config.js' +``` + +or with explicit config file name: + +```yaml +--- +settings: + import/resolver: + webpack: + config: 'webpack.multiple.config.js' + config-index: 1 # take the config at index 1 ``` diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index df6f15f03..8919ef388 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -40,6 +40,7 @@ exports.resolve = function (source, file, settings) { try { var configPath = get(settings, 'config') + , configIndex = get(settings, 'config-index') , packageDir , extension @@ -93,6 +94,17 @@ exports.resolve = function (source, file, settings) { webpackConfig = {} } + if (Array.isArray(webpackConfig)) { + if (typeof configIndex !== 'undefined' && webpackConfig.length > configIndex) { + webpackConfig = webpackConfig[configIndex] + } + else { + webpackConfig = find(webpackConfig, function findFirstWithResolve(config) { + return !!config.resolve + }) + } + } + // externals if (findExternal(source, webpackConfig.externals)) return { found: true, path: null } diff --git a/resolvers/webpack/test/config.js b/resolvers/webpack/test/config.js index 01e9dee5a..1d00bf487 100644 --- a/resolvers/webpack/test/config.js +++ b/resolvers/webpack/test/config.js @@ -36,4 +36,23 @@ describe("config", function () { .to.have.property('path') .and.equal(path.join(__dirname, 'config-extensions', 'src', 'main-module.js')) }) + + it("finds the first config with a resolve section", function () { + var settings = { + config: path.join(__dirname, './files/webpack.config.multiple.js'), + } + + expect(resolve('main-module', file, settings)).to.have.property('path') + .and.equal(path.join(__dirname, 'files', 'src', 'main-module.js')) + }) + + it("finds the config at option config-index", function () { + var settings = { + config: path.join(__dirname, './files/webpack.config.multiple.js'), + 'config-index': 2, + } + + expect(resolve('foo', file, settings)).to.have.property('path') + .and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'foo.js')) + }) }) diff --git a/resolvers/webpack/test/files/webpack.config.multiple.js b/resolvers/webpack/test/files/webpack.config.multiple.js new file mode 100644 index 000000000..f2ab9185a --- /dev/null +++ b/resolvers/webpack/test/files/webpack.config.multiple.js @@ -0,0 +1,20 @@ +var path = require('path') + +module.exports = [{ + name: 'one', +}, { + name: 'two', + resolve: { + root: path.join(__dirname, 'src'), + fallback: path.join(__dirname, 'fallback'), + }, +}, { + name: 'three', + resolve: { + alias: { + 'foo': path.join(__dirname, 'some', 'goofy', 'path', 'foo.js'), + }, + modulesDirectories: ['node_modules', 'bower_components'], + root: path.join(__dirname, 'src'), + }, +}]