webpack.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 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: [
  24. // 多入口的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. inject: true,
  31. hash: true, //防止缓存
  32. minify: {
  33. removeAttributeQuotes: true, //压缩 去掉引号
  34. removeComments: true, //移除HTML中的注释
  35. collapseWhitespace: true //删除空白符与换行符
  36. }
  37. }),
  38. new HtmlWebpackPlugin({
  39. title: 'page',
  40. template: path.resolve(__dirname, 'src/html', 'page.html'),
  41. filename: 'page.html',
  42. chunks: ['page', 'vendor','common'],
  43. inject: true,
  44. hash: true, //防止缓存
  45. minify: {
  46. removeAttributeQuotes: true, //压缩 去掉引号
  47. removeComments: true, //移除HTML中的注释
  48. collapseWhitespace: true //删除空白符与换行符
  49. }
  50. }),
  51. new HtmlWebpackPlugin({
  52. title: 'scoreSheet',
  53. template: path.resolve(__dirname, 'src/html', 'scoreSheet.html'),
  54. filename: 'scoreSheet.html',
  55. chunks: ['scoreSheet', 'vendor','common'],
  56. inject: true,
  57. hash: true, //防止缓存
  58. minify: {
  59. removeAttributeQuotes: true, //压缩 去掉引号
  60. removeComments: true, //移除HTML中的注释
  61. collapseWhitespace: true //删除空白符与换行符
  62. }
  63. }),
  64. new MiniCssExtractPlugin({
  65. // Options similar to the same options in webpackOptions.output
  66. // both options are optional
  67. filename: 'css/[name].css',
  68. chunkFilename: '[id].css',
  69. }),
  70. new webpack.HotModuleReplacementPlugin(),
  71. new CleanWebpackPlugin(),
  72. ],
  73. optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
  74. splitChunks: {
  75. cacheGroups: {
  76. commons: {
  77. chunks: "initial",
  78. name: "common",
  79. minChunks: 2,
  80. maxInitialRequests: 5, // The default limit is too small to showcase the effect
  81. minSize: 0, // This is example is too small to create commons chunks
  82. reuseExistingChunk: true // 可设置是否重用该chunk(查看源码没有发现默认值)
  83. }
  84. }
  85. }
  86. },
  87. module: {
  88. // 多个loader是有顺序要求的,从右往左写,因为转换的时候是从右往左转换的
  89. rules: [
  90. {
  91. test: /\.js$/,
  92. use: "imports-loader?$=jquery"
  93. },
  94. {
  95. test: /\.css$/,
  96. use: [{
  97. loader: MiniCssExtractPlugin.loader,
  98. options: {
  99. // you can specify a publicPath here
  100. // by default it uses publicPath in webpackOptions.output
  101. },
  102. },
  103. 'css-loader',
  104. ],
  105. },
  106. {
  107. test: /\.less$/,
  108. use: ['style-loader','css-loader','less-loader']
  109. },
  110. { //file-loader 解决css等文件中引入图片路径的问题
  111. // url-loader 当图片较小的时候会把图片BASE64编码,大于limit参数的时候还是使用file-loader 进行拷贝
  112. test: /\.(png|jpg|jpeg|gif|svg)$/,
  113. use: {
  114. loader: 'file-loader',
  115. options: {
  116. outputPath: 'images/', // 图片输出的路径和存储路径保持一致
  117. limit: 100,
  118. name: '[name].[ext]'
  119. }
  120. }
  121. },
  122. ]
  123. },
  124. devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map //生产环境cheap-module-source-map
  125. devServer: {
  126. contentBase: path.join(__dirname, "dist"), //静态文件根目录
  127. proxy: {
  128. '/api': proxyHost
  129. },
  130. hot: true
  131. },
  132. }