webpack.config.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 webpack = require('webpack');
  6. const proxyHost = "http://192.168.2.236:5050";
  7. module.exports = {
  8. entry: {
  9. index: path.resolve(__dirname, 'src', 'index.js'),
  10. page: path.resolve(__dirname, 'src', 'page.js'),
  11. scoreSheet: path.resolve(__dirname, 'src', 'scoreSheet.js'),
  12. information: path.resolve(__dirname, 'src/js', 'information.js'),
  13. drugInfo: path.resolve(__dirname, 'src/js', 'drugInfo.js'),
  14. disclaimer: path.resolve(__dirname, 'src/js', 'disclaimer.js'),
  15. scale: path.resolve(__dirname, 'src/js', 'scale.js'),
  16. vendor:'lodash' // 多个页面所需的公共库文件,防止重复打包带入
  17. },
  18. output: {
  19. publicPath: '/', //这里要放的是静态资源CDN的地址
  20. path: path.resolve(__dirname, 'dist'),
  21. filename: 'js/[name].js'
  22. },
  23. resolve: {
  24. extensions: [".js", ".css", ".json"],
  25. alias: {} //配置别名可以加快webpack查找模块的速度
  26. },
  27. plugins: [
  28. // 多入口的html文件用chunks这个参数来区分
  29. new HtmlWebpackPlugin({
  30. title: 'index',
  31. template: path.resolve(__dirname, 'src/html', 'index.html'),
  32. filename: 'index.html',
  33. chunks: ['index', 'vendor','common'],
  34. inject: true,
  35. hash: true, //防止缓存
  36. minify: {
  37. removeAttributeQuotes: true, //压缩 去掉引号
  38. removeComments: true, //移除HTML中的注释
  39. collapseWhitespace: true //删除空白符与换行符
  40. }
  41. }),
  42. new HtmlWebpackPlugin({
  43. title: 'page',
  44. template: path.resolve(__dirname, 'src/html', 'page.html'),
  45. filename: 'page.html',
  46. chunks: ['page', 'vendor','common'],
  47. inject: true,
  48. hash: true, //防止缓存
  49. minify: {
  50. removeAttributeQuotes: true, //压缩 去掉引号
  51. removeComments: true, //移除HTML中的注释
  52. collapseWhitespace: true //删除空白符与换行符
  53. }
  54. }),
  55. new HtmlWebpackPlugin({
  56. title: 'scoreSheet',
  57. template: path.resolve(__dirname, 'src/html', 'scoreSheet.html'),
  58. filename: 'scoreSheet.html',
  59. chunks: ['scoreSheet', 'vendor','common'],
  60. inject: true,
  61. hash: true, //防止缓存
  62. minify: {
  63. removeAttributeQuotes: true, //压缩 去掉引号
  64. removeComments: true, //移除HTML中的注释
  65. collapseWhitespace: true //删除空白符与换行符
  66. }
  67. }),
  68. new HtmlWebpackPlugin({
  69. title: 'information',
  70. template: path.resolve(__dirname, 'src/html', 'information.html'),
  71. filename: 'information.html',
  72. chunks: ['information', 'vendor','common'],
  73. inject: true,
  74. hash: true, //防止缓存
  75. minify: {
  76. removeAttributeQuotes: true, //压缩 去掉引号
  77. removeComments: true, //移除HTML中的注释
  78. collapseWhitespace: true //删除空白符与换行符
  79. }
  80. }),
  81. new HtmlWebpackPlugin({
  82. title: 'drugInfo',
  83. template: path.resolve(__dirname, 'src/html', 'drugInfo.html'),
  84. filename: 'drugInfo.html',
  85. chunks: ['drugInfo', 'vendor','common'],
  86. inject: true,
  87. hash: true, //防止缓存
  88. minify: {
  89. removeAttributeQuotes: true, //压缩 去掉引号
  90. removeComments: true, //移除HTML中的注释
  91. collapseWhitespace: true //删除空白符与换行符
  92. }
  93. }),
  94. new HtmlWebpackPlugin({
  95. title: 'disclaimer',
  96. template: path.resolve(__dirname, 'src/html', 'disclaimer.html'),
  97. filename: 'disclaimer.html',
  98. chunks: ['disclaimer', 'vendor','common'],
  99. inject: true,
  100. hash: true, //防止缓存
  101. minify: {
  102. removeAttributeQuotes: true, //压缩 去掉引号
  103. removeComments: true, //移除HTML中的注释
  104. collapseWhitespace: true //删除空白符与换行符
  105. }
  106. }),
  107. new HtmlWebpackPlugin({
  108. title: 'scale',
  109. template: path.resolve(__dirname, 'src/html', 'scale.html'),
  110. filename: 'scale.html',
  111. chunks: ['scale', 'vendor','common'],
  112. inject: true,
  113. hash: true, //防止缓存
  114. minify: {
  115. removeAttributeQuotes: true, //压缩 去掉引号
  116. removeComments: true, //移除HTML中的注释
  117. collapseWhitespace: true //删除空白符与换行符
  118. }
  119. }),
  120. new MiniCssExtractPlugin({
  121. // Options similar to the same options in webpackOptions.output
  122. // both options are optional
  123. filename: 'css/[name].css',
  124. chunkFilename: '[id].css',
  125. }),
  126. new webpack.HotModuleReplacementPlugin(),
  127. new CleanWebpackPlugin(),
  128. ],
  129. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  130. splitChunks: {
  131. cacheGroups: {
  132. commons: {
  133. chunks: "initial",
  134. name: "common",
  135. minChunks: 2,
  136. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  137. minSize: 0, // This is example is too small to create commons chunks
  138. reuseExistingChunk: true // 可设置是否重用该chunk(查看源码没有发现默认值)
  139. }
  140. }
  141. }
  142. },
  143. module: {
  144. // 多个loader是有顺序要求的,从右往左写,因为转换的时候是从右往左转换的
  145. rules: [
  146. {
  147. test: /\.js$/,
  148. use: "imports-loader?$=jquery"
  149. },
  150. {
  151. test: /\.css$/,
  152. use: [{
  153. loader: MiniCssExtractPlugin.loader,
  154. options: {
  155. // you can specify a publicPath here
  156. // by default it uses publicPath in webpackOptions.output
  157. },
  158. },
  159. 'css-loader',
  160. ],
  161. },
  162. {
  163. test: /\.less$/,
  164. use: ['style-loader','css-loader','less-loader']
  165. },
  166. { //file-loader 解决css等文件中引入图片路径的问题
  167. // url-loader 当图片较小的时候会把图片BASE64编码,大于limit参数的时候还是使用file-loader 进行拷贝
  168. test: /\.(png|jpg|jpeg|gif|svg)$/,
  169. use: {
  170. loader: 'file-loader',
  171. options: {
  172. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  173. limit: 100,
  174. name: '[name].[ext]'
  175. }
  176. }
  177. },
  178. ]
  179. },
  180. devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  181. devServer: {
  182. contentBase: path.join(__dirname, "dist"), //静态文件根目录
  183. proxy: {
  184. '/api': proxyHost
  185. },
  186. hot: true
  187. },
  188. }