webpack.config.js 10 KB

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