webpack.config.js 9.3 KB

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