webpack.config.js 7.3 KB

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