webpack.config.js 6.5 KB

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