webpack.config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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://173.18.12.192:5858";
  8. module.exports = {
  9. entry: {
  10. index: path.resolve(__dirname, 'src', 'index.js'),
  11. total: path.resolve(__dirname, 'src/js', 'total.js'),
  12. vendor: 'lodash'// 多个页面所需的公共库文件,防止重复打包带入
  13. },
  14. output: {
  15. publicPath: '/', //这里要放的是静态资源CDN的地址
  16. path: path.resolve(__dirname, 'dist'),
  17. filename: 'js/[name].js'
  18. },
  19. resolve: {
  20. extensions: [".js", ".css", ".json"],
  21. alias: {} //配置别名可以加快webpack查找模块的速度
  22. },
  23. plugins: [// 多入口的html文件用chunks这个参数来区分
  24. new HtmlWebpackPlugin({
  25. title: 'index',
  26. template: path.resolve(__dirname, 'src', 'index.html'),
  27. filename: 'index.html',
  28. chunks: ['index', 'vendor', 'common'],
  29. inject: true,
  30. hash: true, //防止缓存
  31. minify: {
  32. removeAttributeQuotes: true, //压缩 去掉引号
  33. removeComments: true, //移除HTML中的注释
  34. collapseWhitespace: true //删除空白符与换行符
  35. }
  36. }),
  37. new HtmlWebpackPlugin({
  38. title: 'total',
  39. template: path.resolve(__dirname, 'src', 'total.html'),
  40. filename: 'total.html',
  41. chunks: ['total', 'vendor', 'common'],
  42. inject: true,
  43. hash: true, //防止缓存
  44. minify: {
  45. removeAttributeQuotes: true, //压缩 去掉引号
  46. removeComments: true, //移除HTML中的注释
  47. collapseWhitespace: true //删除空白符与换行符
  48. }
  49. }),
  50. new MiniCssExtractPlugin({
  51. filename: 'css/[name].css',
  52. chunkFilename: '[id].css'
  53. }),
  54. new webpack.HotModuleReplacementPlugin(),
  55. new CleanWebpackPlugin()
  56. ],
  57. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  58. minimizer: [
  59. new UglifyJsPlugin({
  60. uglifyOptions: {
  61. ie8: true,
  62. compress: {
  63. properties: false,
  64. warnings: false
  65. },
  66. mangle: {
  67. screw_ie8: false,
  68. except: ['e']
  69. },
  70. output: {
  71. beautify: true
  72. },
  73. sourceMap: false
  74. }
  75. })
  76. ],
  77. splitChunks: {
  78. cacheGroups: {
  79. commons: {
  80. chunks: "initial",
  81. name: "common",
  82. minChunks: 2,
  83. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  84. minSize: 0, // This is example is too small to create commons chunks
  85. reuseExistingChunk: true // 可设置是否重用该chunk
  86. }
  87. }
  88. }
  89. },
  90. module: {
  91. rules: [
  92. {
  93. test: /.js$/,
  94. enforce: 'post',
  95. loader: 'es3ify-loader'
  96. },
  97. {
  98. test: /\.m?js$/,
  99. exclude: /(node_modules|bower_components)/,
  100. use: {
  101. loader: 'babel-loader',
  102. options: {
  103. presets:['@babel/preset-env']
  104. }
  105. }
  106. },
  107. {
  108. test: /\.css$/,
  109. use: [{
  110. loader: MiniCssExtractPlugin.loader
  111. },
  112. 'css-loader'
  113. ]
  114. },
  115. {
  116. test: /\.less$/,
  117. use : [
  118. MiniCssExtractPlugin.loader,
  119. { loader: "css-loader" },
  120. { loader: "less-loader" }
  121. ]},
  122. {
  123. test: /\.(png|jpg|jpeg|gif|svg)$/,
  124. use: {
  125. loader: 'file-loader',
  126. options: {
  127. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  128. limit: 10000,
  129. name: '[name].[ext]'
  130. }
  131. }
  132. }
  133. ]
  134. },
  135. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  136. mode: 'development',
  137. devServer: {
  138. contentBase: "./dist", //静态文件根目录
  139. proxy: {
  140. '/': proxyHost
  141. },
  142. hot: true,
  143. openPage:'index.html?behospitalCode=10003464_9&hospitalId=3&modeId=1'
  144. },
  145. stats: { children: false }
  146. }