webpack-config-utils
Version:
Utilities to help your webpack config be easier to read
65 lines (62 loc) • 1.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.removeEmpty = removeEmpty;
/**
* Accepts an array or an object. In the case of an array, remove all undefined values (using `filter`).
* In the case of an object, remove all keys whose values are undefined.
*
* @example
* // Primary use case is in `plugins` and `entry` where `undefined` values can cause issues
* module.exports = {
* ... your config
* entry: removeEmpty({
* app: ifProduction('./indexWithoutCSS', './indexWithCSS'),
* css: ifNotProduction('./style.css')
* }),
* plugins: removeEmpty([
* ifProduction(new webpack.optimize.DedupePlugin()),
* ifProduction(new webpack.LoaderOptionsPlugin({
* minimize: true,
* quiet: true,
* })),
* ifProduction(new webpack.DefinePlugin({
* 'process.env': {
* NODE_ENV: '"production"',
* },
* })),
* ifProduction(new webpack.optimize.UglifyJsPlugin({
* compress: {
* screw_ie8: true,
* warnings: false,
* },
* })),
* new HtmlWebpackPlugin({
* template: './index.html',
* inject: 'head',
* }),
* ifProduction(new OfflinePlugin()),
* ]),
* }
*
* @param {object | Array} input The object to remove keys from or the array to remove values from
* @returns {object | Array} The resulting object or array.
*/
function removeEmpty(input) {
var output = void 0;
if (Array.isArray(input)) {
output = input.filter(function (item) {
return typeof item !== 'undefined';
});
} else {
output = {};
Object.keys(input).forEach(function (key) {
var value = input[key];
if (typeof value !== 'undefined') {
output[key] = value;
}
});
}
return output;
}