prod.js 3.0 KB

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