webpack4.0高级
环境变量
webpack --env.NODE_ENV=local --env.production --progress
Tree Shaking
移除JS上下文字未被引用的代码
只支持ES6的import和export
optimization: {
usedExports: true
}
package.json
"sideEffects": ["*.css", "*.scss"],
development and production
webpack-merge
构建开发环境和生成环境
开发环境需要实时重新加载,热模块替换能力的source map和localhost server
生成环境需要更小的bundle, 更轻量级的source map
Code Splitting
首先介绍第一种代码分割
lodash
entry: {
main: resolve(__dirname, '../src/index.js'),
lodash: resolve(__dirname, '../src/lodash.js')
}
创建lodah.js
import _ from 'lodash' window._ = _
webpack的代码分割
optimization:{
splitChunks:{
chunks: 'all'
}
}
同步代码:只需要在webpack.common.js中配置optimization
异步代码:通过import,无需任何配置即可,会自动进行代码分割
async function getLodash() {
const { default: _ } = await import(/* webpackChunkName: 'lodash' */ 'lodash')
const ele = document.createElement('div')
ele.innerHTML = _.join(['Hi', 'Susan'], '*')
return ele
} getLodash().then( res => {
document.body.appendChild(res)
})
webpack --profile --json > stats.json
filename and chunkFileName
filename
对应entry里面生成的文件名字
chunFileName
chunkFileName未被列在entry中,如异步加载(import)
MiniCssExtractPlugin
mini-css-extract-plugin
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
plugins: [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: '[name].css',
chunkFilename: '[id].css',
}),
],
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it uses publicPath in webpackOptions.output
publicPath: '../',
hmr: process.env.NODE_ENV === 'development',
},
},
'css-loader',
],
},
],
},
};
development中热更新
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const devMode = process.env.NODE_ENV !== 'production'; module.exports = {
plugins: [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: devMode ? '[name].css' : '[name].[hash].css',
chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
}),
],
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: process.env.NODE_ENV === 'development',
},
},
'css-loader',
'postcss-loader',
'sass-loader',
],
},
],
},
};
production中css压缩
const TerserJSPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
module.exports = {
optimization: {
minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css',
}),
],
module: {
rules: [
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
],
},
};
缓存
格式
[<hashType>:contenthash:<digestType>:<length>]
Shimming
一直垫片形式,项目中使用lodash,我们可以不需要引入lodash,webpack自动完成
new webpack.ProvidePlugin({
_: 'lodash'
})
index.js代码,没引入lodash
const dom = document.createElement('div')
dom.innerHTML = _.join(['Hi', 'Susan'], ' ')
document.body.appendChild(dom)
imports-loader
模块中的this指向是一个{},可以是用imports-loader指定this->window
use: 'imports-loader?this=>window'
TypeScript
ts-loader / typescript
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader'
}
]
}
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist",
"module": "es6",
"target": "es5",
"allowJs": true
}
}
index.tsx
import * as _ from 'lodash' class Greeter {
greeting: string
constructor(message: string) {
this.greeting = message
}
greet() {
console.log(_.join([this.greeting, 'Go'], '_'))
}
} const greeter = new Greeter('Hi Susan') greeter.greet()
historyApiFallback
proxy
secure
resolve
alias
module.exports = {
//...
resolve: {
alias: {
Utilities: path.resolve(__dirname, 'src/utilities/'),
Templates: path.resolve(__dirname, 'src/templates/')
}
}
};
extensions
module.exports = {
//...
resolve: {
extensions: ['.wasm', '.mjs', '.js', '.json']
}
};
webpack4.0高级的更多相关文章
- webpack4.0介绍与使用(一)
1:webpack的基本使用: ##在网页中会引用那些静态资源: js, css, images, 字体文件和模板文件(.vue)等 ##网页总引用静态资源多了以后会有那些问题: 网页加载速度慢,因为 ...
- Yii2.0高级框架数据库增删改查的一些操作(转)
yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...
- Yii2.0高级框架数据库增删改查的一些操作
yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...
- C# 6 与 .NET Core 1.0 高级编程 - 37 章 ADO.NET
译文,个人原创,转载请注明出处,有不对的地方欢迎指出与交流. 英文原文:Professional C# 6 and .NET Core 1.0 - 37 ADO.NET --------------- ...
- C# 6 与 .NET Core 1.0 高级编程 - 38 章 实体框架核心(上)
译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 38 章 实体框架核心(上)),不对的地方欢迎指出与交流. 章节出自<Professional C# 6 ...
- C# 6 与 .NET Core 1.0 高级编程 - 38 章 实体框架核心(下)
译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 38 章 实体框架核心(下)),不对的地方欢迎指出与交流. 章节出自<Professional C# 6 ...
- C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(上)
译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(上)),不对的地方欢迎指出与交流. 章节出自<Professional C ...
- C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(下)
译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 39 章 Windows 服务(下)),不对的地方欢迎指出与交流. 章节出自<Professional C ...
- C# 6 与 .NET Core 1.0 高级编程 - 40 ASP.NET Core(上)
译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 40 章 ASP.NET Core(上)),不对的地方欢迎指出与交流. 章节出自<Professiona ...
随机推荐
- Android Studio在Ubuntu下离线安装Gradle
更新android studio3.0后又要升级gradle了,估计又要很长时间,晚上临走前跟开始更新下载,第二天一早发现又卡了,吐血. 在某CSDN下载gradle-4.1-all.zip,直接手动 ...
- 解决虚拟机克隆的linux系统ip无法正常使用问题
当我们克隆centos虚拟机无法正常获取IP地址,重启网卡也提示Bringing up interface eth0: Device eth0 does not seem to be present ...
- 【LeetCode】Heap
[215] Kth Largest Element in an Array [Medium] 给个无序数组,返回第K大的数字. 方法1. 直接使用优先队列 priority_queue class S ...
- leetcode-12双周赛-1246-删除回文子数组
题目描述: 方法:区间dp O(N^3) class Solution: def minimumMoves(self, A: List[int]) -> int: N = len(A) dp = ...
- delphi 打印 PDevMode 说明
//PDevMode = _devicemodeW;// _devicemodeW = record// dmDeviceName: array[0..CCHDEVICENAME - 1] of Wi ...
- contest-20191021
文化课读的真不开心 回来竞赛 假人 sol 根据不等式有 abs(a-b)+abs(b-c)>=abs(a-c) 那么每一个都会选. 可以发现每一段只会选在端点上(否则移到端点更优). 那么dp ...
- unittest框架学习笔记四之report
# coding=utf-8'''created:2018/3/29 author:star project:test report'''# import time,os# from selenium ...
- 如何在Python中让两个print()函数的输出打印在一行内?
1.两个连续的print()函数为什么在输出时内容会分行显示? 解:print()中有两个默认参数sep和end,其中sep是代替分隔符,end是代替末尾的换行符,默认使用‘,’代替空格,且默认末尾加 ...
- maven学习整理-基础知识
1.maven认识 maven是一种自动化的构建工具,它主要解决的问题有: ①项目中的划分规则:原先我们用package或文件夹的形式来划分不同模块,导致在一个项目中存在大量的文件夹和包代码显得庞大: ...
- git分布式版本控制系统权威指南学习笔记(五):git checkout
文章目录 分离头指针 通过cat可以查看当前的分支 通过branch查看当前分支 checkout commitId(真正的