webpack.prod.js 2.2 KB

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