《前后端面試題
》專欄集合了前后端各個知識模塊的面試題,包括html,javascript,css,vue,react,java,Openlayers,leaflet,cesium,mapboxGL,threejs,nodejs,mangoDB,MySQL,Linux… 。
文章目錄
- 一、本文面試題目錄
- 1. 什么是Vite?它和其他前端構建工具的區別是什么?
- 2. Vite的主要特性有哪些?
- 3. Vite的劣勢是什么?
- 4. Vite和Webpack的區別是什么?
- 5. Vite和Rollup的關系是怎樣的?
- 6. Vite的開發服務器是如何工作的?
- 7. Vite的生產環境構建是如何工作的?
- 8. Vite如何利用瀏覽器原生ES模塊?
- 9. Vite的依賴預構建是如何實現的?
- 10. Vite中如何配置別名(alias)?
- 11. Vite中如何配置環境變量?
- 12. Vite中.env文件的作用是什么?
- 13. Vite中如何使用不同的模式(mode)?
- 14. Vite中如何處理CSS?
- 15. Vite中如何使用CSS預處理器(Sass, Less, Stylus)?
- 16. Vite中如何使用CSS Modules?
- 17. Vite中如何使用PostCSS?
- 18. Vite中如何處理靜態資源?
- 19. Vite中如何配置構建輸出路徑?
- 20. Vite中如何進行代碼分割?
- 21. Vite中如何配置多入口?
- 22. Vite的構建優化有哪些方法?
- 23. Vite的插件機制是如何工作的?
- 24. Vite中如何編寫一個插件?
- 25. Vite中常用的插件有哪些?
- 26. Vite中如何使用TypeScript?
- 27. Vite中如何處理Vue單文件組件?
- 28. Vite中如何處理React組件?
- 29. Vite中如何使用JSX/TSX?
- 30. Vite中如何進行單元測試?
- 二、120道面試題目錄列表
一、本文面試題目錄
1. 什么是Vite?它和其他前端構建工具的區別是什么?
Vite是一個由尤雨溪開發的前端構建工具,旨在提供更快的開發體驗。與傳統構建工具(如Webpack)不同,Vite基于原生ES模塊(ES Modules),在開發環境中無需打包即可直接運行代碼,大大提高了冷啟動和熱更新速度。
主要區別:
- 冷啟動速度:Vite無需等待整個應用打包,啟動時間接近瞬時;而Webpack需要先構建整個應用。
- 模塊處理方式:Vite在開發時利用瀏覽器原生ES模塊支持,按需加載;Webpack通過打包將所有模塊合并為一個或多個bundle。
- 熱更新(HMR):Vite的HMR速度更快,且通常能保持組件狀態;Webpack的HMR可能需要重新編譯部分代碼。
2. Vite的主要特性有哪些?
- 極速冷啟動:基于瀏覽器原生ES模塊,無需打包直接啟動。
- 即時熱更新(HMR):模塊熱替換速度極快,且不丟失組件狀態。
- 智能依賴預構建:使用esbuild預構建依賴,提高加載速度。
- 按需編譯:僅在瀏覽器請求時編譯模塊,而非一次性處理所有代碼。
- 內置對多種格式的支持:如TypeScript、JSX、CSS預處理器等。
- 優化的生產構建:使用Rollup生成優化后的靜態資源。
- 插件系統:可擴展的插件接口,支持各種功能擴展。
3. Vite的劣勢是什么?
- 生態系統成熟度:相比Webpack,Vite的插件生態可能不夠完善,某些特定場景的解決方案可能較少。
- 復雜配置支持:對于非常復雜的項目結構或特殊需求,Vite的配置可能不如Webpack靈活。
- 生產環境構建:雖然Vite在開發環境表現出色,但生產環境仍依賴Rollup,對于某些極端復雜的打包需求,可能不如Webpack全面。
- 瀏覽器兼容性:由于依賴原生ES模塊,Vite在舊版瀏覽器中的支持較差,需要額外配置。
4. Vite和Webpack的區別是什么?
特性 | Vite | Webpack |
---|---|---|
構建方式 | 開發時基于ES模塊,按需加載 | 先打包所有模塊再運行 |
冷啟動時間 | 接近瞬時 | 隨項目大小增長 |
熱更新速度 | 極快,通常保持組件狀態 | 較慢,可能需要重新編譯 |
依賴處理 | 使用esbuild預構建依賴 | 使用loader和plugin處理依賴 |
生產環境打包 | 使用Rollup | 直接使用Webpack |
配置復雜度 | 相對簡單 | 復雜,需要大量配置 |
生態系統 | 快速發展,但不如Webpack成熟 | 非常成熟,幾乎支持所有場景 |
5. Vite和Rollup的關系是怎樣的?
Vite在生產環境使用Rollup作為打包工具,而在開發環境則使用基于ES模塊的輕量級服務器。具體關系:
- 開發環境:Vite不使用Rollup,而是直接利用瀏覽器原生ES模塊支持,通過中間件處理模塊導入和轉換。
- 生產環境:Vite使用Rollup進行打包,默認配置已優化,但可通過
build.rollupOptions
自定義。 - 插件兼容性:大多數Rollup插件可直接在Vite中使用,通過
vite-plugin-rollup
適配器。
6. Vite的開發服務器是如何工作的?
Vite開發服務器的工作流程:
- 啟動服務器:基于原生ES模塊,無需打包。
- 依賴預構建:使用esbuild預構建第三方依賴,將CommonJS/UMD轉換為ES模塊。
- 模塊請求處理:攔截瀏覽器的模塊請求,動態轉換代碼(如TypeScript、JSX)。
- 模塊解析:處理裸模塊導入(如
import 'vue'
),轉換為正確的路徑。 - HMR支持:實現模塊熱更新,通過WebSocket與瀏覽器通信。
- 靜態資源處理:直接提供靜態資源,支持按需編譯。
7. Vite的生產環境構建是如何工作的?
Vite在生產環境使用Rollup進行構建:
- 配置合并:合并Vite默認配置和用戶自定義的
build.rollupOptions
。 - 代碼轉換:使用各種插件處理代碼(如TypeScript、JSX轉換)。
- 依賴處理:將依賴和應用代碼分離,生成優化的chunk。
- 代碼分割:基于動態導入或路由進行代碼分割,提高加載性能。
- 資源處理:處理靜態資源(圖片、字體等),生成哈希文件名。
- 壓縮和優化:壓縮JavaScript、CSS代碼,生成sourcemap。
- 輸出文件:將優化后的資源輸出到指定目錄。
8. Vite如何利用瀏覽器原生ES模塊?
Vite通過以下方式利用原生ES模塊:
- 直接提供ES模塊:開發服務器直接返回符合ES模塊規范的JavaScript文件。
- 處理裸模塊導入:將
import 'vue'
轉換為import '/node_modules/vue/dist/vue.esm-bundler.js'
。 - 動態模塊轉換:在瀏覽器請求時,動態轉換非JavaScript模塊(如TypeScript、CSS)為ES模塊。
- 按需加載:僅在瀏覽器請求時加載模塊,無需預打包整個應用。
示例:
// 源代碼
import { createApp } from 'vue';
import App from './App.vue';// Vite處理后,瀏覽器實際請求的代碼
import { createApp } from '/node_modules/vue/dist/vue.esm-bundler.js';
import App from '/src/App.vue?vue&type=script&lang=js';
9. Vite的依賴預構建是如何實現的?
Vite使用esbuild預構建依賴的流程:
- 掃描導入:分析項目入口文件,提取所有依賴(裸模塊導入)。
- esbuild轉換:使用esbuild將CommonJS/UMD模塊轉換為ES模塊。
- 緩存結果:將預構建的依賴緩存到
node_modules/.vite
目錄。 - 優化加載:預構建后的依賴作為單個模塊提供,減少瀏覽器請求。
配置示例(vite.config.js
):
export default {optimizeDeps: {include: ['lodash-es', 'axios'], // 指定需要預構建的依賴exclude: ['some-lib'], // 排除某些依賴不進行預構建},
};
10. Vite中如何配置別名(alias)?
在vite.config.js
中使用resolve.alias
配置別名:
import { defineConfig } from 'vite';export default defineConfig({resolve: {alias: {'@': '/src', // 將@映射到src目錄'components': '/src/components', // 自定義別名},},
});
使用示例:
// 原路徑
import MyComponent from '../../components/MyComponent.vue';// 使用別名后
import MyComponent from '@/components/MyComponent.vue';
No. | 大劍師精品GIS教程推薦 |
---|---|
0 | 地圖渲染基礎- 【WebGL 教程】 - 【Canvas 教程】 - 【SVG 教程】 |
1 | Openlayers 【入門教程】 - 【源代碼+示例 300+】 |
2 | Leaflet 【入門教程】 - 【源代碼+圖文示例 150+】 |
3 | MapboxGL 【入門教程】 - 【源代碼+圖文示例150+】 |
4 | Cesium 【入門教程】 - 【源代碼+綜合教程 200+】 |
5 | threejs 【中文API】 - 【源代碼+圖文示例200+】 |
6 | Shader 編程 【圖文示例 100+】 |
11. Vite中如何配置環境變量?
Vite通過.env
文件和import.meta.env
訪問環境變量:
- 創建環境文件:在項目根目錄創建
.env
(通用)、.env.development
(開發環境)、.env.production
(生產環境)。 - 定義變量:變量名必須以
VITE_
開頭。VITE_API_URL=https://api.example.com VITE_APP_TITLE=My App
- 訪問變量:在代碼中使用
import.meta.env.VITE_API_URL
。
12. Vite中.env文件的作用是什么?
.env
文件用于存儲環境變量,支持以下特性:
- 環境特定配置:通過
.env.development
、.env.production
等文件為不同環境定義變量。 - 變量前綴:只有以
VITE_
開頭的變量會被暴露給客戶端代碼。 - 優先級:環境特定文件 > 通用文件,命令行變量 > 文件變量。
示例:
# .env.development
VITE_API_URL=http://localhost:3000/api# .env.production
VITE_API_URL=https://api.production.com
13. Vite中如何使用不同的模式(mode)?
Vite通過mode
參數區分不同環境:
- 內置模式:
development
(開發)和production
(生產)。 - 自定義模式:可以定義其他模式(如
test
、staging
)。 - 指定模式:通過命令行參數或配置文件指定。
命令行示例:
# 使用development模式(默認)
npm run dev# 使用production模式
npm run build# 使用自定義模式
vite build --mode staging
配置文件示例(vite.config.js
):
export default defineConfig(({ mode }) => {if (mode === 'staging') {return {base: '/staging/',// 其他配置};}return {// 默認配置};
});
14. Vite中如何處理CSS?
Vite內置對CSS的支持,包括:
- 直接導入:在JavaScript中導入CSS文件。
import './style.css';
- CSS Modules:通過
.module.css
文件名啟用。import styles from './style.module.css'; document.body.classList.add(styles.container);
- PostCSS:自動應用項目中的PostCSS配置(如
postcss.config.js
)。 - CSS預處理器:支持Sass、Less、Stylus等,需安裝相應依賴。
- CSS代碼分割:根據入口點和動態導入自動分割CSS。
15. Vite中如何使用CSS預處理器(Sass, Less, Stylus)?
-
安裝依賴:
# Sass npm install sass -D# Less npm install less -D# Stylus npm install stylus -D
-
直接使用:導入相應擴展名的文件(
.scss
、.less
、.styl
)。import './style.scss';
-
全局變量/混合:使用
css.preprocessorOptions
配置。export default defineConfig({css: {preprocessorOptions: {scss: {additionalData: `@import "@/styles/variables.scss";`}}} });
16. Vite中如何使用CSS Modules?
-
命名約定:使用
.module.css
、.module.scss
等擴展名。 -
導入和使用:
// style.module.css .container {padding: 20px; }// App.js import styles from './style.module.css';function App() {return <div className={styles.container}>Hello World</div>; }
-
自定義生成的類名:在
vite.config.js
中配置:export default defineConfig({css: {modules: {localsConvention: 'camelCaseOnly', // 類名轉為駝峰generateScopedName: '[name]__[local]___[hash:base64:5]' // 自定義格式}} });
17. Vite中如何使用PostCSS?
-
安裝PostCSS:
npm install postcss postcss-loader -D
-
創建PostCSS配置文件(
postcss.config.js
):module.exports = {plugins: {'postcss-preset-env': {}, // 自動添加瀏覽器前綴cssnano: {} // 壓縮CSS} };
-
配置Vite(可選,通常會自動讀取PostCSS配置):
export default defineConfig({css: {postcss: {plugins: [// 直接在這里配置插件]}} });
18. Vite中如何處理靜態資源?
Vite內置對多種靜態資源的支持:
-
直接導入:
import imgUrl from './assets/image.png'; document.getElementById('logo').src = imgUrl;
-
URL引用:在CSS或HTML中使用相對路徑。
.logo {background-image: url('./assets/logo.png'); }
-
資源處理:
- 小于
build.assetsInlineLimit
(默認4kb)的資源會被內聯為base64。 - 其他資源會被復制到輸出目錄并生成哈希文件名。
- 小于
-
特殊導入:
import svgUrl from './logo.svg?url'; // 強制作為URL import svgComponent from './logo.svg?component'; // 作為React/Vue組件導入
19. Vite中如何配置構建輸出路徑?
在vite.config.js
中使用build.outDir
配置:
export default defineConfig({build: {outDir: 'dist', // 默認輸出目錄assetsDir: 'assets', // 靜態資源目錄},
});
20. Vite中如何進行代碼分割?
Vite支持多種代碼分割方式:
-
動態導入(推薦):
// 按需加載模塊 button.addEventListener('click', async () => {const { heavyModule } = await import('./heavyModule.js');heavyModule(); });
-
多入口應用:
export default defineConfig({build: {rollupOptions: {input: {main: '/index.html',about: '/about.html',},},}, });
-
共享模塊分割:Rollup會自動分割共享模塊,也可通過
manualChunks
配置:export default defineConfig({build: {rollupOptions: {output: {manualChunks: {vendor: ['lodash', 'axios'], // 將這些依賴單獨打包},},},}, });
No. | 大劍師精品GIS教程推薦 |
---|---|
0 | 地圖渲染基礎- 【WebGL 教程】 - 【Canvas 教程】 - 【SVG 教程】 |
1 | Openlayers 【入門教程】 - 【源代碼+示例 300+】 |
2 | Leaflet 【入門教程】 - 【源代碼+圖文示例 150+】 |
3 | MapboxGL 【入門教程】 - 【源代碼+圖文示例150+】 |
4 | Cesium 【入門教程】 - 【源代碼+綜合教程 200+】 |
5 | threejs 【中文API】 - 【源代碼+圖文示例200+】 |
6 | Shader 編程 【圖文示例 100+】 |
21. Vite中如何配置多入口?
在vite.config.js
中配置Rollup的input
選項:
import { defineConfig } from 'vite';
import { fileURLToPath, URL } from 'url';export default defineConfig({build: {rollupOptions: {input: {main: fileURLToPath(new URL('./index.html', import.meta.url)),admin: fileURLToPath(new URL('./admin.html', import.meta.url)),},},},
});
對應的HTML文件結構:
├── index.html
├── admin.html
└── src/├── main.js└── admin.js
22. Vite的構建優化有哪些方法?
- 代碼分割:使用動態導入或
manualChunks
配置。 - 壓縮代碼:生產環境默認啟用Terser壓縮。
- 移除console.log:通過Terser配置移除調試代碼。
- CDN加載外部資源:使用
externals
排除依賴,通過CDN加載。 - 圖片優化:使用
image
插件壓縮圖片。 - 預加載/預取:通過插件自動添加
<link rel="preload">
。 - 按需加載:動態導入非關鍵資源。
- 使用esbuild:通過
esbuild
選項進一步優化構建。
配置示例:
export default defineConfig({build: {minify: 'terser',terserOptions: {compress: {drop_console: true, // 移除consoledrop_debugger: true, // 移除debugger},},rollupOptions: {external: ['vue', 'react'], // 排除這些依賴output: {manualChunks: {vendor: ['lodash'], // 將lodash單獨打包},},},},
});
23. Vite的插件機制是如何工作的?
Vite插件基于Rollup插件接口,同時擴展了一些特定鉤子:
-
插件類型:
- 通用插件:同時影響開發和生產環境。
- 開發時插件:僅在開發環境生效。
- 構建時插件:僅在生產環境生效。
-
核心鉤子:
config
:修改Vite配置。transform
:轉換單個模塊內容。configureServer
:配置開發服務器。handleHotUpdate
:自定義HMR邏輯。
-
執行順序:插件按注冊順序執行,可通過
enforce
選項調整優先級。
24. Vite中如何編寫一個插件?
一個簡單的Vite插件結構:
// my-plugin.js
export default function myPlugin() {return {name: 'my-vite-plugin', // 插件名稱enforce: 'pre', // 執行優先級:'pre' | 'post'// 修改Vite配置config(config, { command }) {if (command === 'serve') {return {// 開發環境配置};}},// 轉換模塊內容transform(code, id) {if (id.endsWith('.js')) {// 修改JavaScript代碼return code.replace('console.log', '// console.log');}return code;},// 配置開發服務器configureServer(server) {server.middlewares.use((req, res, next) => {// 自定義中間件邏輯next();});},// 自定義HMR處理handleHotUpdate(ctx) {console.log('HMR update:', ctx.file);return [];},};
}
在vite.config.js
中使用:
import myPlugin from './my-plugin';export default defineConfig({plugins: [myPlugin()],
});
25. Vite中常用的插件有哪些?
-
官方插件:
@vitejs/plugin-vue
:Vue 3支持。@vitejs/plugin-react
:React支持。@vitejs/plugin-legacy
:舊版瀏覽器支持。
-
社區插件:
vite-plugin-pwa
:漸進式Web應用支持。vite-plugin-svg-icons
:SVG圖標處理。vite-plugin-compression
:生產環境資源壓縮。vite-plugin-eslint
:集成ESLint。unplugin-auto-import
:自動導入API。unplugin-vue-components
:自動導入組件。@rollup/plugin-alias
:別名支持。@rollup/plugin-json
:JSON處理。
26. Vite中如何使用TypeScript?
Vite內置對TypeScript的支持,無需額外配置:
-
安裝依賴:
npm install typescript @types/node -D
-
創建tsconfig.json:
npx tsc --init
-
編寫TypeScript代碼:
// src/main.ts import { createApp } from 'vue'; import App from './App.vue';createApp(App).mount('#app');
-
配置選項(可選):
// vite.config.ts import { defineConfig } from 'vite';export default defineConfig({esbuild: {// 配置esbuild轉換選項jsxFactory: 'h',jsxFragment: 'Fragment',}, });
27. Vite中如何處理Vue單文件組件?
-
安裝Vue插件:
npm install @vitejs/plugin-vue -D
-
配置Vite:
// vite.config.js import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue';export default defineConfig({plugins: [vue()], });
-
編寫Vue組件:
<!-- src/App.vue --> <template><div class="hello"><h1>{{ msg }}</h1></div> </template><script lang="ts"> import { defineComponent } from 'vue';export default defineComponent({setup() {return {msg: 'Hello Vue 3 + TypeScript + Vite',};}, }); </script>
28. Vite中如何處理React組件?
-
安裝React插件:
npm install @vitejs/plugin-react -D
-
配置Vite:
// vite.config.js import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react';export default defineConfig({plugins: [react()], });
-
編寫React組件:
// src/App.jsx import React from 'react';function App() {return (<div className="App"><h1>Hello React + Vite</h1></div>); }export default App;
29. Vite中如何使用JSX/TSX?
Vite內置對JSX/TSX的支持,無需額外配置:
- React JSX:使用
@vitejs/plugin-react
插件。 - Vue JSX:安裝
@vue/babel-plugin-jsx
或使用@vitejs/plugin-vue-jsx
。
示例(Vue JSX):
// 安裝依賴
npm install @vitejs/plugin-vue-jsx -D// vite.config.js
import { defineConfig } from 'vite';
import vueJsx from '@vitejs/plugin-vue-jsx';export default defineConfig({plugins: [vueJsx()],
});// 組件中使用
import { defineComponent } from 'vue';export default defineComponent({setup() {return () => <h1>Hello Vue JSX</h1>;},
});
30. Vite中如何進行單元測試?
Vite項目常用的單元測試方案:
-
Jest:
- 安裝:
npm install jest @types/jest ts-jest -D
- 配置:創建
jest.config.js
module.exports = {preset: 'ts-jest',testEnvironment: 'jsdom', };
- 測試命令:
npx jest
- 安裝:
-
Vitest(Vite官方推薦):
- 安裝:
npm install vitest -D
- 配置(
vite.config.js
):
import { defineConfig } from 'vite';export default defineConfig({test: {environment: 'jsdom',}, });
- 編寫測試:
// sum.test.js import { describe, it, expect } from 'vitest';describe('sum test', () => {it('adds 1 + 2 to equal 3', () => {expect(1 + 2).toBe(3);}); });
- 測試命令:
npx vitest
- 安裝:
二、120道面試題目錄列表
文章序號 | vite面試題120道 |
---|---|
1 | vite面試題及詳細答案120道(01-30) |
2 | vite面試題及詳細答案120道(31-60) |
3 | vite面試題及詳細答案120道(61-90) |
4 | vite面試題及詳細答案120道(91-120) |