webpack.config.js
入口和出口文件
1 2 3 4 5 6 7 8 9
| const path = require('path') module.exports = { entry: './src/index.js', output: { filename: "bundle.js", path: path.resolve(__dirname, "./dist"), clean: true, } }
|
生成HTML文件
安装html-webpack-plugin插件
1
| npm i html-webpack-plugin -D
|
该模块用于自动生成HTML文件。在webpack.config.js中引入:
1
| const HtmlWebpackPlugin = require('html-webpack-plugin');
|
通过plugins配置插件:
1 2 3 4 5 6 7
| plugins: [ new HtmlWebpackPlugin({ template: "./index.html", filename: "app.html", inject: 'body', }) ]
|
代码有修改后自动打包:
开发环境
安装webpack-dev-server插件
1
| npm i webpack-dev-server -D
|
1 2 3 4 5 6
| mode: "development", devtool: 'inline-source-map', devServer: { static:'./dist' },
|
命令行执行:
无需打包,通过浏览器访问8080端口实时查看页面效果
资源模块
Resource资源
加载资源导出url
1 2 3 4 5 6 7 8 9 10 11
| module:{ rules: [ { test:/\.png$/, type: "asset/resource", generator: { filename: 'images/[contenthash][ext]' } } ] }
|
示例:在index.js中导入png图片
1 2 3 4 5
| import imgsrc from '../asset/微信.png'
const img = document.createElement('img') img.src = imgsrc document.body.appendChild(img)
|
也可再output中使用assetModuleFilename指定打包路径,优先级低于rules中的配置
1 2 3 4 5 6
| output: { filename: "bundle.js", path: path.resolve(__dirname, "./dist"), clean: true, assetModuleFilename: "images/[contenthash][ext]", },
|
inline资源
导出为Data URL base64格式
1 2 3 4
| { test: /\.svg$/, type: "asset/inline", },
|
示例:导入svg图片
1 2 3 4 5 6
| import logoSvg from './asset/logo.svg'
const img2 = document.createElement('img') img2.src = logoSvg img2.style.cssText='width:400px;height:600px' document.body.appendChild(img2)
|
source资源
导出文件源码
1 2 3 4
| { test: /\.txt$/, type: "asset/source", }
|
示例:导入源码
1 2 3 4 5 6
| import helloTxt from './asset/hello.txt'
const block = document.createElement('div') block.style.cssText = 'width:200px;height:200px;border:1px solid gray' block.textContent = helloTxt document.body.appendChild(block)
|
通用资源类型
自动选择资源类型,小于maxSize用inline类型,大于则用resource类型
1 2 3 4 5 6 7 8 9
| { test: /\.jpg$/, type: "asset", parser: { dataUrlCondition:{ maxSize: 4 * 1024 } } }
|
示例:导入jpg图片
1 2 3 4 5 6
| import jpgMap from './asset/头像.jpg'
const block = document.createElement('div') block.style.cssText = 'width:200px;height:200px;border:1px solid gray' block.textContent = helloTxt document.body.appendChild(block)
|
loader
加载CSS
安装loader
1 2
| npm i css-loader -D npm i style-loader -D
|
在module > rules中配置
1 2 3 4 5 6
| { test: /\.css$/, use: ['style-loader','css-loader'], }
|
抽离和压缩CSS
安装插件
1 2
| npm i mini-css-extract-plugin -D npm i css-minimizer-webpack-plugin -D
|
引入插件
1 2
| const MiniCssExtractPlugin = require('mini-css-extract-plugin') const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
|
在plugin中使用MiniCssExtractPlugin(用于以link标签的形式在html中导入css文件)
1 2 3
| new MiniCssExtractPlugin({ filename:'styles/[contenthash].css' })
|
1 2 3 4 5
| { test: /\.css$/, use: [MiniCssExtractPlugin.loader,'css-loader'], }
|
新配置项optimization中使用CssMinimizerPlugin(用于压缩css文件)
1 2 3 4 5 6
| optimization: { minimizer: [ new CssMinimizerPlugin() ] }, mode:'production'//生产环境下才会压缩(会有其他报错)
|
babel-loader
如果浏览器不支持ES6语法,使用babel-loader,将ES6转为ES5
安装babel-loader,@babel/core,@babel/preset-env
1 2 3
| npm i babel-loader @babel/core @babel/preset-env -D npm i @babel/runtime -D npm i @babel/plugin-transform-runtime -D
|
module > rules中配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| { test:/\.js$/, exclude: /node_modules/, use:{ loader:'babel-loader', options: { presets:['@babel/preset-env'], plugins: [ [ '@babel/plugin-transform-runtime' ] ] } } }
|
代码分离
多入口
entry中设置多入口文件,修改output中的filename
1 2 3 4 5 6 7 8 9 10 11
| entry: { index: './src/index.js', another: './src/another.js' }, output: { filename: "[name].bundle.js", path: path.resolve(__dirname, "./dist"), clean: true, assetModuleFilename: "images/[contenthash][ext]", },
|
防止重复
1 2 3 4 5
| optimization: { splitChunks: { chunks: "all" } }
|