From d149767429db720d670116bad0a6adfbe0624263 Mon Sep 17 00:00:00 2001 From: Jonathan Felchlin Date: Wed, 11 May 2016 21:06:14 -0700 Subject: [PATCH 1/7] Added handling for multiple Webpack configs If the Webpack config is an array, then use the first config that includes a resolve section. --- resolvers/webpack/index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index df6f15f03..487d72352 100644 --- a/resolvers/webpack/index.js +++ b/resolvers/webpack/index.js @@ -93,6 +93,12 @@ exports.resolve = function (source, file, settings) { webpackConfig = {} } + if (Array.isArray(webpackConfig)) { + webpackConfig = find(webpackConfig, function findFirstWithResolve(config) { + return !!config.resolve; + }); + } + // externals if (findExternal(source, webpackConfig.externals)) return { found: true, path: null } From 71ef967562c1ba8c8139a85b6f4209ba82e9239d Mon Sep 17 00:00:00 2001 From: Jonathan Felchlin Date: Wed, 11 May 2016 21:22:14 -0700 Subject: [PATCH 2/7] Adding unit test for multiple webpack configs --- resolvers/webpack/test/config.js | 10 ++++++++++ .../webpack/test/files/webpack.config.multiple.js | 15 +++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 resolvers/webpack/test/files/webpack.config.multiple.js diff --git a/resolvers/webpack/test/config.js b/resolvers/webpack/test/config.js index 01e9dee5a..0fe2df501 100644 --- a/resolvers/webpack/test/config.js +++ b/resolvers/webpack/test/config.js @@ -36,4 +36,14 @@ 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('resolve') + .and.equal(path.join(__dirname, 'files', 'src', 'main-module.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..4872b573e --- /dev/null +++ b/resolvers/webpack/test/files/webpack.config.multiple.js @@ -0,0 +1,15 @@ +var path = require('path') + +module.exports = [{ + name: 'one', +}, { + name: 'two', + resolve: { + alias: { + 'foo': path.join(__dirname, 'some', 'goofy', 'path', 'foo.js'), + }, + modulesDirectories: ['node_modules', 'bower_components'], + root: path.join(__dirname, 'src'), + fallback: path.join(__dirname, 'fallback'), + }, +}] From f010c75c1ec1d31143ad105e4183388da9edafe3 Mon Sep 17 00:00:00 2001 From: Jonathan Felchlin Date: Wed, 11 May 2016 21:39:16 -0700 Subject: [PATCH 3/7] Fixed unit test for multiple webpack configs --- resolvers/webpack/test/config.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/resolvers/webpack/test/config.js b/resolvers/webpack/test/config.js index 0fe2df501..dd5c8f9e8 100644 --- a/resolvers/webpack/test/config.js +++ b/resolvers/webpack/test/config.js @@ -37,13 +37,12 @@ describe("config", function () { .and.equal(path.join(__dirname, 'config-extensions', 'src', 'main-module.js')) }) - it("finds the first config with a resolve section", function () { + it.only("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('resolve') - .and.equal(path.join(__dirname, 'files', 'src', 'main-module.js')) + expect(resolve('main-module', file, settings)).to.have.property('path') + .and.equal(path.join(__dirname, 'files', 'src', 'main-module.js')) }) }) From 97e4e79d9930f3bd06860baa306ba67be6a195d8 Mon Sep 17 00:00:00 2001 From: Jonathan Felchlin Date: Wed, 11 May 2016 21:46:14 -0700 Subject: [PATCH 4/7] Remove only call from unit test --- resolvers/webpack/test/config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resolvers/webpack/test/config.js b/resolvers/webpack/test/config.js index dd5c8f9e8..107ec3acf 100644 --- a/resolvers/webpack/test/config.js +++ b/resolvers/webpack/test/config.js @@ -37,7 +37,7 @@ describe("config", function () { .and.equal(path.join(__dirname, 'config-extensions', 'src', 'main-module.js')) }) - it.only("finds the first config with a resolve section", function () { + it("finds the first config with a resolve section", function () { var settings = { config: path.join(__dirname, './files/webpack.config.multiple.js'), } From 69a5817f946ca353adfc7fba5b2140cbb05892cf Mon Sep 17 00:00:00 2001 From: Jonathan Felchlin Date: Fri, 13 May 2016 12:39:44 -0700 Subject: [PATCH 5/7] Added 'config-index' setting for resolving multiple configs --- resolvers/webpack/index.js | 12 +++++++++--- resolvers/webpack/test/config.js | 10 ++++++++++ .../webpack/test/files/webpack.config.multiple.js | 7 ++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/resolvers/webpack/index.js b/resolvers/webpack/index.js index 487d72352..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 @@ -94,9 +95,14 @@ exports.resolve = function (source, file, settings) { } if (Array.isArray(webpackConfig)) { - webpackConfig = find(webpackConfig, function findFirstWithResolve(config) { - return !!config.resolve; - }); + if (typeof configIndex !== 'undefined' && webpackConfig.length > configIndex) { + webpackConfig = webpackConfig[configIndex] + } + else { + webpackConfig = find(webpackConfig, function findFirstWithResolve(config) { + return !!config.resolve + }) + } } // externals diff --git a/resolvers/webpack/test/config.js b/resolvers/webpack/test/config.js index 107ec3acf..1d00bf487 100644 --- a/resolvers/webpack/test/config.js +++ b/resolvers/webpack/test/config.js @@ -45,4 +45,14 @@ describe("config", function () { 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 index 4872b573e..f2ab9185a 100644 --- a/resolvers/webpack/test/files/webpack.config.multiple.js +++ b/resolvers/webpack/test/files/webpack.config.multiple.js @@ -4,12 +4,17 @@ 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'), - fallback: path.join(__dirname, 'fallback'), }, }] From f3d42b53d8ab54fd0dac6bbabe9d80974ae0988a Mon Sep 17 00:00:00 2001 From: Jonathan Felchlin Date: Fri, 13 May 2016 12:47:42 -0700 Subject: [PATCH 6/7] Added documentation and release notes for multiple config support --- CHANGELOG.md | 4 ++++ resolvers/webpack/README.md | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb467d3df..fd1aa6ca2 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..ef04f11d5 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: @@ -25,3 +27,13 @@ settings: import/resolver: 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 +``` From bc232f8b6e12a1d353a23fda90f4bc4e116d0321 Mon Sep 17 00:00:00 2001 From: Jonathan Felchlin Date: Sat, 14 May 2016 12:15:13 -0700 Subject: [PATCH 7/7] Fixed documentation for config-index; Fixed JSON in YAML examples --- resolvers/webpack/README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/resolvers/webpack/README.md b/resolvers/webpack/README.md index ef04f11d5..f6542825e 100644 --- a/resolvers/webpack/README.md +++ b/resolvers/webpack/README.md @@ -25,7 +25,8 @@ 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: @@ -34,6 +35,7 @@ or with explicit config file name: --- settings: import/resolver: - webpack: { config: 'webpack.multiple.config.js' } - config-index: 1 + webpack: + config: 'webpack.multiple.config.js' + config-index: 1 # take the config at index 1 ```