# 各模块功能

# Webpack-dev-server

webpack开发环境具有热更新Hot reload的特点

需要插件

webpack-dev-server

# html-webpack-plugin

GitHub (opens new window)

html-webpack-plugin详解 (opens new window)

yarn add html-webpack-plugin -D
1

该插件将为你生成一个 HTML5 文件, 在 body 中使用 script 标签引入你所有 webpack 生成的 bundle。 只需添加该插件到你的 webpack 配置中,如下所示:

new HtmlWebpackPlugin({
   title: 'Webpack Vue Template',
   template: path.resolve(__dirname, '../public/index.html'),
   inject: 'body',
   hash: true
 })
1
2
3
4
5
6

具体配置看文档 (opens new window)

而package.json中的script加上了这样一行

"dev": "webpack server --mode development --config ./build/webpack.config.js -o dist",
1

这样一来,Hot reload环境就搭建好了,浏览器可以实时地更新了。

关于模块热更新,具体还可以看webpack官方文档 (opens new window)

# clean-webpack-plugin

每次打包自动清空dist文件夹

webpack 5.2已内置功能

Output.clean = true即可开启

开发的时候浏览器会有日志

如果不想看见这些日志,可以通过设置devServer.client (opens new window)去除

将其设置为warn以下就看不见了

# 配置eslint

yarn add eslint eslint-webpack-plugin -D
1

webpack.dev.js

const EslintWebpackPlugin = require('eslint-webpack-plugin')
plugins: [
  // @ts-ignore
  new EslintWebpackPlugin()
]
1
2
3
4
5

有了eslint还需要规则

我们这里使用prettier的规则

yarn add eslint-plugin-prettier eslint-config-prettier -D
1

最终配置eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true
  },
  // extends: ["plugin:vue/essential", "eslint:recommended"],
  extends: [
    'eslint:recommended',
    'plugin:prettier/recommended',
    'plugin:vue/recommended',
    'eslint-config-prettier'
  ],
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    // 重写prettier验证规则
    'prettier/prettier': [
      'warn',
      // 针对会被 ESLint 格式化的文件类型,Prettier 会作为 ESLint 的一个规则运行并格式化文件,因此需要添加如下配置
      { semi: false, singleQuote: true, trailingComma: 'none' }
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 最终的配置

const path = require('path')
const { VueLoaderPlugin } = require('vue-loader')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  entry: './src/main.js',
  output: {
    clean: true,
    path: path.resolve(__dirname, './dist'),
    filename: 'index_bundle.js',
  },
  target: 'web',
  devServer: {
    // static: {
    //   directory: path.join(__dirname, 'public'),
    // },
    client:{
      logging: 'warn'
    },
    compress: true, // 开启gzip压缩
    open: true, // 自动打开浏览器
    port: 9000
  },
  module: {
    rules: [
      { test: /\.vue$/, loader: 'vue-loader' },
      {
        test: /\.s[ac]ss$/i,
        use: [
          // Creates `style` nodes from JS strings
          'style-loader',
          // Translates CSS into CommonJS
          'css-loader',
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  ['postcss-preset-env', {// 其他选项
                  }]
                ]
              }
            }
          },
          // Compiles Sass to CSS
          'sass-loader'
        ]
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      title: 'Webpack Vue Template',
      template: path.resolve(__dirname, '../public/index.html'),
      inject: 'body',
      hash: true
    })
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

# TypeScript支持

具体看这篇文章 (opens new window)

vue-shim.d.ts

declare module "*.vue" {
  import Vue from "vue";
  export default Vue;
}
1
2
3
4

# Vue2.x JSX支持

yarn add @vue/babel-preset-jsx @vue/babel-helper-vue-jsx-merge-props -D
1

使用babel-loader处理jsx文件

在webpack.base.js中添加rules

{ test: /\.jsx?$/, exclude: /node_modules/, use: ['babel-loader'] },
1

babel.config.js配置

module.exports = {
  presets: [
    ['@babel/preset-env', { useBuiltIns: 'usage', corejs: 3.19, modules: false }],
    ['@vue/babel-preset-jsx', { injectH: true }]
  ]
}
1
2
3
4
5
6

# 报错

报错ReferenceError: BASE_URL is not defined

解决方法 (opens new window)

参考