Author: 哇哇小仔
Date: 2021-03-18
Webpack版本:webpack 5.24.4
webpack-cli 4.5.0
说明:尚硅谷webpack视频教程总结的原创笔记
const {resolve} = require('path'); // node内置核心模块,用来处理路径问题
module.exports = {
entry: './src/index.js', // 文件入口
output: { // 输出配置
filename: 'built.js', // 输出文件名
path: resolve(__dirname, 'build') // 输出文件路径配置
},
mode: 'development' // 开发环境
}
import './index.css;'
, import './index.less'
npm i style-loader css-loader less-loader less -D
const {resolve} = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'built.js',
path: resolve(__dirname, 'build')
},
// loader 的配置
module:{
rules: [
{
test: /\.css$/,
use: [
// use中的loader从右向左,从下向上,依次执行
// 创建 style 标签, 将 js 中的样式资源插入进行, 添加到 head 中生效
'style-loader',
// 将 css 文件变成 commonjs 模块加载 js 中, 里面内容是样式字符串
'css-loader'
]
},
{
test: /\.less$/,
// 将 less 文件编译成 css 文件,需要下载 less-loader 和 less
use: ['style-loader', 'css-loader', 'less-loader']
}
]
},
plugins: [ ], // 详细plugins的配置
mode: 'development', // 开发模式
// mode: 'production', // 生产模式
}
./src/index.html
, ./src/index.js
, webpack.config.js
npm i --save-dev html-webpack-plugin
const {resolve} = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: 'built.js',
path: resolve(__dirname, 'build')
},
module: {
rules: [],
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
})
],
mode: 'development'
}
webpack
html-webpack-plugiin
默认会创建一个空的HTML,自动引入打包输出的所有资源(css/js)./src/index.js
, ./src/index.html
, ./src/index.less
, 三张图片(angular.jpg, react.png, vue.jpg)url-loader
和file-loader
(处理样式中引入的图片,比如background-imgage)html-loader
(webpack4负责处理html中的img引入的图片)html-withimg-loader
(webpack 5.24.4负责处理html中的img引入的图片) const {resolve} = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index/js',
output: {
filename: 'built.js',
path: resolve(__dirname, 'build')
},
module: {
rules: [
{
test: /\.less$/,
use: ['style-loader', 'css-loader', 'less-loader']
},
{
// 处理样式中引入的图片资源
test: /\.(png|jpg|gif)/,
loader: 'url-loader',
options: {
// 当图片小于8kb时,就会被base64处理
limit: 8*1024,
esModule: false,
// [hash:10]取hash值的前十位,冒号后边一定不能有空格
// [ext]取文件原来的扩展名
name: "[hash:10].[ext]"
}
},
{
test: /\.html$/,
// 处理HTML文件的img图片,负责引入img,从而能被url-loader进行处理
loader: "html-withimg-loader"
}
]
},
plugins: [
new HtmlWebpackPlugin({template: './src/index.html'})
],
mode: 'development'
}
webpack
html-loader
用来打包img中引入的图片,但是webpack 5.24.4中出错,使用html-withimg-loader
const {resolve} = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: 'built.js',
path: resolve(__dirname, 'build')
},
module: {
rules:[
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
},
// 打包其他资源(除了html/js/css资源以外的资源)
{
// 排除 html, css, less, js资源
exclude: /\.(html|css|less|js)$/,
loader: "file-loader",
name: "[hash:10].[ext]"
}
]
},
plugins: [{
template: "./src/index.html"
}],
mode: "development"
}
npm i webpack-dev-server -D
// 除了webpack的五大元素之外,多加一个devServer属性的设置
devServer: {
contentBase: resolve(__dirname, 'build'),
compress: true,
port: 3000,
open: true
}
npx webpack serve
(webpack@5的启动命令)npx webpack-dev-server
(webpack@4的启动命令) const {resolve} = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/js/index.js',
output: {
filename: './js/built.js',
path: resolve(__dirname, 'build')
},
module{
rules: [
{ // less资源打包
// 打包后,样式资源合并在js中,不再有单独的css文件,因此不需要设置输出文件路径
test: /\.less$/,
use: ["style-loader", "css-loader", "less-loader"]
},
{ // css资源打包
test: /\.css$/,
use: ["style-loader", "css-loader"]
},
{ // 图片资源打包
test: /\.(jpg|png|gif)$/,
loader: "url-loader",
options: {
limit: 8 * 1024,
name: "[hash:10].[ext]",
esModule: false,
outputPath: "imgs"
}
},
{ // img链接中的图片资源
test: /\.html$/,
loader: "html-withimg-loader"
},
{ // 其他资源
exclude: /\.(html|css|less|js|jpg|png|gif)$/,
loader: "file-loader",
options: {
name: "[hash:10].[ext]",
outputPath: "media"
}
}
]
},
plugins: [
// 打包html资源
new HtmlWebpackPlugin({
template: "./src/index.html"
})
],
mode: "development", // 开发模式,默认的是生产模式production
devServer: {
contentBase: resolve(__dirname, "build"),
compress: true,
port: 3000,
open: true
}
}
npx webpack serve