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抽离公共库,大幅缩减项目体积,及项目打包速度的更多相关文章

  1. 深入 Create React App 核心概念

    本文差点难产而死.因为总结的过程中,多次怀疑本文是对官方文档的直接翻译和简单诺列:同时官方文档很全面,全范围的介绍无疑加深了写作的心智负担.但在最终的梳理中,发现走出了一条与众不同的路,于是坚持分享出 ...

  2. tap news:week5 0.0 create react app

    参考https://blog.csdn.net/qtfying/article/details/78665664 先创建文件夹 安装create react app 这个脚手架(facebook官方提 ...

  3. Create React App

    Facebook开源了React前端框架(MIT Licence),也同时提供了React脚手架 - create-react-app. create-react-app遵循约定优于配置(Coc)的原 ...

  4. 使用create react app教程

    This project was bootstrapped with Create React App. Below you will find some information on how to ...

  5. 如何扩展 Create React App 的 Webpack 配置

    如何扩展 Create React App 的 Webpack 配置  原文地址https://zhaozhiming.github.io/blog/2018/01/08/create-react-a ...

  6. 在 .NET Core 5 中集成 Create React app

    翻译自 Camilo Reyes 2021年2月22日的文章 <Integrate Create React app with .NET Core 5> [1] Camilo Reyes ...

  7. [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 ...

  8. Create React App 安装less 报错

    执行npm run eject 暴露模块 安装 npm i  less less-loader -D 1.打开 react app 的 webpack.config.js const sassRege ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. svn 中如何checkout出单个文件

    A 通过命令行操作 1.检出目录images svn co --depth=empty http://www.iusesvn.com/project1/images images_work_dir 这 ...

  2. Ftp连接-200 Switching to ASCII mode,227 Entering Passive Mode

    测试 ftp 服务器是否部署成功,最简单的方法,就是找个 windows 系统直连服务器,能连上就说明服务部署成功了.不过,有时候即使 ftp 服务部署成功了,windows 系统依然连接不上服务,这 ...

  3. Docker学习笔记-03 容器数据卷

    1.宿主 vs容器直接映射数据容器卷 docker run -it  --privileged=true  -v  /宿主机绝对路径目录 :/ 容器内目录   镜像名 eg:  docker run  ...

  4. Jmeter - Config Tips

    Guideline: Jmeter.properties file should avoid to be modified , modified user.properties instead > ...

  5. C++ 函数类型和函数指针类型的自动推导、声明和赋值

    1.函数类型推导 #include <iostream> bool MyComp(int val1, int val2) { return val1 > val2; } int ma ...

  6. 特别好用的题库(oj)

    tk.hustoj.com 每次做题时,我都会对"外部导入"这四个字感到迷惑: 这些题,究竟是从哪里"导入"的? 我们不为而知...... 直到后来...... ...

  7. 为什么gdb通过0地址显示偏移会提示地址错误

    现象 在gdb中,如果想看一个struct的某个field的偏移量,和C语言一样可以通过对一个0地址变量取地址,然后取成员的偏移量获得.更神奇的地方在于和C语言一样,这里也不会触发内存访问异常. 另外 ...

  8. 《SAP MDM主数据管理》.pdf

    <SAP MDM主数据管理>.pdf 有需要的联系 wx :erpworld

  9. jni调用

    花了三天时间,终于把这个环境和实现都弄通了一遍. 先来个总纲: 准备工作:jdk环境,eclipse,gcc环境,cdt插件 a.编写带有native声明的java类, b.编译java类 c.用ja ...

  10. 记一个好用的轮播图的FlexSlider

    之前给自己公司的主页套用过一个js动态生成的轮播图,但是从载入的时机和载入后的效果都不太理想,又懒得去优化了,这次偶然遇到一个比较不错的轮播图的js插件,记录之. 首先它是给予jquery的,引进jq ...