webpack.prod.config.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * @Author: liyxt
  3. * @Date: 2019-07-02 10:02:16
  4. * @LastEditors: liyxt
  5. * @LastEditTime: 2020-04-14 14:31:30
  6. * @Description: file content
  7. */
  8. /**
  9. * 生产环境配置
  10. */
  11. const webpack = require('webpack');
  12. const common = require('./webpack.common');
  13. const path = require('path');
  14. const merge = require('webpack-merge');
  15. const configJSON = require('../config.json');
  16. const buildEntry = require('./buildEntry');
  17. const TerserPlugin = require('terser-webpack-plugin');
  18. // const Visualizer = require('webpack-visualizer-plugin');
  19. let { patch } = configJSON;
  20. let { project, branch, provider } = patch || {};
  21. module.exports = function(env, argv) {
  22. let { mode, hash, client, fse, buildPath, outputPath } = env;
  23. if (client === 'mobile') {
  24. process.env.PROJECT_CLIENT = 'mobile';
  25. }
  26. buildPath = buildPath || configJSON.buildEntryPath || './src/*/*/*/*/index.js';
  27. let buildWithoutHTML = configJSON.buildWithoutHTML;
  28. buildWithoutHTML && typeof buildWithoutHTML === 'string' && (buildWithoutHTML = [buildWithoutHTML]);
  29. let prodConfig = {
  30. mode: 'production',
  31. entry: {},
  32. output: {
  33. path: path.resolve(__dirname, `../${outputPath || 'dist'}`),
  34. publicPath: '../../../../',
  35. library: '[name]',
  36. libraryTarget: 'umd',
  37. chunkFilename: '[name].js'
  38. },
  39. devtool: false,
  40. plugins: [
  41. // new FileListPlugin(),
  42. // new ChunkListPlugin(),
  43. new webpack.BannerPlugin({
  44. banner:
  45. '@ncctag ' +
  46. JSON.stringify({
  47. project,
  48. branch,
  49. provider,
  50. date: new Date().toLocaleString()
  51. }), // 其值为字符串,将作为注释存在
  52. raw: false, // 如果值为 true,将直出,不会被作为注释
  53. entryOnly: false // 如果值为 true,将只在入口 chunks 文件中添加
  54. }),
  55. new webpack.DefinePlugin({
  56. NODE_ENV: JSON.stringify(mode),
  57. ISMA: configJSON.isMA,
  58. LOGIN_INFO: JSON.stringify(configJSON.directConnectInfo),
  59. MA_INFO: JSON.stringify(configJSON.maInfo)
  60. })
  61. // new Visualizer()
  62. ],
  63. optimization: {
  64. minimize: true, // 是否启用压缩
  65. splitChunks: {
  66. automaticNameDelimiter: '_'
  67. },
  68. minimizer: [
  69. new TerserPlugin({
  70. parallel: 4,
  71. sourceMap: true,
  72. extractComments: false,
  73. terserOptions: {
  74. // compress: {
  75. // drop_console: true
  76. // },
  77. output: {
  78. comments: /@ncctag/i
  79. }
  80. }
  81. })
  82. ]
  83. }
  84. };
  85. if (hash === 'false') {
  86. hash = false;
  87. } else if (hash === 'true') {
  88. hash = true;
  89. }
  90. // test模式,加source-map调试
  91. mode === 'test' && (prodConfig.devtool = 'source-map');
  92. // 节点加hash,参照不加hash
  93. prodConfig.output.filename = hash ? '[name].[hash:8].js' : '[name].js';
  94. // 获取入口
  95. let { entries, plugins, externals } = buildEntry({ buildPath, buildWithoutHTML, hash, client, mode, fse });
  96. Object.assign(common.externals, externals);
  97. Object.assign(prodConfig.entry, entries);
  98. prodConfig.plugins.push(...plugins);
  99. prodConfig = merge(common, prodConfig);
  100. return prodConfig;
  101. };