create-react-app react 使用dll抽离公共库,大幅缩减项目体积,及项目打包速度
1、安装依赖(clean-webpack-plugin、add-asset-html-webpack-plugin、webpack-cli)
yarn add clean-webpack-plugin add-asset-html-webpack-plugin webpack-cli -D
2、项目根目录新建 webpack.dll.config.js,内容如下
const path = require('path')
const webpack = require('webpack')
const {
CleanWebpackPlugin
} = require('clean-webpack-plugin')
// dll文件抽取的目录
const dllPath = 'public/vendor'
module.exports = {
entry: {
// 需要提取的库文件
vendor: ['react','qs','react-helmet','echarts','echarts-for-react','antd','ahooks','aws-sdk','react-dom','react-redux','redux','react-router-dom','react-router-config','react-router','redux-thunk','redux-devtools-extension','axios','less','less-loader','xlsx','crypto-js','dayjs'] //依赖根据自己的package.json来编写,这是我自己项目中的
},
output: {
path: path.join(__dirname, dllPath),
filename: '[name].dll.js',
// 与 webpack.DllPlugin 中的名称一样
library: '[name]_[hash]'
},
plugins: [
new CleanWebpackPlugin(),
new webpack.DllPlugin({
path: path.join(__dirname, dllPath, '[name]-manifest.json'),
// 与 output.library的名称一样
name: '[name]_[hash]',
context: process.cwd(),
})
]
}
3、package.json 中 scripts 括号内,新增一行
"dll": "webpack --progress --config ./webpack.dll.config.js",
4、在config-overrides.js 中配置(config-overrides可以在网上找教程)
const {
override,
} = require("customize-cra");
const webpack = require("webpack");
const AddAssetHtmlPlugin = require("add-asset-html-webpack-plugin");
const path = require("path");
const addDll = () => (config) => {
if (config.mode === "production") {
config.devtool = false; //去掉map文件
config.plugins.push(
new webpack.DllReferencePlugin({
context: process.cwd(),
manifest: require("./public/vendor/vendor-manifest.json"),
}),
//dll 添加到生成的html文件中
new AddAssetHtmlPlugin({
// dll文件位置
filepath: path.resolve(__dirname, "./public/vendor/*.js"),
// dll 引用路径,不能用./,否则刷新会报错
publicPath: "/vendor",
// dll输出的目录
outputPath: "./vendor",
})
);
}
return config;
};
module.exports = override(
addDll
)
create-react-app react 使用dll抽离公共库,大幅缩减项目体积,及项目打包速度的更多相关文章
- 深入 Create React App 核心概念
本文差点难产而死.因为总结的过程中,多次怀疑本文是对官方文档的直接翻译和简单诺列:同时官方文档很全面,全范围的介绍无疑加深了写作的心智负担.但在最终的梳理中,发现走出了一条与众不同的路,于是坚持分享出 ...
- tap news:week5 0.0 create react app
参考https://blog.csdn.net/qtfying/article/details/78665664 先创建文件夹 安装create react app 这个脚手架(facebook官方提 ...
- Create React App
Facebook开源了React前端框架(MIT Licence),也同时提供了React脚手架 - create-react-app. create-react-app遵循约定优于配置(Coc)的原 ...
- 使用create react app教程
This project was bootstrapped with Create React App. Below you will find some information on how to ...
- 如何扩展 Create React App 的 Webpack 配置
如何扩展 Create React App 的 Webpack 配置 原文地址https://zhaozhiming.github.io/blog/2018/01/08/create-react-a ...
- 在 .NET Core 5 中集成 Create React app
翻译自 Camilo Reyes 2021年2月22日的文章 <Integrate Create React app with .NET Core 5> [1] Camilo Reyes ...
- [React] Create & Deploy a Universal React App using Zeit Next
In this lesson, we'll use next to create a universal React application with no configuration. We'll ...
- Create React App 安装less 报错
执行npm run eject 暴露模块 安装 npm i less less-loader -D 1.打开 react app 的 webpack.config.js const sassRege ...
- [React] Create and import React components with Markdown using MDXC
In this lesson I demonstrate how to use the library MDXC to create and import React components with ...
- [PReact] Reduce the Size of a React App in Two Lines with preact-compat
Not every app is greenfield, and it would be a shame if existing React apps could not benefit from t ...
随机推荐
- (python)正则表达式
# -*- coding: utf-8 -*- import re def test_dot(): """ . => 表示匹配任意字符 ""&q ...
- 一周ppt 总结
最近写了一篇培训ppt ,大概花了7个工作日,走了一些弯路,问题总结: 开始一项工作前,对接清除核心要点(刚开网上搜罗一圈 拼凑了一份(将各个内容进行筛选整理) 反馈后不是领导想要的) 制作ppt前, ...
- Webrtc audio
整体理解 在 WebRTC 中,Call 是peer connection 的. 为 WebRTC Call 注入的 AudioState 来自于全局的 MediaEngine 的 VoiceEngi ...
- nohup文件的压缩分割
编写sh脚本 先拷贝,之后,清空. 待完成,压缩功能 #!/bin/sh #description split logs time1=$(date -d 'yesterday' "+%Y%m ...
- php echo print
echo print都是语言结构,都不是函数,但echo没有返回值,print有. echo print
- mysql 获取某个时间段内每天的数据
SELECT dayTb.cday '日期' ,IFNULL(tNumTb.num,0) '订单数' FROM ( SELECT @cdate := DATE_ADD(@cdate, INTERVAL ...
- file类型的input框获取文件
- SQL Server【提高】碎片
碎片 当对索引所在的基础数据表进行增删改时,若存储的数据进行了不适当的跨页(SQL Server中存储的最小单位是页,页是不可再分的),就会导致索引碎片的产生. 外部碎片 插入的数据使页与页之间造成断 ...
- b站
题目描述 n条鱼,每条鱼的体积是ai 每一轮,每一条鱼一定会吃掉右边比自己小的第一条鱼,一条鱼只能被吃一次. 多少轮后,鱼的数量会稳定. 例子: 6 6 3 3 --> 6 6 3(第二个3)- ...
- 增、改生产订单组件BAPI BAPI_ALM_ORDER_MAINTAIN
转载留存 IT_METHODS LIKE BAPI_ALM_ORDER_METHOD处理方法,必选项,存储CREATE CREATETONOTIF CHANGE DELETE RELEAS ...