标签:引入 nts ext The tput tree 指定 nod mode
前端资源加载/打包工具
js→js→png→less→sass
development
production
? webpack --mode development
0配置,webpack4
受Parcel
打包工具启发,尽可能的让开发者运行项目的成本变低。webpack4
不再强制需要webpack.config.js
作为打包的入口配置文件,默认的入口为./src/
和默认的出口./dist
,小项目的福音。
提供了针对插件和钩子的新API。
sync
/callback
/promise
作为插件类型this.hooks={myHook:new SyncHook(...)}
来注册hook了cnpm install --save-dev webpack
npm install --save-dev webpack-cli
npm install --global webpack
mkdir webpack-demo
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")
}
}
.\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
2kb
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
webpack预处理源文件,例如ts->js
当文件的大小小于某个指定size时,转成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的形式存储在文件中
负责把es6,es7的代码转化为es5的代码
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']
}
}
}
]
}
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"
]
}]
}
}
css合并到一起
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"
]
}]
}
}
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"
]
}]
}
}
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"
]
}]
}
}
安装
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
标签:引入 nts ext The tput tree 指定 nod mode
原文地址:https://www.cnblogs.com/RandyField/p/12339672.html