webpack.config.js 8.4 KB

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