webpack 基本使用

新建项目

初始化包管理配置文件 package.json

1
npm init -y

安装 jQuery

1
npm install jquery -S

项目安装webpack

安装相关的两个包

1
npm install webpack@5.42.1 webpack-cli@4.7.2 -D

配置webpack

在项目根目录,创建名为webpack.config.js的webpack配置文件,并初始化配置

1
2
3
4
5
6
7
8
9
10
11
const path = require('path')

module.exports = {
//mode 用来指定构建模式。可选值有 development 和 production
mode: 'development',
entry: path.join(__dirname, './src/index.js'),
output: {
path: path.join(__dirname, './dist'),
filename: 'bundle.js'
}
}

在 package.json 的 scripts 节点下,新增 dev 脚步如下:

1
"dev": "webpack"

webpack 插件

  1. webpack-dev-server
  • 修改源代码,自动打包和构建
  1. html-webpack-plugin
  • 通过插件自定制index.html页面的内容
安装 webpack-dev-server
1
npm install webpack-dev-server@3.11.2 -D

修改 package.json -> scripts 中的dev 命令如下:

1
"dev": "webpack serve"

加载内存中的 bundel.js

1
<script src="/bundle.js"></scipt>

html-webpack-plugin

安装插件

1
npm install html-webpack-plugin@5.3.2 -D
配置html-webpack-plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

//1. 导入html-webpack-plugin 这个插件,得到插件的构造函数
const HtmlPlugin = require('html-webpack-plugin')
// 2. new 构造函数 ,创建插件实例
const htmlPlugin = new HtmlPlugin({
// 指定要复制哪个页面
template:'./src/index.html',
// 指定复制出来的文件名和存放路径
filename:'./index.html'
})

module.exports = {
//mode 用来指定构建模式。可选值有 development 和 production
mode: 'development',

// 插件的数组,将来webpack在运行时,会加载并调用
plugins:[htmlPlugin]
}

devServer节点

1
2
3
4
5
6
7
module.exports = {
devServer:{
open:true,
host:'127.0.0.1'
port:8081,
}
}

打包处理css文件

  1. 运行 npm i style-loader@3.0.0 css-loader@5.2.6 -D 命令,安装处理css文件的loader
  2. 在webpack.config.js 的 module -> rules 数组中,添加 loader 规则如下:
1
2
3
4
5
6
module: {
rules: [{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}]
}

其中,text表示匹配的文件类型,use 表示对应要调用的loader

注意:

  • use 数组中指定的loader 顺序是固定的
  • 多个loader 的调用顺序是: 从后往前调用

打包处理 less 文件

  1. 运行 npm i less-loader@10.0.1 less@4.1.1 -D 命令
  2. 在webpack.config.js 的model -> rules 数组中 ,添加loader规则如下:
1
2
3
4
5
6
module: {
rules: [{
test: /\.less$/,
use: ['style-loader', 'css-loader','less-loader']
}]
}

打包处理样式表中与 url 路径相关的文件

  1. 运行 npm i url-loader@4.1.1 file-loader@6.2.0 -D
  2. 在 webpack.config.js 的 module -> rules 数组中,添加 loader 规则如下:
1
2
3
4
5
6
module: {
rules: [{
test: /\.jpg|png|gif$/,
use: ['url-loader?limit=10000']
}]
}

打包处理js文件中的高级语法

安装相关依赖包

1
npm i babel-loader@8.2.2 @babel/core@7.14.6 @babel/plugin-proposal-decorators@7.14.5 -D

在webpack.config.js 的 module -> rules 数组中,添加loader 规则如下:

1
2
3
4
5
rules:{
test: /\.js$/,
use: 'babel-loader',
exclude: /node_modules/
}

配置babel-loader

在项目根目录下,创建名为 babel.config.js 的配置文件,定义 Babel 的配置项如下:

1
2
3
4
5
6
7
8
module.exports = {
// 声明 babel 可用插件
plugins: [
['@babel/plugin-proposal-decorators', {
legacy: true
}]
]
}

配置webpack的打包发布

在package.json 文件的 scripts 节点下,新增 build 命令如下:

1
2
3
4
"scripts":{
"dev":"webpack serve",// 开发环境中,运行 dev 命令
"build":"webpack --mode production" // 项目发布时,运行 build 命令
}

把图片文件统一生成到image目录中

修改 webpack.config.js 中的 url-loader 配置项,新增 outputPath 选项即可指定图片文件的输出路径:

1
2
3
4
5
6
7
8
9
10
11
{
test: /\.jpg|png|gif$/,
use: {
loader: 'url-loader',
options: {
limit: 10,
// 明确指定把打包生成的图片文件,存储到dist目录下 的 image 文件夹中
outputPath: 'image'
}
}
}

自动清理 dist 目录下的旧文件

为了在每次打包发布时,自动清理掉 dist 目录中的旧文件,可以安装并配置 clean-webpack-plugin 插件:

1
2
3
// 1. 安装清理dist 目录的 webpack 插件
npm install clean-webpack-plugin@3.0.0 -D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 左侧的花括号是解构赋值
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

const webpackConfig = {
plugins: [
/**
* All files inside webpack's output.path directory will be removed once, but the
* directory itself will not be. If using webpack 4+'s default configuration,
* everything under <PROJECT_DIR>/dist/ will be removed.
* Use cleanOnceBeforeBuildPatterns to override this behavior.
*
* During rebuilds, all webpack assets that are not used anymore
* will be removed automatically.
*
* See `Options and Defaults` for information
*/
new CleanWebpackPlugin(),
],
};

module.exports = webpackConfig;

解决 source map 的问题

开发环境下,推荐在webpack.config.js 中添加如下的配置,即可保证运行时报错的行数与源代码的行数保持一致:

1
2
3
4
module.exports = {
mode: 'development',
devtool: 'eval-source-map',
}

只定位行数不暴露源码,将 devtool 的值设置为 nosources-source-map

路径别名

在 webpack.config.js 文件里,

1
2
3
4
5
6
7
8
module.exports = {
resolve: {
alias: {
// 告诉 webpack , 程序员写的代码中,@ 符号表示 src 这一层目录
'@': path.join(__dirname, './src/')
}
}
}