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 ...
随机推荐
- 浅析Winform的可视样式
每一个C#的Winform项目的Main方法里,都有这么一行代码,那么它究竟是用来做什么的呢? Application.EnableVisualStyles(); 从注释来看,这是一行用作设置样式的代 ...
- 事务与spring事务
事务 事务的特性(ACID) 原子性(Atomicity): 标识将事务中所有的操作进行捆绑层一个不可分割的单元格,计对事务所有进行的数据库修改等操作,要么全部执行,要么就是全部失败隔离性(Isola ...
- Unity 读取Json文件、创建Json文件
using System.IO; using UnityEngine; public class ReadJson:MonoBehaviour { public static TestSetting ...
- Mybatis开发之mapper代理实现自定义接口(常用)
Mybatis开发之mapper代理实现自定义接口(常用) 通过mapper代理实现自定义接口 自定义接口,接口里面定义定义相关的业务方法 编写方法相对应的Mapper.xml. 定义完接口后,Map ...
- APP与Web测试区别
相同点: WEB 测试和 App 测试从流程上来说,没有区别.都需要经历测试计划方 案,用例设计,测试执行,缺陷管理,测试报告等相关活动.从技术上来说, WEB 测试和 APP 测试其测试类型也基本相 ...
- Android组件化开发-----页面路由(ARouter)
平时开发中,我们经常用到页面跳转功能.之前我一直使用Intent过跳转 Intent intent = new Intent(A.this, B.class); intent.putExtra(&qu ...
- c#笔记(四)——switch
---恢复内容开始--- using UnityEngine; using System.Collections; public class Script1 : MonoBehaviour { ...
- OpenMP fortran 学习
参考自TAMU的PPThttps://people.math.umass.edu/~johnston/PHI_WG_2014/OpenMPSlides_tamu_sc.pdf 什么是OpenMP 在C ...
- Vulnhub 靶场 CORROSION: 2
Vulnhub 靶场 CORROSION: 2 前期准备 下载地址:https://www.vulnhub.com/entry/corrosion-2,745/ 靶机地址:192.168.147.19 ...
- COM 对象的利用与挖掘4
作者:Joey@天玄安全实验室 前言 本文在FireEye的研究Hunting COM Objects[1]的基础上,讲述COM对象在IE漏洞.shellcode和Office宏中的利用方式以及如何挖 ...