UNPKG

1.73 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = mergeWebpackConfig;
5
6var _webpackMerge = _interopRequireDefault(require("webpack-merge"));
7
8var _isFunction = _interopRequireDefault(require("lodash/isFunction"));
9
10var _omit = _interopRequireDefault(require("lodash/omit"));
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14const IGNORE_SECTIONS = ['entry', 'externals', 'output', 'watch', 'stats', 'styleguidist'];
15const IGNORE_SECTIONS_ENV = {
16 development: [],
17 // For production builds, we'll ignore devtool settings to avoid
18 // source mapping bloat.
19 production: ['devtool']
20};
21const IGNORE_PLUGINS = ['CommonsChunkPlugins', 'MiniHtmlWebpackPlugin', 'HtmlWebpackPlugin', 'OccurrenceOrderPlugin', 'DedupePlugin', 'UglifyJsPlugin', 'TerserPlugin', 'HotModuleReplacementPlugin'];
22const merge = (0, _webpackMerge.default)({
23 // Ignore user’s plugins to avoid duplicates and issues with our plugins
24 customizeArray: _webpackMerge.default.unique('plugins', IGNORE_PLUGINS, plugin => plugin.constructor && plugin.constructor.name)
25});
26
27/**
28 * Merge two Webpack configs.
29 *
30 * In the user config:
31 * - Ignores given sections (options.ignore).
32 * - Ignores plugins that shouldn’t be used twice or may cause issues.
33 *
34 * @param {object} baseConfig
35 * @param {object|Function} userConfig
36 * @param {string} env
37 * @return {object}
38 */
39function mergeWebpackConfig(baseConfig, userConfig, env = 'production') {
40 const userConfigObject = (0, _isFunction.default)(userConfig) ? userConfig(env) : userConfig;
41 const safeUserConfig = (0, _omit.default)(userConfigObject, IGNORE_SECTIONS.concat(IGNORE_SECTIONS_ENV[env]));
42 return merge(baseConfig, safeUserConfig);
43}
\No newline at end of file