一、简答题 1、Webpack 的构建流程主要有哪些环节?如果可以请尽可能详尽的描述 Webpack 打包的整个过程。
- 初始化参数:根据用户在命令窗口输入的参数以及 webpack.config.js 文件的配置,得到最后的配置。
- 开始编译:根据上一步得到的最终配置初始化得到一个 compiler 对象,注册所有的插件 plugins,插件开始监听 webpack 构建过程的生命周期的环节(事件),不同的环节会有相应的处理,然后开始执行编译。
- 确定入口:根据 webpack.config.js 文件中的 entry 入口,开始解析文件构建 AST 语法树,找出依赖,递归下去。
- 编译模块:递归过程中,根据文件类型和 loader 配置,调用相应的 loader 对不同的文件做不同的转换处理,再找出该模块依赖的模块,然后递归本步骤,直到项目中依赖的所有模块都经过了本步骤的编译处理。
- 编译过程中,有一系列的插件在不同的环节做相应的事情,比如 UglifyPlugin 会在 loader 转换递归完对结果使用 UglifyJs 压缩覆盖之前的结果;再比如 clean-webpack-plugin ,会在结果输出之前清除 dist 目录等等。
- 完成编译并输出:递归结束后,得到每个文件结果,包含转换后的模块以及他们之间的依赖关系,根据 entry 以及 output 等配置生成代码块 chunk。
- 打包完成:根据 output 输出所有的 chunk 到相应的文件目录。
2、Loader 和 Plugin 有哪些不同?请描述一下开发 Loader 和 Plugin 的思路。
Loader: 主要起转换的作用。因为:webpack只识别js文件,像.css, .ts, .jsx, .vue文件并不识别,因此loader可以对这样的文件进行转换,让webpak识别这样的文件 Plugin: 插件。主要通过webpack内部的钩子,在webpack构建的不同阶段执行一些任务。
Loader: 开发思路:
- 通过module.exports导出一个函数
- 该函数默认参数一个参数source(即要处理的资源文件)
- 在函数体中处理资源(loader里配置响应的loader后)
- 通过return返回最终打包后的结果(这里返回的结果需为字符串形式)
Plugin开发思路:
- 通过钩子机制实现
- 插件必须是一个函数或包含apply方法的对象
- 在方法体内通过webpack提供的API获取资源做响应处理
- 将处理完的资源通过webpack提供的方法返回该资源
二、编程题 1、使用 Webpack 实现 Vue 项目打包任务
视频地址: https://www.bilibili.com/video/BV1vv411P76t/
配置文件: config/webpack.common.js
const resolveApp = require('./paths')
const { DefinePlugin } = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { merge } = require('webpack-merge')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const prodConfig = require('./webpack.prod')
const devConfig = require('./webpack.dev')
const commonConfig = {
entry: './src/main.js',
resolve: {
extensions: [".js", ".json", '.vue'],
alias: {
'@': resolveApp('./src')
}
},
output: {
filename: 'js/main.js',
path: resolveApp('./dist')
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
esModule: false
}
},
'postcss-loader'
]
},
{
test: /\.less$/,
use: [
'style-loader',
'css-loader',
'postcss-loader',
'less-loader'
]
},
{
test: /\.(png|svg|gif|jpe?g)$/,
type: 'asset',
generator: {
filename: "img/[name].[hash:4][ext]"
},
parser: {
dataUrlCondition: {
maxSize: 30 * 1024
}
}
},
{
test: /\.(ttf|woff2?)$/,
type: 'asset/resource',
generator: {
filename: 'font/[name].[hash:3][ext]'
}
},
{
test: /\.js?$/,
use: ['babel-loader', 'eslint-loader']
},
{
test: /\.vue$/,
use: ['vue-loader', 'eslint-loader']
}
]
},
plugins: [
new HtmlWebpackPlugin({
title: 'webpack-vue-实战',
template: './public/index.html'
}),
new DefinePlugin({
BASE_URL: '"./"'
}),
new VueLoaderPlugin()
]
}
module.exports = (env) => {
const isProduction = env.production
process.env.NODE_ENV = isProduction ? 'production' : 'development'
const config = isProduction ? prodConfig : devConfig
const mergeConfig = merge(commonConfig, config)
return mergeConfig
}
config/webpack.dev.js
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin')
module.exports = {
mode: 'development',
devtool: 'cheap-module-source-map',
target: 'web',
devServer: {
hot: true,
hotOnly: true,
port: 4000,
open: true,
compress: true,
historyApiFallback: true,
proxy: {
'/api': {
target: 'https://api.github.com',
pathRewrite: { "^/api": "" },
changeOrigin: true
}
}
},
plugins: [
new ReactRefreshWebpackPlugin()
]
}
config/webpack.prod.js
const CopyWebpackPlugin = require('copy-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
module.exports = {
mode: 'production',
plugins: [
new CleanWebpackPlugin(),
new CopyWebpackPlugin({
patterns: [
{
from: 'public',
globOptions: {
ignore: ['**/index.html']
}
}
]
})
]
}
config/paths.js
const path = require('path')
const appDir = process.cwd()
const resolveApp = (relativePath) => {
return path.resolve(appDir, relativePath)
}
module.exports = resolveApp
babel.config.js
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
]
}
postcss.config.js
module.exports = {
plugins: [
require('postcss-preset-env')
]
}
package.json
{
"name": "vue-app-base",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "webpack serve --config ./config/webpack.common.js --env development",
"build": "webpack --config ./config/webpack.common.js --env production",
"lint": "webpack serve --config ./config/webpack.common.js --env development"
},
"dependencies": {
"core-js": "^3.6.5",
"vue": "^2.6.11"
},
"devDependencies": {
"@babel/cli": "^7.14.8",
"@babel/core": "^7.15.0",
"@babel/plugin-transform-arrow-functions": "^7.14.5",
"@babel/plugin-transform-block-scoping": "^7.14.5",
"@babel/preset-env": "^7.15.0",
"@babel/preset-react": "^7.14.5",
"@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
"@vue/cli-plugin-babel": "^4.5.13",
"autoprefixer": "^10.3.1",
"axios": "^0.21.1",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.2.2",
"clean-webpack-plugin": "^4.0.0-alpha.0",
"copy-webpack-plugin": "^9.0.1",
"css-loader": "^6.2.0",
"eslint": "^7.32.0",
"eslint-config-standard": "^16.0.3",
"eslint-loader": "^4.0.2",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-vue": "^7.17.0",
"html-webpack-plugin": "^5.3.2",
"less": "^4.1.1",
"less-loader": "^10.0.1",
"postcss": "^8.3.6",
"postcss-cli": "^8.3.1",
"postcss-loader": "^6.1.1",
"postcss-preset-env": "^6.7.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-refresh": "^0.10.0",
"react-router-dom": "^5.2.0",
"style-loader": "^3.2.1",
"vue-loader": "^15.9.8",
"vue-template-compiler": "^2.6.14",
"webpack": "^5.47.1",
"webpack-cli": "^4.8.0",
"webpack-dev-middleware": "^5.0.0",
"webpack-dev-server": "^3.11.2",
"webpack-merge": "^5.8.0"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {
"no-var": "warn"
}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
|