1. 基本配置
  1. const path = require('path');
  2. // 基础配置
  3. const config = {
  4. entry: path.resolve(__dirname, 'src/index.js') || ['@babel/polyfill', path.resolve(__dirname, 'src/index.js')],
  5. outputPath: path.resolve(__dirname, 'build'),
  6. port: 8080,
  7. htmlTemplate: path.resolve(__dirname, 'src/index.html')
  8. };
  9. // PLUGIN 插件
  10. const HtmlWebpackPlugin = require('html-webpack-plugin'),
  11. MiniCssExtractPlugin = require('mini-css-extract-plugin'),
  12. // 优化css资源的插件
  13. OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin'),
  14. // 优化js资源插件
  15. UglifyJsWebpackPlugin = require('uglifyjs-webpack-plugin'),
  16. Webpack = require('webpack');
  17. let htmlPlugin = new HtmlWebpackPlugin({
  18. // 指定编译得文件
  19. template: config.htmlTemplate,
  20. // 编译后得文件名
  21. filename: 'index.html',
  22. // 文件压缩
  23. minify: {
  24. // 去除空格
  25. collapseWhitespace: true,
  26. // 去除注释
  27. removeComments: true,
  28. // 去除标签中的双引号
  29. removeAttributeQuote: true,
  30. // 去除设置的属性值为 undefined 的值
  31. removeEmptyAttributes: true
  32. }
  33. }),
  34. miniCssPlugin = new MiniCssExtractPlugin({
  35. filename: 'main.css'
  36. }),
  37. optimizeCssPlugin = new OptimizeCssAssetsWebpackPlugin(),
  38. uglifyJsPlugin = new UglifyJsWebpackPlugin({
  39. cache: true, // 是否使用缓存
  40. parallel: true, // 是否并发编译
  41. sourceMap: true // 启动源码映射
  42. }),
  43. // 向每一个模块注入全局变量
  44. webpack = new Webpack.ProvidePlugin({});
  45. // LOADER 加载器
  46. const cssLoader = {
  47. test: /\.(css|less)$/i,
  48. // 使用加载器
  49. use: [
  50. // 'style-loader',
  51. MiniCssExtractPlugin.loader,
  52. 'css-loader',
  53. 'postcss-loader',
  54. {
  55. loader: 'less-loader',
  56. options: {}
  57. }
  58. ],
  59. exclude: /node_modules/,
  60. include: path.resolve(__dirname, 'src')
  61. },
  62. jsLoader = {
  63. test: /\.js$/i,
  64. use: [
  65. {
  66. loader: 'babel-loader',
  67. options: {
  68. // ES6 -> ES5
  69. presets: ['@babel/preset-env'],
  70. plugins: [
  71. ['@babel/plugin-proposal-class-properties', {"loose": true}],
  72. ['@babel/plugin-proposal-decorators', {"legacy": true}],
  73. ['@babel/plugin-transform-runtime']
  74. ]
  75. }
  76. }
  77. ],
  78. exclude: /node_modules/,
  79. include: path.resolve(__dirname, 'src')
  80. },
  81. imgLoader = {
  82. test: /\.(png|jpg|peng|webp|gif|bmp|ico)$/i,
  83. use: [{
  84. loader: 'url-loader',
  85. options: {
  86. // 小于 200kb, 处理为base64
  87. limit: 200 * 1024,
  88. outputPath: 'images'
  89. }
  90. }]
  91. },
  92. // 处理HTML文件中使用img元素引入的图片
  93. htmlImgLoader = {
  94. test: /\.(html|htm|xml)$/i,
  95. use: ['html-withimg-loader']
  96. };
  97. module.exports = {
  98. // 环境 development production
  99. mode: 'production',
  100. // 入口
  101. entry: config.entry,
  102. // 出口
  103. output: {
  104. publicPath: './',
  105. // 文件名
  106. filename: 'bundle.min.[hash:8].js',
  107. // 输出路径
  108. path: config.outputPath
  109. },
  110. // 设置优化项
  111. optimization: {
  112. // 压缩优化
  113. minimizer: [
  114. optimizeCssPlugin,
  115. uglifyJsPlugin
  116. ]
  117. },
  118. // webpack-dev-server
  119. devServer: {
  120. // 创建服务指定的端口号
  121. port: config.port,
  122. // 打包显示进度条
  123. progress: true,
  124. // 指定当前服务处理资源的目录
  125. contentBase: config.outputPath,
  126. // 自动打开浏览器
  127. open: true
  128. },
  129. // plugins
  130. plugins: [
  131. htmlPlugin,
  132. miniCssPlugin,
  133. webpack
  134. ],
  135. // loader 模块加载器
  136. module: {
  137. rules: [
  138. cssLoader,
  139. jsLoader,
  140. imgLoader,
  141. htmlImgLoader
  142. ]
  143. }
  144. };
  1. package.json
    {
    "name": "webpack-app",
    "version": "1.0.0",
    "description": "webpack学习",
    "main": "./src/index.js",
    "scripts": {
     "dev": "webpack --config webpack.config.dev.js",
     "pro": "webpack --config webpack.config.pro.js",
     "server": "webpack-dev-server --config webpack.config.dev.js"
    },
    "keywords": [
     "webpack"
    ],
    "author": "korealu",
    "license": "ISC",
    "browserslist": [
     "> 1%",
     "last 2 versions"
    ],
    "devDependencies": {
     "@babel/core": "^7.8.4",
     "@babel/plugin-proposal-class-properties": "^7.8.3",
     "@babel/plugin-proposal-decorators": "^7.8.3",
     "@babel/plugin-transform-runtime": "^7.8.3",
     "@babel/preset-env": "^7.8.4",
     "autoprefixer": "^9.7.4",
     "babel-loader": "^8.0.6",
     "css-loader": "^3.4.2",
     "eslint": "^6.8.0",
     "eslint-loader": "^3.0.3",
     "expose-loader": "^0.7.5",
     "file-loader": "^5.0.2",
     "html-webpack-plugin": "^3.2.0",
     "html-withimg-loader": "^0.1.16",
     "less": "^3.11.1",
     "less-loader": "^5.0.0",
     "mini-css-extract-plugin": "^0.9.0",
     "optimize-css-assets-webpack-plugin": "^5.0.3",
     "postcss-loader": "^3.0.0",
     "style-loader": "^1.1.3",
     "uglifyjs-webpack-plugin": "^2.2.0",
     "url-loader": "^3.0.0",
     "webpack": "^4.41.6",
     "webpack-cli": "^3.3.11",
     "webpack-dev-server": "^3.10.3"
    },
    "dependencies": {
     "@babel/polyfill": "^7.8.3",
     "@babel/runtime": "^7.8.4"
    }
    }