Skip to content

Commit c15901b

Browse files
GreenGremlinbenmosher
authored andcommitted
Adding handling for multiple Webpack configs (import-js#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
1 parent 0a918b1 commit c15901b

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).
55

6+
## [Unreleased]
7+
### Added
8+
- Added support for multiple webpack configs ([#181], thanks [@GreenGremlin])
9+
610
## [Unreleased]
711
### Fixed
812
- `export * from 'foo'` now properly ignores a `default` export from `foo`, if any. ([#328]/[#332], thanks [@jkimbo])

resolvers/webpack/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Will look for `webpack.config.js` as a sibling of the first ancestral `package.j
1111
or a `config` parameter may be provided with another filename/path either relative to the
1212
`package.json`, or a complete, absolute path.
1313

14+
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.
15+
1416
```yaml
1517
---
1618
settings:
@@ -23,5 +25,17 @@ or with explicit config file name:
2325
---
2426
settings:
2527
import/resolver:
26-
webpack: { config: 'webpack.dev.config.js' }
28+
webpack:
29+
config: 'webpack.dev.config.js'
30+
```
31+
32+
or with explicit config file name:
33+
34+
```yaml
35+
---
36+
settings:
37+
import/resolver:
38+
webpack:
39+
config: 'webpack.multiple.config.js'
40+
config-index: 1 # take the config at index 1
2741
```

resolvers/webpack/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ exports.resolve = function (source, file, settings) {
4040

4141
try {
4242
var configPath = get(settings, 'config')
43+
, configIndex = get(settings, 'config-index')
4344
, packageDir
4445
, extension
4546

@@ -93,6 +94,17 @@ exports.resolve = function (source, file, settings) {
9394
webpackConfig = {}
9495
}
9596

97+
if (Array.isArray(webpackConfig)) {
98+
if (typeof configIndex !== 'undefined' && webpackConfig.length > configIndex) {
99+
webpackConfig = webpackConfig[configIndex]
100+
}
101+
else {
102+
webpackConfig = find(webpackConfig, function findFirstWithResolve(config) {
103+
return !!config.resolve
104+
})
105+
}
106+
}
107+
96108
// externals
97109
if (findExternal(source, webpackConfig.externals)) return { found: true, path: null }
98110

resolvers/webpack/test/config.js

+19
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,23 @@ describe("config", function () {
3636
.to.have.property('path')
3737
.and.equal(path.join(__dirname, 'config-extensions', 'src', 'main-module.js'))
3838
})
39+
40+
it("finds the first config with a resolve section", function () {
41+
var settings = {
42+
config: path.join(__dirname, './files/webpack.config.multiple.js'),
43+
}
44+
45+
expect(resolve('main-module', file, settings)).to.have.property('path')
46+
.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'))
47+
})
48+
49+
it("finds the config at option config-index", function () {
50+
var settings = {
51+
config: path.join(__dirname, './files/webpack.config.multiple.js'),
52+
'config-index': 2,
53+
}
54+
55+
expect(resolve('foo', file, settings)).to.have.property('path')
56+
.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'foo.js'))
57+
})
3958
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var path = require('path')
2+
3+
module.exports = [{
4+
name: 'one',
5+
}, {
6+
name: 'two',
7+
resolve: {
8+
root: path.join(__dirname, 'src'),
9+
fallback: path.join(__dirname, 'fallback'),
10+
},
11+
}, {
12+
name: 'three',
13+
resolve: {
14+
alias: {
15+
'foo': path.join(__dirname, 'some', 'goofy', 'path', 'foo.js'),
16+
},
17+
modulesDirectories: ['node_modules', 'bower_components'],
18+
root: path.join(__dirname, 'src'),
19+
},
20+
}]

0 commit comments

Comments
 (0)