webpack.prod.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const merge = require('webpack-merge')
  2. const commonConf = require('./webpack.common.js')
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  4. const TerserJSPlugin = require('terser-webpack-plugin')
  5. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
  6. const CleanWebpackPlugin = require('clean-webpack-plugin')
  7. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  8. const webpack = require('webpack')
  9. const path = require('path')
  10. module.exports = env => {
  11. const prodConf = {
  12. mode: 'production',
  13. devtool: 'cheap-module-source-map',
  14. output: {
  15. path: env && env.test ? path.resolve(__dirname, '../../../pub/zq2019/t_ydzy_simulator') : path.resolve(__dirname, '../../../pub/zq2019/ydzy_simulator'),
  16. publicPath: env && env.test ? '//pub.dwstatic.com/zq2019/t_ydzy_simulator/' : '//pub.dwstatic.com/zq2019/ydzy_simulator/',
  17. filename: 'js/[name]_[contenthash:5].js',
  18. },
  19. module: {
  20. rules: [
  21. {
  22. test: /(?<!\.module)\.(scss|css)$/,
  23. use: [
  24. MiniCssExtractPlugin.loader,
  25. {
  26. loader: 'css-loader',
  27. options: {
  28. importLoaders: 2
  29. }
  30. },
  31. 'sass-loader',
  32. 'postcss-loader'
  33. ]
  34. },
  35. {
  36. test: /(?<=\.module)\.(scss|css)$/,
  37. use: [
  38. MiniCssExtractPlugin.loader,
  39. {
  40. loader: 'css-loader',
  41. options: {
  42. importLoaders: 2,
  43. modules: true
  44. }
  45. },
  46. 'sass-loader',
  47. 'postcss-loader'
  48. ]
  49. }
  50. ]
  51. },
  52. plugins: [
  53. new MiniCssExtractPlugin({
  54. filename: 'css/[name]_[contenthash:5].css',
  55. chunkFilename: 'css/[id]_[contenthash:5].css'
  56. }),
  57. new CleanWebpackPlugin(),
  58. new webpack.DefinePlugin({
  59. DEV: JSON.stringify(false)
  60. })
  61. ].concat(env && env.analyse ? new BundleAnalyzerPlugin() : []),
  62. optimization: {
  63. minimizer: [
  64. new TerserJSPlugin({
  65. sourceMap: true
  66. }),
  67. new OptimizeCSSAssetsPlugin({})
  68. ]
  69. }
  70. }
  71. return merge(commonConf, prodConf)
  72. }