From 49f7fb4cef366a50b9ebe8fd5e1e44db1ae0272f Mon Sep 17 00:00:00 2001 From: Andy Wermke Date: Sat, 5 Aug 2017 15:05:11 +0200 Subject: [PATCH] Improve `createConfig()` error message (#182) In case you passed an invalid setter. --- packages/core/CHANGELOG.md | 4 ++++ packages/core/lib/index.js | 11 +++++++++-- packages/webpack/CHANGELOG.md | 2 ++ packages/webpack/index.js | 2 -- packages/webpack/package.json | 1 - 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index 368f00ba..9ea9b1e8 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -1,5 +1,9 @@ # @webpack-blocks/core - Changelog +## Next version + +- More useful error message when passing invalid blocks to `createConfig()` ([#171](https://github.com/andywer/webpack-blocks/issues/171)) + ## 1.0.0-beta - Added `match()` diff --git a/packages/core/lib/index.js b/packages/core/lib/index.js index b77fae55..57102df3 100644 --- a/packages/core/lib/index.js +++ b/packages/core/lib/index.js @@ -16,8 +16,15 @@ const isFunction = value => typeof value === 'function' * @param {Function[]} configSetters Array of functions as returned by webpack blocks. */ function assertConfigSetters (configSetters) { - if (!Array.isArray(configSetters) || !configSetters.every(isFunction)) { - throw new Error('Expected parameter \'configSetters\' to be an array of functions.') + if (!Array.isArray(configSetters)) { + throw new Error(`Expected parameter 'configSetters' to be an array of functions. Instead got ${configSetters}.`) + } + if (!configSetters.every(isFunction)) { + const invalidElementIndex = configSetters.findIndex(setter => !isFunction(setter)) + throw new Error( + `Expected parameter 'configSetters' to be an array of functions. ` + + `Element at index ${invalidElementIndex} is invalid: ${configSetters[invalidElementIndex]}.` + ) } } diff --git a/packages/webpack/CHANGELOG.md b/packages/webpack/CHANGELOG.md index 9c5704d3..ab8f46cd 100644 --- a/packages/webpack/CHANGELOG.md +++ b/packages/webpack/CHANGELOG.md @@ -3,6 +3,8 @@ ## Next version - Make resolve() prepend custom extensions ([#177](https://github.com/andywer/webpack-blocks/issues/177)) +- Let core `createConfig()` validate the passed setters ([#171](https://github.com/andywer/webpack-blocks/issues/171)) + ## 1.0.0-beta.2 diff --git a/packages/webpack/index.js b/packages/webpack/index.js index 9af219af..4df4268a 100644 --- a/packages/webpack/index.js +++ b/packages/webpack/index.js @@ -4,7 +4,6 @@ * @see https://webpack.github.io/docs/configuration.html */ -const assert = require('assert-plus') const core = require('@webpack-blocks/core') const webpack = require('webpack') const webpackMerge = require('webpack-merge') @@ -43,7 +42,6 @@ exports.sourceMaps = sourceMaps * @return {object} Webpack config object. */ function createConfig (configSetters) { - assert.arrayOfFunc(configSetters, '1st param passed to createConfig.vanilla() must be an array of functions.') return core.createConfig({ webpack, webpackVersion }, [ createEmptyConfig ].concat(configSetters)) } diff --git a/packages/webpack/package.json b/packages/webpack/package.json index c8fc480f..2dd17687 100644 --- a/packages/webpack/package.json +++ b/packages/webpack/package.json @@ -19,7 +19,6 @@ "bugs": "https://github.com/andywer/webpack-blocks/issues", "dependencies": { "@webpack-blocks/core": "^1.0.0-beta", - "assert-plus": "^1.0.0", "webpack-merge": "^4.1.0" }, "devDependencies": {