Article Directory
- 1. Overview of `webpack`
- 2. Basic usage of `webpack`
-
- 2.1 Create a list of interlaced color change cases
- 2.2 Install and configure `webpack` in the project
- 2.3 Configure the entry and exit of the package
- 2.4 Configure the automatic packaging function of `webpack`
- 2.5 Configure `html-webpack-plugin` to generate preview page
- 2.6 Configure parameters related to automatic packaging
- 3. Loaders in `webpack`
-
- 3.1 Package non-js modules with `loader`
- 3.2 The calling process of `loader`
- 3.3 Basic usage of loader in `webpack`
-
- 3.3.1 Packaging and processing `css` files
- 3.3.2 Package processing of `less` files
- 3.3.3 Package processing `scss` file
- 3.3.4 Configure `postCSS` to automatically add a compatible prefix of `css`
- 3.3.5 Pack the pictures and font files in the style sheet
- 3.3.6 Package processing of advanced syntax in `js` files
1. webpack
Overview
webpack
It is a popular front-end project construction tool (packaging tool) that can solve web
the dilemma faced in current development. webpack
Provides friendly modular support , as well as powerful functions such as code compression and confusion , handling js
compatibility issues , performance optimization, etc., so that programmers can focus on the implementation of specific functions, improving development efficiency and project maintainability .
2. webpack
Basic use
2.1 Create a list of interlaced color change cases
- Create a new project blank directory and run the
npm init –y
command to initialize the package management configuration filepackage.json
- New
src
source code directory - New
src -> index.html
homepage - Initialize the basic structure of the home page
- Run the
npm install jquery –S
command, installjQuery
- Through the modular form, realize the interlaced color effect of the list
2.2 Installation and configuration in the projectwebpack
- Run the
npm install webpack webpack-cli –D
command to installwebpack
related packages - In the project root directory, create
webpack.config.js
awebpack
configuration file named - In
webpack
the configuration file, initialize the following basic configuration:
module.exports = {
mode: 'development' // mode 用来指定构建模式
}
- Under
package.json
thescripts
node in the configuration file , the newdev
script is as follows:
"scripts": {
"dev": "webpack" // script 节点下的脚本,可以通过 npm run 执行
}
- Run the
npm run dev
command in the terminal to start thewebpack
project packaging
2.3 Configure the entry and exit of the package
webpack
The default convention in the 4.x version:
- The packaged entry file is
src -> index.js
- The packaged output file is
dist -> main.js
If you want to modify the entry and exit of the package, you can webpack.config.js
add the following configuration information in it:
const path = require('path'); // 导入 node.js 中专门操作路径的模块
module.exports = {
entry: path.join(__dirname, './src/index.js'), // 打包入口文件的路径
output: {
path: path.join(__dirname, './dist'), // 输出文件的存放路径
filename: 'bundle.js' // 输出文件的名称
}
}
2.4 Configured webpack
automatic packaging function
- Run
npm install webpack-dev-server –D
commands to install tools that support automatic project packaging - Modify
package.json -> scripts
thedev
command as follows:
"scripts": {
"dev": "webpack-dev-server" // script 节点下的脚本,可以通过 npm run 执行
}
- Will
src -> index.html
, thescript
script reference path, modify the"/buldle.js“
- Run the
npm run dev
command and re-package - Visit the
http://localhost:8080
address in the browser to check the automatic packaging effect
Note :
webpack-dev-server
Will start a real-time packagedhttp
serverwebpack-dev-server
The output file generated by the package is placed in the project root directory by default, and it is virtual and invisible
2.5 Configure to html-webpack-plugin
generate a preview page
- Run the
npm install html-webpack-plugin –D
command to install the plugin that generates the preview page - Modify the
webpack.config.js
file header area and add the following configuration information:
// 导入生成预览页面的插件,得到一个构造函数
const HtmlWebpackPlugin = require('html-webpack-plugin')
const htmlPlugin = new HtmlWebpackPlugin({
// 创建插件的实例对象
template: './src/index.html', // 指定要用到的模板文件
filename: 'index.html' // 指定生成的文件的名称,该文件存在于内存中,在目录中不显示
});
- Modify
webpack.config.js
the configuration objects exposed to the outside in the file, and add the following configuration nodes:
module.exports = {
plugins: [htmlPlugin] // plugins 数组是 webpack 打包期间会用到的一些插件列表
}
2.6 Configure parameters related to automatic packaging
// package.json中的配置
// --open 打包完成后自动打开浏览器页面
// --host 配置 IP 地址
// --port 配置端口
"scripts": {
"dev": "webpack-dev-server --open --host 127.0.0.1 --port 8888"
},
3. webpack
Loader in
3.1 By loader
packaging non- js
modules
In the actual development process, webpack
only .js
modules ending with the suffix name can be packaged and processed by default . Other .js
modules that webpack
do not end with the suffix name cannot be processed by default. The loader
loader needs to be called to package normally, otherwise an error will be reported!
loader
The loader can assist in webpack
packaging and processing specific file modules, such as:
less-loader
Can package and process.less
related filessass-loader
Can package and process.scss
related filesurl-loader
It can be packaged dealcss
withurl
file-related paths
3.2 loader
call process
webpack
The basic use of the loader in 3.3
3.3.1 Package processing css
file
- Run the
npm i style-loader css-loader -D
command, install and processcss 文件的 loader
- In
webpack.config.js
themodule -> rules
array, theloader
rules for adding are as follows:
// 所有第三方文件模块的匹配规则
module: {
rules: [
{
test: /\.css$/, use: ['style-loader', 'css-loader'] }
]
}
Among them, test
indicates the matching file type, use
indicates the corresponding file to be called, loader
and executes from back to front
Note :
use
Theloader
order specified in the array is fixedloader
The order of multiple calls is: call from back to front
3.3.2 Package processing less
file
- Run
npm i less-loader less -D
command - In
webpack.config.js
themodule -> rules
array, theloader
rules for adding are as follows:
// 所有第三方文件模块的匹配规则
module: {
rules: [
{
test: /\.less$/, use: ['style-loader', 'css-loader', 'less-loader'] }
]
}
3.3.3 Package processing scss
file
- Run
npm i sass-loader node-sass -D
command - In
webpack.config.js
themodule -> rules
array, theloader
rules for adding are as follows:
// 所有第三方文件模块的匹配规则
module: {
rules: [
{
test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'] }
]
}
3.3.4 Configure postCSS
automatically added css
compatible prefix
- Run
npm i postcss-loader autoprefixer -D
command - Create
postcss
a configuration file in the project root directorypostcss.config.js
and initialize the following configuration:
const autoprefixer = require('autoprefixer') // 导入自动添加前缀的插件
module.exports = {
plugins: [autoprefixer] // 挂载插件
}
- In
webpack.config.js
themodule -> rules
array, modifyingcss
theloader
rules are as follows:
module: {
rules: [
{
test: /\.css$/, use: ['style-loader', 'css-loader', 'postcss-loader'] }
]
}
3.3.5 Pack the pictures and font files in the style sheet
- Run
npm i url-loader file-loader -D
command - In
webpack.config.js
themodule -> rules
array, theloader
rules for adding are as follows:
module: {
rules: [{
test: /\.jpg|png|gif|bmp|ttf|eot|svg|woff|woff2$/,
use: 'url-loader?limit=16940'
}]
}
Note :
- Which
?
is after theloader
parameter entry limit
Used to specify the size of the picture, the unit is byte (byte
), onlylimit
pictures smaller than the size will be converted tobase64
pictures
3.3.6 js
Advanced syntax in package processing file
- Install
babel
converter-related packages:npm i babel-loader @babel/core @babel/runtime -D
- Install
babel
the packages related to the syntax plugin:npm i @babel/preset-env @babel/plugin-transformruntime @babel/plugin-proposal-class-properties –D
- In the project root directory, create a
babel
configuration filebabel.config.js
and initialize the basic configuration as follows:
module.exports = {
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-transform-runtime', '@babel/plugin-proposalclass-properties']
};
- In
webpack.config.js
themodule -> rules
array, theloader
rules for adding are as follows:
// exclude 为排除项,表示 babel-loader 不需要处理 node_modules 中的 js 文件
{
test: /\.js$/, use: 'babel-loader', exclude: /node_modules/ }