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 ...
随机推荐
- Leecode剑指 Offer 07. 重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字. 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inor ...
- 升级安装cuda
下载驱动: https://www.nvidia.com/Download/Find.aspx 在这里下载并按照指导安装: https://developer.nvidia.com/cuda-down ...
- Spring Boot 配置 Swagger(3.0.0 版本)
添加 Swagger 依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter --> ...
- saml login的流程
用户会访问首页/, 然后进入到指定的一个URL, 就是admin在site-settings里面的设置的那个地址, 发现权限不够,进入到403accesslogin, 然后调用user4032logi ...
- BIP去掉弹框中的参照的新增按钮
viewModel.get("material_class_name").on("afterInitVm", function (arg) { ...
- guide, manual, tutorial之间的区别
总结:一般而言,guide表示用户指南,提供简要的信息:manual表示用户手册,提供较为完整深入的说明:tutorial表示教程,提供详细的说明,侧重于给没有经验的人一步一步进行详细的指导.实际情况 ...
- 《基于CNN和SVM的人脸识别系统的设计与实现》论文笔记十六
一.基本信息 标题:基于CNN和SVM的人脸识别系统的设计与实现 时间:2021 来源:计算机与数字工程 关键词: 人脸识别;卷积神经网络;支持向量机;深度学习; 二.研究内容 问题定义: 针对人脸识 ...
- Finance财务软件(支持Excel模板打印专题)
我们可以修改模板文件./service/PrintTemplate/凭证打印模板_v1.xlsx 模板中的字段对应 2010_upgrade_01.sql 中的存储过程sp_voucher_print ...
- STM32F4 HAL库中是如何实现UART IO配置的?
1.配置串口IO.中断等底层的东西需要在用户文件中重写HAL_UART_MspInit函数2.hal库是在stm32f4xx_hal_msp.c文件中重写的HAL_UART_MspInit函数,分析如 ...
- background-repeat属性(背景平铺)
background-repeat属性(背景平铺) 默认情况下,如果一幅背景图像不足以占满整个容器时,就会在水平方向和垂直方向重复,以填满整个容器.然而,有时候却希望背景图像只出现一次,或只在某个方向 ...