webpack.config.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin'); // 抽离css,不插入到head标签里
  4. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); // 压缩css
  5. const TerserJSPlugin = require('terser-webpack-plugin')
  6. const webpack = require('webpack');
  7. const htmlPlugin = new HtmlWebpackPlugin({
  8. template: './src/index.html',
  9. filename: 'index.html',
  10. hash: true,
  11. minify: {
  12. removeAttributeQuotes: true,
  13. collapseWhitespace: true
  14. }
  15. });
  16. const aboutPlugin = new HtmlWebpackPlugin({
  17. template: './src/about.html',
  18. filename: 'about.html',
  19. hash: true,
  20. minify: {
  21. removeAttributeQuotes: true,
  22. collapseWhitespace: true
  23. }
  24. });
  25. /* const htmlPlugin = ['index', 'about'].map(chunkName => {
  26. return new HtmlWebpackPlugin({
  27. template: `./src/${chunkName}.html`,
  28. filename: `${chunkName}.html`,
  29. hash: true,
  30. chunks: [`./src/${chunkName}`],
  31. minify: {
  32. removeAttributeQuotes: true,
  33. collapseWhitespace: true
  34. }
  35. });
  36. }) */
  37. const miniPlugin = new MiniCssExtractPlugin({
  38. filename: 'main.css'
  39. });
  40. // 每个模块中注入jq
  41. const webpackProvidePlugin = new webpack.ProvidePlugin({
  42. $: 'jquery',
  43. })
  44. module.exports = {
  45. optimization: {
  46. minimize: true, // 开启压缩,默认false
  47. minimizer: [
  48. new TerserJSPlugin({
  49. terserOptions: {
  50. output: {
  51. comments: false // 删除js文件中的注释
  52. }
  53. },
  54. extractComments: false, // 不将注释提取到单独的文件
  55. sourceMap: true // 源码映射
  56. }),
  57. new OptimizeCSSAssetsPlugin()
  58. ]
  59. },
  60. devServer: {
  61. // port: 3000,
  62. // progress: true,
  63. contentBase: path.resolve(__dirname, './build'),
  64. open: true,
  65. host: '127.0.0.1'
  66. },
  67. mode: 'development', // development production
  68. entry: {
  69. index: './src/index.js',
  70. about: './src/about.js'
  71. },
  72. output: {
  73. filename: '[name].bundle.js',
  74. path: path.resolve(__dirname, 'dist'),
  75. },
  76. plugins: [
  77. miniPlugin,
  78. htmlPlugin,
  79. aboutPlugin,
  80. webpackProvidePlugin
  81. ],
  82. module: {
  83. rules: [{
  84. test: /\.(html)$/, // html文件中img标签src没有被打包的问题
  85. use: {
  86. loader: 'html-loader',
  87. options: {
  88. attrs: ['img:src', 'img:data-src', 'audio:src'],
  89. minimize: true
  90. }
  91. }
  92. },
  93. {
  94. test: /\.(png|jpg)$/,
  95. use: [{
  96. loader: 'url-loader',
  97. options: {
  98. limit: 1,
  99. esModule: false,
  100. outputPath: 'img/'
  101. }
  102. }]
  103. },
  104. {
  105. test: /\.js$/,
  106. use: {
  107. loader: 'babel-loader',
  108. options: {
  109. presets: [
  110. '@babel/preset-env'
  111. ]
  112. }
  113. },
  114. include: path.resolve(__dirname, 'src'),
  115. exclude: /node_modules/
  116. },
  117. {
  118. test: /\.css$/,
  119. use: [
  120. MiniCssExtractPlugin.loader,
  121. 'css-loader',
  122. 'postcss-loader'
  123. ]
  124. },
  125. {
  126. test: /\.less$/,
  127. use: [
  128. MiniCssExtractPlugin.loader,
  129. 'css-loader',
  130. 'less-loader',
  131. 'postcss-loader',
  132. ]
  133. }
  134. ]
  135. }
  136. }