身為 React 開發人員,我們一直在尋找改善開發體驗和應用程式效能的方法。您可能會考慮的一項重大升級是從 Create React App (CRA) 遷移到 Vite。 Vite 提供更快的建造時間、更快的熱模組更換 (HMR) 和更簡化的開發體驗。在這份綜合指南中,我們將逐步介紹將 CRA 專案遷移到 Vite 的過程,包括處理代理伺服器和啟用 Gzip 壓縮。
在我們深入了解遷移過程之前,讓我們先簡單討論一下您可能想要從 CRA 切換到 Vite 的原因:
開始遷移過程之前,請確保您已:
首先,我們新建一個Vite專案:
npm create vite@latest my-vite-app -- --template react cd my-vite-app
此指令使用 React 範本建立一個新的 Vite 專案。我們將使用它作為遷移的基礎。
現在,讓我們將現有的原始碼從 CRA 專案移至新的 Vite 專案:
我們需要更新 package.json 檔案以包含 CRA 專案中的所有依賴項:
"scripts": { "dev": "vite", "build": "vite build", "serve": "vite preview", "test": "vitest", "lint": "eslint src --ext js,jsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview" }
npm install
在專案根目錄建立一個 vite.config.js 檔案:
import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' export default defineConfig({ plugins: [react()], resolve: { alias: { '@': '/src', }, }, server: { port: 3000, }, })
此組態設定 React 插件,定義 src 目錄的別名,並將開發伺服器連接埠設為 3000(與 CRA 的預設值相符)。
Vite 使用 ES 模組,因此您可能需要更新一些導入語句:
Vite 處理環境變數的方式與 CRA 不同:
如果您將 Jest 與 CRA 一起使用,則需要切換到 Vitest,它與 Vite 整合得更好:
npm install -D vitest jsdom @testing-library/react @testing-library/jest-dom
import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' export default defineConfig({ plugins: [react()], test: { globals: true, environment: 'jsdom', setupFiles: './src/setupTests.js', }, })
如果您的 CRA 專案使用了代理配置,您需要在 Vite 中進行設定:
npm install -D http-proxy
import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' import httpProxy from 'http-proxy' const proxy = httpProxy.createProxyServer() export default defineConfig({ plugins: [react()], server: { proxy: { '/api': { target: 'http://localhost:5000', changeOrigin: true, configure: (proxy, options) => { proxy.on('error', (err, req, res) => { console.log('proxy error', err) }) proxy.on('proxyReq', (proxyReq, req, res) => { console.log('Sending Request to the Target:', req.method, req.url) }) proxy.on('proxyRes', (proxyRes, req, res) => { console.log('Received Response from the Target:', proxyRes.statusCode, req.url) }) }, }, }, }, })
This configuration sets up a proxy for /api requests to http://localhost:5000. Adjust the target URL as needed for your backend.
To enable Gzip compression in Vite:
npm install -D vite-plugin-compression
import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' import viteCompression from 'vite-plugin-compression' export default defineConfig({ plugins: [ react(), viteCompression({ verbose: true, disable: false, threshold: 10240, algorithm: 'gzip', ext: '.gz', }), ], // ... other configurations })
This setup will generate Gzip-compressed versions of your assets during the build process.
Remove any CRA-specific files and folders:
Update your README.md to reflect the new Vite setup and commands.
Update your CI/CD pipelines to use the new Vite commands (npm run dev, npm run build, etc.).
Migrating from Create React App to Vite can significantly improve your development experience and application performance. While the process involves several steps, the benefits of faster build times, quicker HMR, and more flexible configuration make it a worthwhile endeavor.
Remember to thoroughly test your application after migration to ensure everything works as expected. You may need to make additional adjustments based on your specific project structure and dependencies.
Have you successfully migrated your project from CRA to Vite? Share your experiences and any additional tips in the comments below!
Happy coding!
以上是從 Create React App 遷移到 Vite:逐步指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!