Skip to content

Commit

Permalink
Adding handling for multiple Webpack configs (import-js#335)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
GreenGremlin authored and benmosher committed May 17, 2016
1 parent 0a918b1 commit c15901b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
16 changes: 15 additions & 1 deletion resolvers/webpack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
```
12 changes: 12 additions & 0 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ exports.resolve = function (source, file, settings) {

try {
var configPath = get(settings, 'config')
, configIndex = get(settings, 'config-index')
, packageDir
, extension

Expand Down Expand Up @@ -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 }

Expand Down
19 changes: 19 additions & 0 deletions resolvers/webpack/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})
})
20 changes: 20 additions & 0 deletions resolvers/webpack/test/files/webpack.config.multiple.js
Original file line number Diff line number Diff line change
@@ -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'),
},
}]

0 comments on commit c15901b

Please sign in to comment.