Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding handling for multiple Webpack configs #335

Merged
merged 7 commits into from
May 17, 2016
Merged
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
12 changes: 12 additions & 0 deletions 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 @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually needs to be a sibling of the "config" key, I think. The settings that the resolver gets are the contents of the object keyed by "webpack"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I also fixed the JSON piece in the YAML example.

```
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'),
},
}]