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. core文件段错误---对应内核处理

    do_page_fault __bad_area __bad_area_nosemaphore force_sig_info_fault

  2. 将本地文件推送到Gitee仓库

    完整的步骤 1.在gitee中创建仓库 2.下载.安装git 3.推送文件到gitee 推送文件到gitee 1.在要上传的文件夹空白处,点击右键,选择 git bash here 2.输入 git ...

  3. sql-log

    使用插件必须 先设置打印sql日志级别    debug 不然打不出来 SQL Params Setter插件 Ctrl+V mybaties log

  4. 小程序使用webview嵌套H5两边如何传参.

    需求:项目里面需要进行人脸核身.需要调起小程序的人脸核身功能.需要h5跳转到小程序页面.验证完后回退 1.h5页面先引入一个js文件 2.当用微信小程序的web-view内嵌H5页面的时候,H5页面的 ...

  5. lua-table类的继承

    --男人类man = {name = "man",age=123}--继承空间man.__index=man--儿童类child= {}--继承setmetatable(child ...

  6. windows11上面打开HEIC文件的有效方法

    今天在传资料的时候,用手机拍了一张照片传到window11,打开时竟然无法打开,顿时有点诧异,仔细看了文件类型(HEIC),可能软件不识别,怎么解决呢? 经过搜索整理尝试了许多方法,没找到一个合适的. ...

  7. 编译内核出现错误cc1: error: code model kernel does not support PIC mode

    删除该模块目录下的 .cache.mk 文件就好了,重新 make 即可

  8. mount无响应

    mount -t xfs /dev/sdb /data 挂载不成功,且命令无任何回显. dmesg 查看到有报错 tailf /var/log/messages -n 100 systemctl da ...

  9. wrf-python离线安装

    由于客户环境不能联网,python的插件库只能离线安装,wrf库的安装中踩了不少坑,特此记录. 1.官方插件库pypi.org只有压缩包,没有提供wheel,在线安装没有问题. 2.下载压缩包解压后, ...

  10. maven profile 的作用

    maven 的profile  可以是我们通过编译时指定 -P 来实现 使用不同的 属性变量. 比如: <profiles> <profile> <id>local ...