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. 代码格式 linux

    indent -kr -i8 -ts8 -sob -l80 -ss -bs -psl test.c

  2. 批量修改excel中超链接

    打开需要处理的excel文件 alt + F11打开VB编辑器 双击打开对应的sheet 编辑如下代码 Dim oldfile As String Dim Newfile As String Sub ...

  3. 在uniapp中,定义导航栏左侧,右侧按钮

    在page.json中 代码: { "path": "pages/pandian", "style": { "navigation ...

  4. java使用MVC编程模型实现1+到100图形界面

    MVC概念 MVC编程模型是可以说从提出到现在经久不败,是一种先进的设计结构.能很好的体现个人分工,从而实现前后端分离. M(Model):模型:存储数据的对象.后端操作数据库的. V(View):视 ...

  5. superset2.0.0 支持MaxCompute 时间颗粒

    编译的时候需要在superset/superset/db_engine_specs以下路径增加一个 odps.py的文件 # Licensed to the Apache Software Found ...

  6. noi 2.1基本算法之枚举

    7647:余数相同问题 1.描述 已知三个正整数 a,b,c. 现有一个大于1的整数x,将其作为除数分别除a,b,c,得到的余数相同. 请问满足上述条件的x的最小值是多少? 数据保证x有解. 2.输入 ...

  7. 常用的Shell实用脚本

    1.检测两台服务器指定目录下的文件的一致性 #!/bin/bash######################################检测两台服务器指定目录下的文件一致性########### ...

  8. JVM系列(四):GC策略

    一.概念 GC,Garbage Collection垃圾回收,主要针对JVM中的堆和方法区,而JVM栈.本地方法栈,程序计数器都是线程私有的,跟随线程生命周期. 二.对象存活判断 1. 引用计数:每个 ...

  9. CDO学习2 CDO 入门教程Tutorial

    #20210117#注:如果需要用cdo对数据进行截取,可参考buguse的几篇博文: 如何利用CDO从数据集中提取数据 CDO条件性选择数据 - 云+社区 - 腾讯云 CDO转换数据集格式 - 云+ ...

  10. WPF_MVVM框架(5)

    1.NuGet引用MVVM框架包 引入该框架包之后, 默认会在目录下创建ViewModel层的示例代码 2.第二步, 通过在MainViewModel中创建一些业务代码, 将其与MainWindow. ...