【One by one系列】一步步学习webpack打包
2021-06-04 19:02
标签:引入 nts ext The tput tree 指定 nod mode 前端资源加载/打包工具 js→js→png→less→sass development production ? 0配置, 提供了针对插件和钩子的新API。 webpack预处理源文件,例如ts->js 当文件的大小小于某个指定size时,转成 负责把es6,es7的代码转化为es5的代码 css合并到一起 安装 热启动: 【One by one系列】一步步学习webpack打包 标签:引入 nts ext The tput tree 指定 nod mode 原文地址:https://www.cnblogs.com/RandyField/p/12339672.htmlWebpack Quick Start
1.webpack到底是什么?
2.webpack4新特性
2.1 mode属性
webpack --mode development
2.2 开箱即用webassembly
2.3 支持多种模块类型
2.4 0CJS
webpack4
受Parcel
打包工具启发,尽可能的让开发者运行项目的成本变低。webpack4
不再强制需要webpack.config.js
作为打包的入口配置文件,默认的入口为./src/
和默认的出口./dist
,小项目的福音。2.5 新的插件系统
sync
/callback
/promise
作为插件类型this.hooks={myHook:new SyncHook(...)}
来注册hook了2.6 node.js>=8.9.4
3.安装
3.1本地安装
cnpm install --save-dev webpack
npm install --save-dev webpack-cli
3.2全局安装
npm install --global webpack
4.Quick Start
4.1新建文件夹
mkdir webpack-demo
4.2新建文件
index.js
/**
* es6
* 写法
*
* */
import { hello } from './hello_module'
hello.sayHello()
/**
* node.js
* 写法
*/
// let hello=require('./hello_module')
// hello.sayHello();
hello_module.js
/**
* es6
*/
export default{
sayHello:function () {
console.log("hello")
}
}
/**
* nodejs
*/
module.exports = {
sayHello: function () {
console.log("hello")
}
}
4.3打包
.\node_modules\.bin\webpack --mode development index.js -o output_test_d.js
5kb.\node_modules\.bin\webpack --mode production index.js -o output_test_p.js
2kb5.配置文件
webpack.config.js
const path=require('path')
module.exports={
entry:'./input.js', //入口文件
output:{
path:path.resolve(__dirname,'dist'),
filename:'output.bundle.js'
}
}
const path=require('path')
module.exports={
entry:{
home:'./home.js'
about:'./about.js'
other:'./other.js'
}, //入口文件
output:{
path:path.resolve(__dirname,'dist'),
filename:'[name].bundle.js' //name->home or about or other
},
mode:"development"
}
$:webpack
6.Loaders
6.1 url-loader
DataURL
,base64,不做雪碧图。npm install url-loader --save-dev
import img from ‘./image.png‘
//webpack.config.js
module.exports={
module:{
rules:[
test:/\.(png|jpg|gif)$/i,
use:[{
loader:'url-loader',
options:{
limit:8192
}
}]
]
}
}//当文件是png,jpg,gif时,会使用url-loader来进行处理,文件小于8k时,会把文件以DataUrl的形式存储在文件中
6.2 babel-loader
npm install -D babel-loader @babel/core @babel/preset-env webpack
moudule:{
rules:[
{
test:/\.m?js$,
exclude:/(node_modules|bower_components),
use:{
loader:'babel-loader',
options:{
presets:['@babel/preset-env']
}
}
}
]
}
6.3 sass-loader
npm install sass-loader node-sass -D
npm install style-loader css-loader --save-dev
//webpack.config.js
module.exports={
..
module:{
rules:[{
test:/\.scss$/,
use:[
"style-loader",
"css-loader",
"sass-loader"
]
}]
}
}
7. Plugins插件
7.1 MinCssExtractPlugin.loader
npm install mini-css-extract-plugin -D
const MiniCssExtractPlugin=require("min-css-extract-plugin");
module.exports={
entry:{
home:'./home.js'
about:'./about.js'
other:'./other.js'
}, //入口文件
output:{
path:path.resolve(__dirname,'dist'),
filename:'[name].bundle.js' //name->home or about or other
},
mode:"development",
plugins:[
new MiniCssExtractPlugin({
filename:"[name].css",//name->home
chunkFilename:"[id].css"
})
],
module:{
rules:[{
test:/\.scss$/,
use:[
MinCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}]
}
}
7.2 DefinePlugin
const path=require('path');
const MiniCssExtractPlugin=require("min-css-extract-plugin");
const webpack=require('webpack')
module.exports={
entry:{
home:'./home.js'
about:'./about.js'
other:'./other.js'
}, //入口文件
output:{
path:path.resolve(__dirname,'dist'),
filename:'[name].bundle.js' //name->home or about or other
},
mode:"development",
plugins:[
new MiniCssExtractPlugin({
filename:"[name].css",//name->home
chunkFilename:"[id].css"
}),
new webpack.DefinePlugin({ 'SERVICE_URL':JSON.stringify('http://www.sina.com')
})//就可以直接在js文件中使用SERVICE_URL中使用
],
module:{
rules:[{
test:/\.scss$/,
use:[
MinCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}]
}
}
7.3 HtmlWebpackPlugin
npm install --save-dev html-webpack-plugin
npm install html-webpack-plugin -D
const path=require('path');
const MiniCssExtractPlugin=require("min-css-extract-plugin");
const webpack=require('webpack');
const HtmlWebpackPlugin=require('html-webpack-plugin');
module.exports={
entry:{
home:'./home.js'
about:'./about.js'
other:'./other.js'
}, //入口文件
output:{
path:path.resolve(__dirname,'dist'),
filename:'[name].bundle.js' //name->home or about or other
},
mode:"development",
plugins:[
new MiniCssExtractPlugin({
filename:"[name].css",//name->home
chunkFilename:"[id].css"
}),
new webpack.DefinePlugin({ 'SERVICE_URL':JSON.stringify('http://www.sina.com')
}),//就可以直接在js文件中使用SERVICE_URL中使用
new HtmlWebpackPlugin()
],
module:{
rules:[{
test:/\.scss$/,
use:[
MinCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}]
}
}
8.热替换-自动刷新
npm install webpack-dev-server -D
const path=require('path');
const MiniCssExtractPlugin=require("min-css-extract-plugin");
const webpack=require('webpack');
const HtmlWebpackPlugin=require('html-webpack-plugin');
module.exports={
entry:{
home:'./home.js'
about:'./about.js'
other:'./other.js'
}, //入口文件
output:{
path:path.resolve(__dirname,'dist'),
filename:'[name].bundle.js' //name->home or about or other
},
devServer:{
contentBase:path.join(__dirname,'dist'),
compress:true,
port:8080
},
mode:"development",
plugins:[
new MiniCssExtractPlugin({
filename:"[name].css",//name->home
chunkFilename:"[id].css"
}),
new webpack.DefinePlugin({ 'SERVICE_URL':JSON.stringify('http://www.sina.com')
}),//就可以直接在js文件中使用SERVICE_URL中使用
new HtmlWebpackPlugin()
],
module:{
rules:[{
test:/\.scss$/,
use:[
MinCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}]
}
}
{
"scripts": {
"start": "webpack-dev-server"//会去node_modules寻找
},
"devDependencies": {
"webpack": "^4.41.6",
"webpack-cli": "^3.3.11"
}
}
npm run start
上一篇:JS淘宝浏览商品
下一篇:Node.js基础:第一篇
文章标题:【One by one系列】一步步学习webpack打包
文章链接:http://soscw.com/index.php/essay/90523.html