webpack.config.js 11 KB

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