Skip to content
This repository has been archived by the owner on Sep 11, 2018. It is now read-only.

Commit

Permalink
fix: argv not passed to function config in all scenarios (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape authored May 29, 2018
1 parent 1c85b99 commit f84918d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/resolve.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const minimist = require('minimist');

const fromFunction = (config, argv) => {
const result = config(argv);

Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions test/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/types/function-argv/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = (argv) => {
return {
entry: 'function',
mode: argv.mode,
};
};
2 changes: 1 addition & 1 deletion test/fixtures/types/function-promise/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = (/* env*/) =>
module.exports = (/* argv */) =>
new Promise((resolve /* reject */) => {
resolve({
entry: 'function-promise',
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/types/function/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = (/* env */) => {
module.exports = (/* argv */) => {
return {
entry: 'function',
mode: 'development',
Expand Down
30 changes: 30 additions & 0 deletions test/tests/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit f84918d

Please sign in to comment.