From f84918d26967a8b5890a36d53f51bba8b788f867 Mon Sep 17 00:00:00 2001 From: Andrew Powell Date: Tue, 29 May 2018 15:35:29 +0300 Subject: [PATCH] fix: argv not passed to function config in all scenarios (#12) --- README.md | 4 +-- lib/resolve.js | 5 ++++ package-lock.json | 3 +- package.json | 4 +-- test/__snapshots__/test.js.snap | 14 +++++++++ .../types/function-argv/webpack.config.js | 6 ++++ .../types/function-promise/webpack.config.js | 2 +- .../fixtures/types/function/webpack.config.js | 2 +- test/tests/resolve.js | 30 +++++++++++++++++++ 9 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/types/function-argv/webpack.config.js diff --git a/README.md b/README.md index 09290e1..a0603e2 100644 --- a/README.md +++ b/README.md @@ -133,10 +133,10 @@ for a configuration file. ### `require` -Type: `String` +Type: `String | Array[String]` Default: `undefined` -Specifies a compiler to use when loading modules from files containing the +Specifies compiler(s) to use when loading modules from files containing the configuration. For example: ```js diff --git a/lib/resolve.js b/lib/resolve.js index 978a893..2ed5cf1 100644 --- a/lib/resolve.js +++ b/lib/resolve.js @@ -1,3 +1,5 @@ +const minimist = require('minimist'); + const fromFunction = (config, argv) => { const result = config(argv); @@ -16,6 +18,9 @@ module.exports = (configSet, argv) => { const type = typeof (config.default || config); const handler = handlers[type]; + // eslint-disable-next-line no-param-reassign + argv = argv || minimist(process.argv.slice(2)); + return handler(config.default || config, argv).then((configResult) => { return { config: configResult, diff --git a/package-lock.json b/package-lock.json index 96271a1..c70b91a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7447,8 +7447,7 @@ "minimist": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", - "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", - "dev": true + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" }, "minimist-options": { "version": "3.0.2", diff --git a/package.json b/package.json index 968755f..4cf1499 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "release:ci": "conventional-github-releaser -p angular", "release:validate": "commitlint --from=$(git describe --tags --abbrev=0) --to=$(git rev-parse HEAD)", "security": "nsp check", - "test": "mocha test/test.js --exit", + "test": "mocha test/test.js --exit --mode=superman", "test:coverage": "mkdir -p coverage && nyc --silent npm test && npm run test:coverage:report", "test:coverage:report": "nyc report --reporter=lcov --reporter=text-lcov --reporter=json --reporter=clover > coverage/lcov.info", "ci:lint": "npm run lint && npm run security", @@ -45,6 +45,7 @@ "cosmiconfig": "^5.0.2", "loud-rejection": "^1.6.0", "merge-options": "^1.0.1", + "minimist": "^1.2.0", "resolve": "^1.6.0", "webpack-log": "^1.1.2" }, @@ -74,7 +75,6 @@ "jest-snapshot": "^22.4.3", "lint-staged": "^7.0.3", "memory-fs": "^0.4.1", - "minimist": "^1.2.0", "mocha": "^5.0.5", "nsp": "^3.2.1", "nyc": "^11.6.0", diff --git a/test/__snapshots__/test.js.snap b/test/__snapshots__/test.js.snap index 33cfe82..e44ac24 100644 --- a/test/__snapshots__/test.js.snap +++ b/test/__snapshots__/test.js.snap @@ -105,6 +105,20 @@ Object { } `; +exports[`Resolve > should resolve a function config and parse argv #0 1`] = ` +Object { + "entry": "function", + "mode": "superman", +} +`; + +exports[`Resolve > should resolve a function config and pass argv #0 1`] = ` +Object { + "entry": "function", + "mode": "batman", +} +`; + exports[`Resolve > should resolve type: array #0 1`] = ` Array [ Object { diff --git a/test/fixtures/types/function-argv/webpack.config.js b/test/fixtures/types/function-argv/webpack.config.js new file mode 100644 index 0000000..a5f9712 --- /dev/null +++ b/test/fixtures/types/function-argv/webpack.config.js @@ -0,0 +1,6 @@ +module.exports = (argv) => { + return { + entry: 'function', + mode: argv.mode, + }; +}; diff --git a/test/fixtures/types/function-promise/webpack.config.js b/test/fixtures/types/function-promise/webpack.config.js index 9399d1a..0d44aa3 100644 --- a/test/fixtures/types/function-promise/webpack.config.js +++ b/test/fixtures/types/function-promise/webpack.config.js @@ -1,4 +1,4 @@ -module.exports = (/* env*/) => +module.exports = (/* argv */) => new Promise((resolve /* reject */) => { resolve({ entry: 'function-promise', diff --git a/test/fixtures/types/function/webpack.config.js b/test/fixtures/types/function/webpack.config.js index 3188301..556f967 100644 --- a/test/fixtures/types/function/webpack.config.js +++ b/test/fixtures/types/function/webpack.config.js @@ -1,4 +1,4 @@ -module.exports = (/* env */) => { +module.exports = (/* argv */) => { return { entry: 'function', mode: 'development', diff --git a/test/tests/resolve.js b/test/tests/resolve.js index 62321a0..dbd4fa3 100644 --- a/test/tests/resolve.js +++ b/test/tests/resolve.js @@ -20,4 +20,34 @@ describe('Resolve', () => { }); }); } + + it(`should resolve a function config and pass argv`, (done) => { + const configPath = path.join( + __dirname, + `../fixtures/types/function-argv/webpack.config.js` + ); + // eslint-disable-next-line import/no-dynamic-require, global-require + const config = require(configPath); + resolve({ config, configPath }, { mode: 'batman' }).then((result) => { + expect(result.config).toMatchSnapshot(); + done(); + }); + }); + + /* NOTE: This test relies upon --mode=superman being part of the NPM script + running the tests, as it examines the actual process.argv, since argv is + not manually passed to resolve. + */ + it(`should resolve a function config and parse argv`, (done) => { + const configPath = path.join( + __dirname, + `../fixtures/types/function-argv/webpack.config.js` + ); + // eslint-disable-next-line import/no-dynamic-require, global-require + const config = require(configPath); + resolve({ config, configPath }).then((result) => { + expect(result.config).toMatchSnapshot(); + done(); + }); + }); });