webpack.config.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. /*const HtmlWebpackInlineSourcePlugin=require('html-webpack-inline-source-plugin');*/
  4. const CleanWebpackPlugin = require('clean-webpack-plugin') // 清空打包目录的插件
  5. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  6. const CopyWebpackPlugin = require('copy-webpack-plugin');
  7. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  8. const webpack = require('webpack');
  9. const proxyHost = "http://192.168.2.236:5858";
  10. module.exports = {
  11. entry: {
  12. index: path.resolve(__dirname, 'src/js', 'index.js'),
  13. vendor: 'lodash'// 多个页面所需的公共库文件,防止重复打包带入
  14. },
  15. output: {
  16. publicPath: '/', //这里要放的是静态资源CDN的地址
  17. path: path.resolve(__dirname, 'dist'),
  18. filename: 'js/[name].js'
  19. },
  20. resolve: {
  21. extensions: [".js", ".css", ".json"],
  22. alias: {} //配置别名可以加快webpack查找模块的速度
  23. },
  24. plugins: [// 多入口的html文件用chunks这个参数来区分
  25. new HtmlWebpackPlugin({
  26. title: 'index',
  27. template: path.resolve(__dirname, 'src/html', 'index.html'),
  28. filename: 'index.html',
  29. chunks: ['index', 'vendor', 'common'],
  30. hash: true, //防止缓存
  31. inject: true,
  32. minify: {
  33. removeAttributeQuotes: true, //压缩 去掉引号
  34. removeComments: true, //移除HTML中的注释
  35. collapseWhitespace: true //删除空白符与换行符
  36. }
  37. }),
  38. new HtmlWebpackPlugin({
  39. title: 'console.html',
  40. template: path.resolve(__dirname, 'src/html', 'console.html'),
  41. filename: 'console.html',
  42. chunks: ['index', 'vendor', 'common'],
  43. hash: true, //防止缓存
  44. inject: true,
  45. minify: {
  46. removeAttributeQuotes: true, //压缩 去掉引号
  47. removeComments: true, //移除HTML中的注释
  48. collapseWhitespace: true //删除空白符与换行符
  49. }
  50. }),
  51. new HtmlWebpackPlugin({
  52. title: 'itemManager.html',
  53. template: path.resolve(__dirname, 'src/html', 'itemManager.html'),
  54. filename: 'itemManager.html',
  55. chunks: ['index', 'vendor', 'common'],
  56. hash: true, //防止缓存
  57. inject: true,
  58. minify: {
  59. removeAttributeQuotes: true, //压缩 去掉引号
  60. removeComments: true, //移除HTML中的注释
  61. collapseWhitespace: true //删除空白符与换行符
  62. }
  63. }),
  64. new HtmlWebpackPlugin({
  65. title: 'moduleManager.html',
  66. template: path.resolve(__dirname, 'src/html', 'moduleManager.html'),
  67. filename: 'moduleManager.html',
  68. chunks: ['index', 'vendor', 'common'],
  69. hash: true, //防止缓存
  70. inject: true,
  71. minify: {
  72. removeAttributeQuotes: true, //压缩 去掉引号
  73. removeComments: true, //移除HTML中的注释
  74. collapseWhitespace: true //删除空白符与换行符
  75. }
  76. }),
  77. new HtmlWebpackPlugin({
  78. title: 'login.html',
  79. template: path.resolve(__dirname, 'src/html', 'login.html'),
  80. filename: 'login.html',
  81. chunks: ['index', 'vendor', 'common'],
  82. hash: true, //防止缓存
  83. inject: true,
  84. minify: {
  85. removeAttributeQuotes: true, //压缩 去掉引号
  86. removeComments: true, //移除HTML中的注释
  87. collapseWhitespace: true //删除空白符与换行符
  88. }
  89. }),
  90. new HtmlWebpackPlugin({
  91. title: 'qcList.html',
  92. template: path.resolve(__dirname, 'src/html', 'qcList.html'),
  93. filename: 'qcList.html',
  94. chunks: ['index', 'vendor', 'common'],
  95. hash: true, //防止缓存
  96. inject: true,
  97. minify: {
  98. removeAttributeQuotes: true, //压缩 去掉引号
  99. removeComments: true, //移除HTML中的注释
  100. collapseWhitespace: true //删除空白符与换行符
  101. }
  102. }),
  103. new HtmlWebpackPlugin({
  104. title: 'qcScore.html',
  105. template: path.resolve(__dirname, 'src/html', 'qcScore.html'),
  106. filename: 'qcScore.html',
  107. chunks: ['index', 'vendor', 'common'],
  108. hash: true, //防止缓存
  109. inject: true,
  110. minify: {
  111. removeAttributeQuotes: true, //压缩 去掉引号
  112. removeComments: true, //移除HTML中的注释
  113. collapseWhitespace: true //删除空白符与换行符
  114. }
  115. }),
  116. new HtmlWebpackPlugin({
  117. title: 'statistics.html',
  118. template: path.resolve(__dirname, 'src/html', 'statistics.html'),
  119. filename: 'statistics.html',
  120. chunks: ['index', 'vendor', 'common'],
  121. hash: true, //防止缓存
  122. inject: true,
  123. minify: {
  124. removeAttributeQuotes: true, //压缩 去掉引号
  125. removeComments: true, //移除HTML中的注释
  126. collapseWhitespace: true //删除空白符与换行符
  127. }
  128. }),
  129. new CopyWebpackPlugin([
  130. {
  131. from:'src/resource',
  132. to:path.resolve(__dirname,'dist','resource'),
  133. flatten:true, //false会拷贝原始文件夹路径
  134. }
  135. ]),
  136. new MiniCssExtractPlugin({
  137. filename: 'css/[name].css',
  138. chunkFilename: '[id].css'
  139. }),
  140. new webpack.HotModuleReplacementPlugin(),
  141. new CleanWebpackPlugin()
  142. ],
  143. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  144. minimizer: [
  145. new UglifyJsPlugin({
  146. uglifyOptions: {
  147. ie8: true,
  148. compress: {
  149. properties: false,
  150. warnings: false
  151. },
  152. mangle: {
  153. screw_ie8: false,
  154. except: ['e']
  155. },
  156. output: {
  157. beautify: true
  158. },
  159. sourceMap: false
  160. }
  161. })
  162. ],
  163. splitChunks: {
  164. cacheGroups: {
  165. commons: {
  166. chunks: "initial",
  167. name: "common",
  168. minChunks: 2,
  169. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  170. minSize: 0, // This is example is too small to create commons chunks
  171. reuseExistingChunk: true // 可设置是否重用该chunk
  172. }
  173. }
  174. }
  175. },
  176. module: {
  177. rules: [
  178. {
  179. test: /.js$/,
  180. enforce: 'post',
  181. loader: 'es3ify-loader'
  182. },
  183. {
  184. test: /\.m?js$/,
  185. exclude: /(node_modules|bower_components)/,
  186. use: {
  187. loader: 'babel-loader',
  188. options: {
  189. presets:['@babel/preset-env']
  190. }
  191. }
  192. },
  193. {
  194. test: /\.less$/,
  195. use: [
  196. {
  197. loader: 'style-loader', // creates style nodes from JS strings
  198. },
  199. {
  200. loader: 'css-loader', // translates CSS into CommonJS
  201. },
  202. {
  203. loader: 'less-loader', // compiles Less to CSS
  204. },
  205. ],
  206. },
  207. {
  208. test:/\.(png|gif|jpg|jpeg|svg|eot|ttf|woff|woff2)$/,
  209. use:[{
  210. loader:'url-loader',
  211. options:{
  212. limit:10240,
  213. esModule:false,
  214. name:'[name]_[hash:6].[ext]',
  215. outputPath:'images/'
  216. }
  217. }],
  218. exclude:/node_modules/
  219. },{
  220. test:/.html$/,
  221. use:'html-withimg-loader'
  222. }
  223. ]
  224. },
  225. // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  226. mode: 'development',
  227. devServer: {
  228. contentBase: "./dist", //静态文件根目录
  229. proxy: {
  230. '/': proxyHost
  231. },
  232. hot: true,
  233. openPage:'index.html'
  234. }
  235. }