webpack.config.js 4.0 KB

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