moduleIds
& chunkIds
的优化tree shaking
polyfill
脚本被移除e6/es2015
的代码SplitChunk
和模块大小Module Federation
type: 'memory'
而且在 生产 模式 中被禁用cache
进行设置cache.type: "filesystem"
的时候,webpack会在内部启用文件缓存和内存缓存,写入的时候会同时写入内存和文件,读取缓存的时候会先读内存,如果内存里没有才会读取文件cnpm i webpack webpack-cli webpack-dev-server babel-loader @babel/core @babel/preset-env -D
const path = require('path');
module.exports = {
mode: 'development',
cache: {
type: 'filesystem', // 'memory' | 'filesystem'
cacheDirectory: path.resolve(__dirname, 'node_modules/.cache/webpack'), // 默认将缓存存储在 node_modules/.cache/webpack
},
watch: true,
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
"@babel/preset-env"
]
}
}
]
}
]
}
}
"scripts": {
"build": "webpack",
"debug": "webpack"
},
module.exports = {
module: {
rules: [
{
test: /\.png$/,
type: 'asset/resource'
},
{
test: /\.ico$/,
type: 'asset/inline'
},
{
test: /\.txt$/,
type: 'asset/source'
}
]
},
experiments: {
asset: true
},
};
src\index.js
import url from './images/kf.jpg';
let img = new Image();
img.src = url;
document.body.appendChild(img);
webpack.config.js
module: {
rules: [
{
test: /\.(jpg|png|gif)$/,
type:'asset'
}
]
}
src\index.js
let url = new URL('./images/kf.jpg', import.meta.url);
let img = new Image();
img.src = url;
document.body.appendChild(img);
module.rule
中被映射到加载器和模块类型src\index.js
import data from "data:text/javascript,export default 'title'";
import url from 'https://img.zhufengpeixun.com/zfjg.png';
console.log(data,url);
webpack.config.js
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
target: ['es6'],
plugins:[
new webpack.experiments.schemes.HttpsUriPlugin()
]
}
可选值 | 含义 | 示例 |
---|---|---|
false | 不应使用任何内置算法,插件提供自定义算法 | Path variable [name] not implemented in this context: [name].js |
natural | 按使用顺序的数字ID | 1 |
named | 方便调试的高可读性id | src_two_js.js |
deterministic | 根据模块名称生成简短的hash值 | 915 |
size | 根据模块大小生成的数字id | 0 |
const path = require('path');
module.exports = {
mode: 'development',
devtool:false,
+ optimization:{
+ moduleIds:'deterministic',
+ chunkIds:'size'
+ },
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
"@babel/preset-env"
]
}
}
]
}
]
}
}
webpack4
带了许多Node.js核心模块的polyfill
,一旦模块中使用了任何核心模块(如crypto),这些模块就会被自动启用webpack5
不再自动引入这些polyfill
cnpm i crypto-js crypto-browserify stream-browserify buffer -D
import CryptoJS from 'crypto-js';
console.log(CryptoJS.MD5('zhufeng').toString());
resolve:{
/* fallback:{
"crypto": require.resolve("crypto-browserify"),
"buffer": require.resolve("buffer"),
"stream":require.resolve("stream-browserify")
}, */
fallback:{
"crypto":false,
"buffer": false,
"stream":false
}
},
class
和 function
的作用域是可以导出到其他模块的// module scope start
// Block
{ // <- scope start
} // <- scope end
// Class
class Foo { // <- scope start
// |
} // <- scope end
// If else
if (true) { // <- scope start
} /* <- scope end */ else { // <- scope start
} // <- scope end
// For
for (;;) { // <- scope start
} // <- scope end
// Catch
try {
} catch (e) { // <- scope start
} // <- scope end
// Function
function() { // <- scope start
} // <- scope end
// Scope
switch() { // <- scope start
} // <- scope end
// module scope end
webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
mode: 'development',
optimization: {
usedExports: true,
},
};
"sideEffects": false
,意思就是对所有的模块都进行Tree Shaking
package.json
{"sideEffects": ["@babel/polyfill"]}
{"sideEffects": ["*.css"]}
src\index.js
import * as calculator from "./calculator";
console.log(calculator.operators.add);
src\calculator.js
import * as operators from "./operators";
export { operators };
src\operators.js
export const add = 'add';
export const minus = 'minus';
webpack.config.js
module.exports = {
mode: 'production'
}
src\index.js
import { getPostUrl } from './api';
console.log('getPostUrl',getPostUrl);
src\api.js
import { host } from './constants';
function useHost() {
return host;
}
export function getUserUrl() {
return useHost()+'/user';
}
export function getPostUrl() {
return '/post';
}
src\api.js
export const host = 'http://localhost';
CommonJS
导出和 require()
调用时的导出使用分析webpack 5 增加了对一些 CommonJS
构造的支持,允许消除未使用的 CommonJs 导出,并从 require() 调用中跟踪引用的导出名称
支持以下构造:
exports|this|module.exports.xxx = ...
src\index.js
let api = require('./api');
console.log(api.getPostUrl);
src\api.js
function getUserUrl() {
return '/user';
}
function getPostUrl() {
return '/post';
}
exports.getPostUrl=getPostUrl;