demo 代码点此,webpack4 进行 code splitting 使用 split-chunks-plugin, 开始前先做点准备工作。

start


安装:

npm i -D webpack webpack-cli
npm i -S lodash

创建 webpack.config.js 进行配置:

const path = require('path');

module.exports = {
mode: 'development',
entry: {
main: './index.js',
},
optimization: {
// code splitting settings
splitChunks: {
chunks: 'all',
cacheGroups: {
vendors: {
// 仅将 node_modules 下的代码打包进 vendors.js
test: /[\\/]node_modules[\\/]/,
priority: -10,
filename: 'vendors.js',
},
},
},
},
// 出口
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
}

创建 index.js :

// 引入 lodash
import _ from 'lodash'; console.log(_.chunk(['a', 'b', 'c', 'd'], 2));

打包终端执行 npx webpack进行打包,打开 dist 目录,可以看见 bundle.js 和 vendors.js,引入的 lodash 被打包到 vendors 中。

公共模块


如果 index.js 引入了公共模块,则可以将此模块进行打包。

修改配置:

// webpack.config.js

const path = require('path');

module.exports = {
mode: 'development',
entry: {
main: './index.js',
},
optimization: {
splitChunks: {
chunks: 'all',
// 代码文件大于 0kb 就进行打包
+ minSize: 0,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
filename: 'vendors.js',
},
+ default: {
+ // 公共模块仅引用 1 次也打包进 common.js
+ minChunks: 1,
+ priority: -20,
+ reuseExistingChunk: true,
+ filename: 'common.js',
+ }
}
}
},
// 出口
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
}

然后创建一个 math.js:

// math.js

export default function add (x, y) {
return x + y;
}

接着修改 index.js:

// inddex.js

import add  from './math';
console.log(add(1, 2));

执行npx webpack进行打包,打开 dist 目录,可以看见 math.js 被打包进 common.js 中了。

异步代码


打包异步代码需要使用 import(...)语法,所以需要配置一下 babel。

安装:

npm i -D babel-loader @babel/core babel-plugin-dynamic-import-webpack

配置一下 webpack.config.js:

const path = require('path');

module.exports = {
mode: 'development',
entry: {
main: './index.js',
},
module: {
rules: [{
test: /\.js/,
use: [{
loader: 'babel-loader',
options: {
"babelrc": false,
"plugins": [
"dynamic-import-webpack"
]
}
}]
}]
},
optimization: {
splitChunks: {
chunks: 'all',
minSize: 0,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
// filename: 'vendors.js',
},
default: {
minChunks: 1,
priority: -20,
reuseExistingChunk: true,
// filename: 'common.js',
}
}
}
},,
output: {...},
}

修改 index.js:

// index.js

async function getComponent() {
const { default: _ } = await import('lodash');
const element = document.createElement('div');
element.innerHTML = _.join(['hello', 'world'], '-');
return element;
} getComponent().then(element => {
document.body.appendChild(element);
})

执行打包,可以看见 import(...) 异步加载的 lodash 被打包成 0.bundle.js。

webpack4 code splitting的更多相关文章

  1. [Webpack 2] Maintain sane file sizes with webpack code splitting

    As a Single Page Application grows in size, the size of the payload can become a real problem for pe ...

  2. webpack优化之code splitting

    作为当前风头正盛的打包工具,webpack风靡前端界.确实作为引领了一个时代的打包工具,很多方面都带来了颠覆性的改进,让我们更加的感受到自动化的快感.不过最为大家诟病的一点就是用起来太难了. 要想愉快 ...

  3. webpack Code Splitting浅析

    Code Splitting是webpack的一个重要特性,他允许你将代码打包生成多个bundle.对多页应用来说,它是必须的,因为必须要配置多个入口生成多个bundle:对于单页应用来说,如果只打包 ...

  4. [转] react-router4 + webpack Code Splitting

    项目升级为react-router4后,就尝试着根据官方文档进行代码分割.https://reacttraining.com/react-router/web/guides/code-splittin ...

  5. react-router4 + webpack Code Splitting

    项目升级为react-router4后,就尝试着根据官方文档进行代码分割.https://reacttraining.com/react-router/web/guides/code-splittin ...

  6. webpack 利用Code Splitting 分批打包、按需下载

    webpack中的解决方案——Code Splitting,简单来说就是按需加载(下载),如果是requireJS对应的AMD的方案中这本是在正常不过了.但是在webpack中All in one的思 ...

  7. 借助Code Splitting 提升单页面应用性能

    近日的工作集中于一个单页面应用(Single-page application),在项目中尝试了闻名已久的Code splitting,收获极大,特此分享. Why we need code spli ...

  8. webpack 和 code splitting

    Code Splitting指的是代码分割,那么什么是代码分割,webpack和code splitting又有什么样的联系呢? 使用npm run dev:"webpack-dev-ser ...

  9. webpack async load modules & dynamic code splitting

    webpack async load modules & dynamic code splitting webpack 按需/异步加载/Code Splitting webpack loade ...

随机推荐

  1. 常用RGB颜色表 色值

    转自:http://blog.sina.com.cn/s/blog_7f422a8901019d8j.html   R G B 值   R G B 值   R G B 值 黑色 0 0 0 #0000 ...

  2. Dynamics CRM 2015/2016新特性之七:有了文档模板,打印分析So Easy

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复190或者20160216可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 从CRM 2015 UR1开始, ...

  3. form分辨率

    近期做项目时,遇到开发的winform在自己电脑上可以正常显示,共享到其他电脑就事儿不能显示了: [转载自:http://blog.csdn.net/lcawen88/article/details/ ...

  4. Python图像处理库Pillow常用使用方法

    PIL(Python Imaging Library)是Python一个强大方便的图像处理库,只支持到Python2.7.Pillow是PIL的一个派生分支,在Python3中用Pillow代替PIL ...

  5. [20191011]通过bash计算sql语句的sql_id.txt

    [20191011]通过bash计算sql语句的sql_id.txt --//当我知道如何通过bash计算sql语句的full_hash_value ,就很想通过bash编程计算sql_id.当时受限 ...

  6. flask接收跨域请求

    ajax发送数据类型为json即可 接受数据详见下文 https://www.cnblogs.com/anxminise/p/9814326.html

  7. [android]system.img文件的打包和解包

    1:system.img的两种格式 system2_0.img: Linux rev 1.0 ext4 filesystem data, UUID=57f8f4bc-abf4-655f-bf67-94 ...

  8. C++:基本类型的转换

    C++:基本类型的转换 一.string转为int [参考:https://blog.csdn.net/m0_37316917/article/details/82712017] string num ...

  9. 05justify-content

    display: flex; 的默认轴是x轴 justify-content: 设置主轴上的子元素排列的方式 所以在使用之前要确定好哪一个是主轴 /* justify-content:flex-sta ...

  10. 关于destoon6.0下的ngnix伪静态

    关于destoon6.0下的ngnix伪静态配置 ##rewrite nginx rewrite '(.*)\.(asp|aspx|asa|asax|dll|jsp|cgi|fcgi|pl)(.*)' ...