webpack.config.js 8.2 KB

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