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
啟動
serve -s build
瀏覽器訪問生成的本地地址 一般是3000端口
http://localhost:3000/

打包優(yōu)化
路由懶加載
什么是路由懶加載?
路由懶加載是指路由的JS資源只有在被訪問的時(shí)候才會動態(tài)獲取,目的是為了優(yōu)化項(xiàng)目首次打開的時(shí)間
配置路由懶加載
- 把路由修改為由React提供的lazy函數(shù)進(jìn)行動態(tài)導(dǎo)入
- 使用React內(nèi)置的Suspense組件包裹路由中element選項(xiàng)對應(yīng)的組件
lazy函數(shù)進(jìn)行動態(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)行成功會自動在瀏覽器打開分析圖
CDN優(yōu)化
什么是CDN?
CDN是一種內(nèi)容分發(fā)網(wǎng)絡(luò)服務(wù),當(dāng)用戶請求網(wǎng)站內(nèi)容時(shí),有離用戶最近的服務(wù)器將緩存資源內(nèi)容傳遞給用戶
哪些資源可以放到CDN服務(wù)器?
體積較大的非業(yè)務(wù)JS文件,比如react,react-dom
- 體積較大,需要利用CDN文件在瀏覽器的緩存特性,加快加載時(shí)間
- 非業(yè)務(wù)JS文件,不需要經(jī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: []
}
// 對webpack進(jìn)行配置
whenProd(() => {
// 只會在生產(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>
<!-- 動態(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)容請搜索腳本之家以前的文章或繼續(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)文章
基于webpack4搭建的react項(xiàng)目框架的方法
本篇文章主要介紹了基于webpack4搭建的react項(xiàng)目框架的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
React替換傳統(tǒng)拷貝方法的Immutable使用
Immutable.js出自Facebook,是最流行的不可變數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn)之一。它實(shí)現(xiàn)了完全的持久化數(shù)據(jù)結(jié)構(gòu),使用結(jié)構(gòu)共享。所有的更新操作都會返回新的值,但是在內(nèi)部結(jié)構(gòu)是共享的,來減少內(nèi)存占用2023-02-02
React.js入門實(shí)例教程之創(chuàng)建hello world 的5種方式
React 是近期非常熱門的一個(gè)前端開發(fā)框架。應(yīng)用非常廣泛,接下來通過本文給大家介紹React.js入門實(shí)例教程之創(chuàng)建hello world 的5種方式 ,需要的朋友參考下吧2016-05-05
react.js CMS 刪除功能的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猺eact.js CMS 刪除功能的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04

