webpack.config.js 9.8 KB

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