webpack.config.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const CleanWebpackPlugin = require('clean-webpack-plugin') // 清空打包目录的插件
  4. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  5. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  6. const webpack = require('webpack');
  7. const proxyHost = "http://192.168.2.241:5050";
  8. module.exports = {
  9. entry: {
  10. index: path.resolve(__dirname, 'src', 'index.js'),
  11. page: path.resolve(__dirname, 'src', 'page.js'),
  12. information: path.resolve(__dirname, 'src/js', 'information.js'),
  13. disclaimer: path.resolve(__dirname, 'src/js', 'disclaimer.js'),
  14. scale: path.resolve(__dirname, 'src/js', 'scale.js'),
  15. vendor: 'lodash'// 多个页面所需的公共库文件,防止重复打包带入
  16. },
  17. output: {
  18. publicPath: '/', //这里要放的是静态资源CDN的地址
  19. path: path.resolve(__dirname, 'dist'),
  20. filename: 'js/[name].js'
  21. },
  22. resolve: {
  23. extensions: [".js", ".css", ".json"],
  24. alias: {} //配置别名可以加快webpack查找模块的速度
  25. },
  26. plugins: [// 多入口的html文件用chunks这个参数来区分
  27. new HtmlWebpackPlugin({
  28. title: 'index',
  29. template: path.resolve(__dirname, 'src/html', 'index.html'),
  30. filename: 'index.html',
  31. chunks: ['index', 'vendor', 'common'],
  32. inject: true,
  33. hash: true, //防止缓存
  34. minify: {
  35. removeAttributeQuotes: true, //压缩 去掉引号
  36. removeComments: true, //移除HTML中的注释
  37. collapseWhitespace: true //删除空白符与换行符
  38. }
  39. }),
  40. new HtmlWebpackPlugin({
  41. title: 'page',
  42. template: path.resolve(__dirname, 'src/html', 'page.html'),
  43. filename: 'page.html',
  44. chunks: ['page', 'vendor', 'common'],
  45. inject: true,
  46. hash: true, //防止缓存
  47. minify: {
  48. removeAttributeQuotes: true, //压缩 去掉引号
  49. removeComments: true, //移除HTML中的注释
  50. collapseWhitespace: true //删除空白符与换行符
  51. }
  52. }),
  53. new HtmlWebpackPlugin({
  54. title: 'information',
  55. template: path.resolve(__dirname, 'src/html', 'information.html'),
  56. filename: 'information.html',
  57. chunks: ['information', 'vendor', 'common'],
  58. inject: true,
  59. hash: true, //防止缓存
  60. minify: {
  61. removeAttributeQuotes: true, //压缩 去掉引号
  62. removeComments: true, //移除HTML中的注释
  63. collapseWhitespace: true //删除空白符与换行符
  64. }
  65. }),
  66. new HtmlWebpackPlugin({
  67. title: 'drugInfo',
  68. template: path.resolve(__dirname, 'src/html', 'drugInfo.html'),
  69. filename: 'drugInfo.html',
  70. chunks: ['information', 'vendor', 'common'],
  71. inject: true,
  72. hash: true, //防止缓存
  73. minify: {
  74. removeAttributeQuotes: true, //压缩 去掉引号
  75. removeComments: true, //移除HTML中的注释
  76. collapseWhitespace: true //删除空白符与换行符
  77. }
  78. }),
  79. new HtmlWebpackPlugin({
  80. title: 'disclaimer',
  81. template: path.resolve(__dirname, 'src/html', 'disclaimer.html'),
  82. filename: 'disclaimer.html',
  83. chunks: ['vendor', 'common', 'disclaimer'],
  84. inject: true,
  85. hash: true, //防止缓存
  86. minify: {
  87. removeAttributeQuotes: true, //压缩 去掉引号
  88. removeComments: true, //移除HTML中的注释
  89. collapseWhitespace: true //删除空白符与换行符
  90. }
  91. }),
  92. new HtmlWebpackPlugin({
  93. title: 'scale',
  94. template: path.resolve(__dirname, 'src/html', 'scale.html'),
  95. filename: 'scale.html',
  96. chunks: ['vendor', 'common', 'scale'],
  97. inject: true,
  98. hash: true, //防止缓存
  99. minify: {
  100. removeAttributeQuotes: true, //压缩 去掉引号
  101. removeComments: true, //移除HTML中的注释
  102. collapseWhitespace: true //删除空白符与换行符
  103. }
  104. }),
  105. new MiniCssExtractPlugin({
  106. filename: 'css/[name].css',
  107. chunkFilename: '[id].css'
  108. }),
  109. new webpack.HotModuleReplacementPlugin(),
  110. new CleanWebpackPlugin()
  111. ],
  112. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  113. minimizer: [
  114. new UglifyJsPlugin({
  115. uglifyOptions: {
  116. ie8: true,
  117. compress: {
  118. properties: false,
  119. warnings: false
  120. },
  121. mangle: {
  122. screw_ie8: false,
  123. except: ['e']
  124. },
  125. output: {
  126. beautify: true
  127. },
  128. sourceMap: false
  129. }
  130. })
  131. ],
  132. splitChunks: {
  133. cacheGroups: {
  134. commons: {
  135. chunks: "initial",
  136. name: "common",
  137. minChunks: 2,
  138. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  139. minSize: 0, // This is example is too small to create commons chunks
  140. reuseExistingChunk: true // 可设置是否重用该chunk
  141. }
  142. }
  143. }
  144. },
  145. module: {
  146. rules: [
  147. {
  148. test: /.js$/,
  149. enforce: 'post',
  150. loader: 'es3ify-loader'
  151. },
  152. {
  153. test: /\.m?js$/,
  154. exclude: /(node_modules|bower_components)/,
  155. use: {
  156. loader: 'babel-loader',
  157. options: {
  158. presets:['@babel/preset-env']
  159. }
  160. }
  161. },
  162. {
  163. test: /\.css$/,
  164. use: [{
  165. loader: MiniCssExtractPlugin.loader
  166. },
  167. 'css-loader'
  168. ]
  169. },
  170. {
  171. test: /\.less$/,
  172. use: [{
  173. loader: 'style-loader' // creates style nodes from JS strings
  174. }, {
  175. loader: 'css-loader' // translates CSS into CommonJS
  176. }, {
  177. loader: 'less-loader' // compiles Less to CSS
  178. }]
  179. },
  180. {
  181. test: /\.(png|jpg|jpeg|gif|svg)$/,
  182. use: {
  183. loader: 'file-loader',
  184. options: {
  185. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  186. limit: 10000,
  187. name: '[name].[ext]'
  188. }
  189. }
  190. }
  191. ]
  192. },
  193. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  194. mode: 'development',
  195. devServer: {
  196. contentBase: path.join(__dirname, "dist"), //静态文件根目录
  197. proxy: {
  198. '/api': proxyHost
  199. },
  200. hot: true
  201. }
  202. }