前端资源加载/打包工具
js→js→png→less→sass
webpack --mode development
0配置,webpack4
受Parcel
打包工具启发,尽可能的让开发者运行项目的成本变低。webpack4
不再强制需要webpack.config.js
作为打包的入口配置文件,默认的入口为./src/
和默认的出口./dist
,小项目的福音。
提供了针对插件和钩子的新API。
hook
由hooks
对象统一管理,它将所有的hook
作为可扩展的类属性。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: {
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: /\.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 path = require('path')
const MiniCssExtractPlugin=require("mini-css-extract-plugin");
const devMode = process.env.NODE_ENV !== 'production';
module.exports = {
entry: {
output: './src/index.js'
}, //入口文件
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js' //name->home or about or other
},
// mode: "production",
mode:"development",
plugins:[
new MiniCssExtractPlugin({
filename:"[name].[hash].css",//name->home
chunkFilename:"[id].[hash].css"
})
],
module: {
rules: [
{
test: /\.(png|jpg|gif)$/i,
use: [{
loader: 'url-loader',
options: {
limit: 8192
}
}]
}
,
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
,
{
test: /\.scss$/,
use: [
// "style-loader",
MiniCssExtractPlugin.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("mini-css-extract-plugin");
const devMode = process.env.NODE_ENV !== 'production';
const webpack=require('webpack')
const HtmlWebpackPlugin=require('html-webpack-plugin')
module.exports = {
entry: {
output: './src/index.js'
}, //入口文件
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js' //name->home or about or other
},
// mode: "production",
mode: "development",
plugins: [
new MiniCssExtractPlugin({
filename: "[name].[hash].css",//name->home
chunkFilename: "[id].[hash].css"
}),
new webpack.DefinePlugin({
'SERVICE_URL': JSON.stringify('https://dev.example.com')
}),
// new HtmlWebpackPlugin()//base
new HtmlWebpackPlugin({
title:"Randy App",
filename:"index.html",
template:'template.html'
})
],
module: {
rules: [
{
test: /\.(png|jpg|gif)$/i,
use: [{
loader: 'url-loader',
options: {
limit: 8192
}
}]
}
,
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
,
{
test: /\.scss$/,
use: [
// "style-loader",
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}
]
}
}
webpack
项目,启动服务,热替换操作,自动化,页面更新也是自动化npm install webpack-dev-server -D
const path = require('path')
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const devMode = process.env.NODE_ENV !== 'production';
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: {
output: './src/index.js'
}, //入口文件
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js' //name->home or about or other
},
devServer: {
open:true,
contentBase: path.join(__dirname, "dist"),//output 路径
compress: true,//
port: 3000
// ,hot:false
//hot配置是否启用模块的热替换功能,
//devServer的默认行为是在发现源代码被变更后,
//通过自动刷新整个页面来做到事实预览,
//而开启hot后,
//将在不刷新整个页面的情况下通过新模块替换老模块来做到实时预览。
},
// mode: "production",
mode: "development",
plugins: [
new MiniCssExtractPlugin({
filename: "[name].[hash].css",//name->home
chunkFilename: "[id].[hash].css"
}),
new webpack.DefinePlugin({
'SERVICE_URL': JSON.stringify('https://dev.example.com')
}),
// new HtmlWebpackPlugin()//base
new HtmlWebpackPlugin({
title: "Randy App",
filename: "index.html",
template: 'template.html'
})
],
module: {
rules: [
{
test: /\.(png|jpg|gif)$/i,
use: [{
loader: 'url-loader',
options: {
limit: 8192
}
}]
}
,
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
,
{
test: /\.scss$/,
use: [
// "style-loader",
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}
]
}
}
{
"scripts": {
"start": "webpack-dev-server --hot",
"build":"webpack",
"dev":"webpack-dev-server"
},
"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/preset-env": "^7.8.4",
"babel-loader": "^8.0.6",
"css-loader": "^3.4.2",
"fibers": "^4.0.2",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.9.0",
"node-sass": "^4.13.1",
"sass": "^1.25.0",
"sass-loader": "^8.0.2",
"style-loader": "^1.1.3",
"url-loader": "^3.0.0",
"webpack": "^4.41.6",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3",
"file-loader": "^5.1.0",
"url-loader": "^3.0.0"
}
}
热启动:npm run start