Skip to content

Commit

Permalink
added support for argv parameter of webpack`s config-as-a-function (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
keann authored and benmosher committed Jan 22, 2019
1 parent fcf2ce7 commit 548ea02
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 3 additions & 2 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ exports.resolve = function (source, file, settings) {
var configPath = get(settings, 'config')
, configIndex = get(settings, 'config-index')
, env = get(settings, 'env')
, argv = get(settings, 'argv', {})
, packageDir

log('Config path from settings:', configPath)
Expand Down Expand Up @@ -87,13 +88,13 @@ exports.resolve = function (source, file, settings) {
}

if (typeof webpackConfig === 'function') {
webpackConfig = webpackConfig(env, {})
webpackConfig = webpackConfig(env, argv)
}

if (Array.isArray(webpackConfig)) {
webpackConfig = webpackConfig.map(cfg => {
if (typeof cfg === 'function') {
return cfg(env, {})
return cfg(env, argv)
}

return cfg
Expand Down
21 changes: 21 additions & 0 deletions resolvers/webpack/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,25 @@ describe("config", function () {
expect(resolve('bar', file, settings)).to.have.property('path')
.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'bar.js'))
})

it('passes argv to config when it is a function', function() {
var settings = {
config: require(path.join(__dirname, './files/webpack.function.config.js')),
argv: {
mode: 'test'
}
}

expect(resolve('baz', file, settings)).to.have.property('path')
.and.equal(path.join(__dirname, 'files', 'some', 'bar', 'bar.js'))
})

it('passes a default empty argv object to config when it is a function', function() {
var settings = {
config: require(path.join(__dirname, './files/webpack.function.config.js')),
argv: undefined
}

expect(function () { resolve('baz', file, settings) }).to.not.throw(Error)
})
})
3 changes: 2 additions & 1 deletion resolvers/webpack/test/files/webpack.function.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
var path = require('path')
var pluginsTest = require('webpack-resolver-plugin-test')

module.exports = function(env) {
module.exports = function(env, argv) {
return {
resolve: {
alias: {
'foo': path.join(__dirname, 'some', 'goofy', 'path', 'foo.js'),
'bar': env ? path.join(__dirname, 'some', 'goofy', 'path', 'bar.js') : undefined,
'baz': argv.mode === 'test' ? path.join(__dirname, 'some', 'bar', 'bar.js') : undefined,
'some-alias': path.join(__dirname, 'some'),
},
modules: [
Expand Down

0 comments on commit 548ea02

Please sign in to comment.