Skip to content

Commit

Permalink
don't swallow config errors (fixes #435)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Jul 17, 2016
1 parent 34170af commit b1c8ed5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
6 changes: 6 additions & 0 deletions resolvers/webpack/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this resolver 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
### Changed
- don't swallow errors, assume config exists ([#435], thanks [@Kovensky])

## 0.4.0 - 2016-07-17
### Added
- support for `webpack.ResolverPlugin` ([#377], thanks [@Rogeres])
Expand Down Expand Up @@ -54,6 +58,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
[#181]: https://github.com/benmosher/eslint-plugin-import/pull/181
[#164]: https://github.com/benmosher/eslint-plugin-import/pull/164

[#435]: https://github.com/benmosher/eslint-plugin-import/issues/435
[#411]: https://github.com/benmosher/eslint-plugin-import/issues/411
[#357]: https://github.com/benmosher/eslint-plugin-import/issues/357
[#286]: https://github.com/benmosher/eslint-plugin-import/issues/286
Expand All @@ -67,3 +72,4 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
[@kesne]: https://github.com/kesne
[@Satyam]: https://github.com/Satyam
[@Rogeres]: https://github.com/Rogeres
[@Kovensky]: https://github.com/Kovensky
37 changes: 16 additions & 21 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,27 @@ exports.resolve = function (source, file, settings) {

var webpackConfig

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

if (configPath) log('Config path from settings:', configPath)
log('Config path from settings:', configPath)

// see if we've got an absolute path
if (!configPath || !isAbsolute(configPath)) {
// if not, find ancestral package.json and use its directory as base for the path
packageDir = findRoot(path.resolve(file))
if (!packageDir) throw new Error('package not found above ' + file)
}
// see if we've got an absolute path
if (!configPath || !isAbsolute(configPath)) {
// if not, find ancestral package.json and use its directory as base for the path
packageDir = findRoot(path.resolve(file))
if (!packageDir) throw new Error('package not found above ' + file)
}

configPath = findConfigPath(configPath, packageDir)
configPath = findConfigPath(configPath, packageDir)

log('Config path resolved to:', configPath)
webpackConfig = require(configPath)
log('Config path resolved to:', configPath)
webpackConfig = require(configPath)

if (webpackConfig && webpackConfig.default) {
log('Using ES6 module "default" key instead of module.exports.')
webpackConfig = webpackConfig.default
}
} catch (err) {
log('Error during config lookup:', err)
webpackConfig = {}
if (webpackConfig && webpackConfig.default) {
log('Using ES6 module "default" key instead of module.exports.')
webpackConfig = webpackConfig.default
}

if (Array.isArray(webpackConfig)) {
Expand Down
8 changes: 8 additions & 0 deletions resolvers/webpack/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe("config", function () {
expect(resolve('main-module', file)).to.have.property('path')
.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'))
})

it("finds absolute webpack.config.js files", function () {
expect(resolve('foo', file, absoluteSettings)).to.have.property('path')
.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'))
Expand Down Expand Up @@ -55,4 +56,11 @@ describe("config", function () {
expect(resolve('foo', file, settings)).to.have.property('path')
.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'foo.js'))
})

it("doesn't swallow config load errors (#435)", function () {
var settings = {
config: path.join(__dirname, './files/webpack.config.garbage.js'),
}
expect(function () { resolve('foo', file, settings) }).to.throw(Error)
})
})
1 change: 1 addition & 0 deletions resolvers/webpack/test/files/webpack.config.garbage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error("this config is garbage")

0 comments on commit b1c8ed5

Please sign in to comment.