prod.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const path = require('path')
  2. const webpack = require('webpack')
  3. const HtmlWebpackPlugin = require('html-webpack-plugin')
  4. const CopyWebpackPlugin = require('copy-webpack-plugin')
  5. const VueLoaderPlugin = require('vue-loader/lib/plugin')
  6. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  7. const TerserPlugin = require('terser-webpack-plugin')
  8. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
  9. const config = require('../config')
  10. module.exports = {
  11. mode: 'production',
  12. entry: path.resolve(__dirname, '../../src/index.js'),
  13. output: {
  14. path: path.resolve(__dirname, '../../dist'),
  15. filename: 'assets/scripts/[name].[chunkhash:8].js',
  16. publicPath: config.prod.publicPath,
  17. },
  18. module: {
  19. rules: [
  20. {
  21. test: /\.css$/i,
  22. use: [
  23. MiniCssExtractPlugin.loader,
  24. {
  25. loader: 'css-loader',
  26. options: {
  27. importLoaders: 1,
  28. },
  29. },
  30. 'postcss-loader',
  31. ],
  32. },
  33. {
  34. test: /\.s[ac]ss$/i,
  35. use: [
  36. MiniCssExtractPlugin.loader,
  37. {
  38. loader: 'css-loader',
  39. options: {
  40. importLoaders: 2,
  41. },
  42. },
  43. 'postcss-loader',
  44. 'sass-loader',
  45. ],
  46. },
  47. {
  48. test: /\.(png|jpe?g|gif|svg)$/i,
  49. loader: 'file-loader',
  50. options: {
  51. name: 'assets/images/[name].[contenthash:8].[ext]',
  52. },
  53. },
  54. {
  55. test: /\.(ttf|otf|eot|woff2?)$/,
  56. loader: 'file-loader',
  57. options: {
  58. name: 'assets/fonts/[name].[contenthash:8].[ext]',
  59. },
  60. },
  61. {
  62. test: /\.js$/,
  63. enforce: 'pre',
  64. exclude: /node_modules/,
  65. loader: 'eslint-loader',
  66. },
  67. {
  68. test: /\.js$/,
  69. exclude: /node_modules/,
  70. loader: 'babel-loader',
  71. },
  72. {
  73. test: /\.vue$/,
  74. loader: 'vue-loader',
  75. },
  76. ],
  77. },
  78. optimization: {
  79. minimize: true,
  80. minimizer: [
  81. new TerserPlugin({
  82. terserOptions: {
  83. parse: {
  84. ecma: 8,
  85. },
  86. compress: {
  87. drop_console: true,
  88. },
  89. mangle: {
  90. safari10: true,
  91. },
  92. },
  93. cache: true,
  94. parallel: true,
  95. }),
  96. new OptimizeCSSAssetsPlugin(),
  97. ],
  98. splitChunks: {
  99. chunks: 'all',
  100. maxSize: 224000,
  101. cacheGroups: {
  102. vendors: {
  103. test: /[\\/]node_modules[\\/]/,
  104. name: 'vendors',
  105. },
  106. },
  107. },
  108. },
  109. externals: {
  110. vue: 'Vue',
  111. },
  112. resolve: {
  113. extensions: [
  114. '.js',
  115. '.vue',
  116. ],
  117. alias: {
  118. '@': path.resolve(__dirname, '../../src'),
  119. },
  120. },
  121. devtool: 'inline-source-map',
  122. plugins: [
  123. new HtmlWebpackPlugin({
  124. template: config.template,
  125. inject: true,
  126. minify: {
  127. collapseWhitespace: true,
  128. removeAttributeQuotes: true,
  129. removeComments: true,
  130. },
  131. }),
  132. new VueLoaderPlugin(),
  133. new webpack.HashedModuleIdsPlugin(),
  134. new webpack.DefinePlugin({
  135. 'process.env': config.prod.env,
  136. }),
  137. new MiniCssExtractPlugin({
  138. filename: 'assets/styles/[name].[contenthash:8].css',
  139. chunkFilename: 'assets/styles/[name].[contenthash:8].chunk.css',
  140. }),
  141. new CopyWebpackPlugin([
  142. {
  143. from: path.resolve(__dirname, '../../static'),
  144. to: 'static',
  145. ignore: ['.*'],
  146. },
  147. ]),
  148. ],
  149. }