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. DOS批处理命令,自动获取本机系统及硬件配置信息

    可以配合域策略自动下发执行, 批量收集域内电脑配置; 手动执行亦可; 如下保存成.bat批处理文件执行即可. /*&cls&echo off&cd /d "%~dp0 ...

  2. 这些有用的CSS伪类通常被忽略

      这些有用的CSS伪类通常被忽略 这篇文章在一定程度上鼓励你在构建UI时使用更简单的CSS和更少的JavaScript. ::first-line 选择文本的第一行 这个选择器用于选取指定选择器的首 ...

  3. HttpClient常用的一些常识

    HttpClient是目前我们通讯组件中最常见的一个Api了吧.至少从我目前接触到与外部系统通讯的话是这样的.下面我将我自己常用的一些知识总结一下. 因为本猿也是边写边总结,有啥不对的还望多多指出. ...

  4. Quartus II 17.1新建一个流水灯

    诸图排序:从左到右,从上到下 一.软件设置 1.新建工程并添加FPGA芯片 2.新建.v文件并添加至顶层实体 3.元器件特性设置 4.分析与阐述(生成网表文件) 5.引脚分配 6.编译(包含分析与综合 ...

  5. python虚拟环境和包管理

    新建一个虚拟环境: python3 -m venv env1 # 新建一个名称为env1的虚拟环境 激活环境: source env1/bin/activate 退出虚拟环境: deactivate ...

  6. C++实现有序表--链表的合并操作代码

    #include<iostream>#include<cstdlib>using namespace std;#define MAXSIZE 100#define OK 1#d ...

  7. antd Vue--this.$confirm弹窗使用

    看API中this.$confirm弹窗使用不对 讲的也不明确,在此记录下正确的用法 this.$confirm("确认删除文件?",{ type:'error'}).then(( ...

  8. vue 打开页面触发事件

    vue中created(),mounted()与activated()区别及应用 created():在创建vue对象时,当html渲染之前就触发:但是注意,全局vue.js不强制刷新或者重启时只创建 ...

  9. 用find和xargs处理文件名中带空格的文件

    我们经常用find和xargs来进行批处理文件. 常见的用法如:find ./ -name ".jpg" | xargs cp -t ./JPEGImages ,实现将当前目前下所 ...

  10. mysql数据库常用指令

    1.查询当前连接数据库别名 select database(); Current database 项显示当前连接的数据库 2.查询数据表中当天的数据 select * from 表名 WHERE T ...