react打包優(yōu)化和本地預(yù)覽的實(shí)現(xiàn)
項(xiàng)目打包
打包指的是將項(xiàng)目中的源代碼和資源文件進(jìn)行處理,生成可在生產(chǎn)環(huán)境中運(yùn)行的靜態(tài)文件的過程
打包命令
npm run build

本地預(yù)覽
本地預(yù)覽是指在本地通過靜態(tài)服務(wù)器模擬生產(chǎn)服務(wù)器運(yùn)行項(xiàng)目的過程
安裝本地服務(wù)包
npm i -g serve
啟動(dòng)
serve -s build
瀏覽器訪問生成的本地地址 一般是3000端口
http://localhost:3000/

打包優(yōu)化
路由懶加載
什么是路由懶加載?
路由懶加載是指路由的JS資源只有在被訪問的時(shí)候才會(huì)動(dòng)態(tài)獲取,目的是為了優(yōu)化項(xiàng)目首次打開的時(shí)間
配置路由懶加載
- 把路由修改為由React提供的lazy函數(shù)進(jìn)行動(dòng)態(tài)導(dǎo)入
- 使用React內(nèi)置的Suspense組件包裹路由中element選項(xiàng)對(duì)應(yīng)的組件
lazy函數(shù)進(jìn)行動(dòng)態(tài)導(dǎo)入
// 格式
const 命名 = lazy(()=>import('路徑'))
const Month = lazy(()=>import('@/page/Month/index'))Suspense組件包裹路由組件
const router = createBrowserRouter([
{
path: "/",
element: <Suspense> <Layout/></Suspense>,
children: [
{
path: "/year",
element:<Suspense><Year/></Suspense>,
},
{
index:true,
element: <Suspense><Month/></Suspense> ,
},
],
}
])包體積分享
什么是包體積分析
通過可視化的方式,直觀的體現(xiàn)項(xiàng)目中各種包打包之后的體積大小方便做優(yōu)化
實(shí)現(xiàn)
- 安裝包 通過 source-map-explorer
- 配置命令指定要分析的js文件
安裝
npm i source-map-explorer
配置命令指定要分析的js文件
"scripts": {
"start": "craco start",
"build": "craco build",
"server": "json-server ./mock/home.json --port 3005",
"explorer": "source-map-explorer 'build/static/js/*.js'" //分析命令
},運(yùn)行命令
npm run explorer

運(yùn)行成功會(huì)自動(dòng)在瀏覽器打開分析圖
CDN優(yōu)化
什么是CDN?
CDN是一種內(nèi)容分發(fā)網(wǎng)絡(luò)服務(wù),當(dāng)用戶請(qǐng)求網(wǎng)站內(nèi)容時(shí),有離用戶最近的服務(wù)器將緩存資源內(nèi)容傳遞給用戶
哪些資源可以放到CDN服務(wù)器?
體積較大的非業(yè)務(wù)JS文件,比如react,react-dom
- 體積較大,需要利用CDN文件在瀏覽器的緩存特性,加快加載時(shí)間
- 非業(yè)務(wù)JS文件,不需要經(jīng)常做變動(dòng),CDN不用頻繁更新緩存
項(xiàng)目中怎么實(shí)現(xiàn) ?
- 把需要做CDN緩存的文件排除在打包之外(react,react-dom)
- 以CDN的方式重新引入資源(react,react-dom)
通過 craco 來修改 webpack 配置,從而實(shí)現(xiàn) CDN 優(yōu)化
craco.config.js 中:
const path = require('path')
module.exports = {
webpack:{
alias:{
'@':path.resolve(__dirname,'src')
},
configure: (webpackConfig) => {
let cdn = {
js: [],
css: []
}
// 對(duì)webpack進(jìn)行配置
whenProd(() => {
// 只會(huì)在生產(chǎn)環(huán)境執(zhí)行
webpackConfig.externals = {
react: 'React',
'react-dom': 'ReactDOM',
redux: 'Redux',
}
cdn = {
js: [
'https://cdn.bootcdn.net/ajax/libs/react/17.0.2/umd/react.production.min.js',
'https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js',
'https://cdn.bootcdn.net/ajax/libs/redux/4.1.0/redux.min.js'
],
css: []
}
})
const { isFound, match } = getPlugin(
webpackConfig,
pluginByName('HtmlWebpackPlugin')
)
if (isFound) {
// 找到了html的插件
match.options.cdn = cdn
}
return webpackConfig
}
}
}public/index.html 中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" rel="external nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" rel="external nofollow" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" rel="external nofollow" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!-- 動(dòng)態(tài)插入cdn資源 -->
<% htmlWebpackPlugin.options.cdn.css.forEach(cdnURL => { %>
<link rel="stylesheet" href="<%= cdnURL %>" rel="external nofollow" ></link>
<% }) %>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
到此這篇關(guān)于react打包優(yōu)化和本地預(yù)覽的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)react打包優(yōu)化和本地預(yù)覽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- react build 后打包發(fā)布總結(jié)
- 詳解React項(xiàng)目如何修改打包地址(編譯輸出文件地址)
- 記一次react前端項(xiàng)目打包優(yōu)化的方法
- 淺談React Native打包apk的坑
- 淺談React + Webpack 構(gòu)建打包優(yōu)化
- webpack打包react項(xiàng)目的實(shí)現(xiàn)方法
- 利用CDN加速react webpack打包后的文件詳解
- react native打包apk文件安裝好之后進(jìn)入應(yīng)用閃退的解決方案
- react項(xiàng)目打包后點(diǎn)擊index.html頁面出現(xiàn)空白的問題
- React項(xiàng)目build打包頁面空白的解決方案
相關(guān)文章
通過實(shí)例學(xué)習(xí)React中事件節(jié)流防抖
這篇文章主要介紹了通過實(shí)例學(xué)習(xí)React中事件節(jié)流防抖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06
解決React報(bào)錯(cuò)React?Hook?useEffect?has?a?missing?dependency
這篇文章主要為大家介紹了解決React報(bào)錯(cuò)React?Hook?useEffect?has?a?missing?dependency,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Reactjs?錯(cuò)誤邊界優(yōu)雅處理方法demo
這篇文章主要為大家介紹了Reactjs?錯(cuò)誤邊界優(yōu)雅處理方法demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
React Native使用Modal自定義分享界面的示例代碼
本篇文章主要介紹了React Native使用Modal自定義分享界面的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
詳解一個(gè)基于react+webpack的多頁面應(yīng)用配置
這篇文章主要介紹了詳解一個(gè)基于react+webpack的多頁面應(yīng)用配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01

