All files merge.js

11.76% Statements 4/34
0% Branches 0/25
0% Functions 0/4
11.76% Lines 4/34
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                                1x 1x                                                                                                             1x                                                                         1x          
/**
 * @description - merge development webpack configuration
 * @author - huang.jian <hjj491229492@hotmail.com>
 */
 
/**
 * @typedef {object} ServerCliConfig
 *
 * @property {string} bootstrap
 * @property {string} publicPath
 * @property {boolean} react
 * @property {boolean} sourceMap
 * @property {boolean} disableHot
 */
 
// external
const _ = require('lodash');
const IP = require('ip');
 
/**
 * @param {object} base
 * @param {ServerCliConfig} cliConfig
 */
function develop(base, cliConfig) {
  const config = _.cloneDeep(base);
  const { bootstrap, publicPath, react, sourceMap, disableHot } = cliConfig;
  const entrance = bootstrap || './src/main.js';
  const HMR = [
    `webpack-dev-server/client/?http://${IP.address()}:8100`,
    'webpack/hot/dev-server'
  ];
  config.entry = disableHot ? [entrance] : [...HMR, entrance];
 
  if (publicPath) {
    config.output.publicPath = publicPath;
  }
 
  // Support react-hot-loader
  if (react && !disableHot) {
    config.entry = ['react-hot-loader/patch', ...config.entry];
  }
 
  if (sourceMap) {
    config.devtool = 'source-map';
  }
 
  return config;
}
 
/**
 * @description - merge webpack-dev-server configuration
 *
 * @param base
 * @param packager
 * @param {ServerCliConfig} cliConfig
 */
function server(base, packager, cliConfig) {
  const config = _.cloneDeep(base);
  const proxy = Reflect.get(packager, 'proxy') || [];
  const { disableHot } = cliConfig;
 
  return Object.assign(
    {},
    config,
    {
      proxy,
      hot: !disableHot,
      host: IP.address()
    }
  );
}
 
const OptimizePluginNames = ['UglifyJsPlugin', 'ImageminPlugin', 'CompressionPlugin', 'HtmlMinifyPlugin'];
 
/**
 * @param {object} base
 * @param {object} options
 */
function production(base, options = {}) {
  const { bootstrap, publicPath, plugins, disableCompress, sourceMap } = options;
  const config = _.cloneDeep(base);
  const entrance = bootstrap || './src/main.js';
 
  config.entry = [entrance];
 
  if (publicPath) {
    config.output.publicPath = publicPath;
  }
 
  if (sourceMap) {
    config.devtool = 'source-map';
  }
 
  if (plugins) {
    config.plugins = [
      ...config.plugins.slice(0, -1),
      ...plugins,
      ...config.plugins.slice(-1)
    ];
  }
 
  if (disableCompress) {
    config.plugins = config.plugins
      .filter((plugin) => !OptimizePluginNames.includes(plugin.constructor.name));
  }
 
  return config;
}
 
module.exports = {
  develop,
  server,
  production
};