Vite + TS 环境搭建
Vite! 下一代前端开发与构建工具
- 首先创建一个空目录
mkdir demo
- 进入创建好的文件夹中
cd demo
并创建 src 文件夹(在该文件夹里写代码,自行创建index.ts
) - 初始化
package.json
pnpm init
- 在生成的
package.json
中写入如下依赖
{
"name": "ts-demo",
"version": "1.0.0",
"description": "study ts",
"main": "index.js",
"scripts": {
"dev": "vite --mode development",
"build": "vite build",
"build:beta": "vite build --mode beta",
"build:release": "vite build --mode release",
"preview": "vite preview"
},
"keywords": [
"ts",
"study"
],
"author": "Cikaros",
"license": "MIT",
"devDependencies": {
"vite": "^2.9.6"
}
}
- 运行
pnpm install
安装依赖 - 在目录中创建
index.html
并写入如下内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="/favicon.svg"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/index.ts"></script>
</body>
</html>
- 在目录中创建
vite.config.js
并写入如下内容
import {defineConfig} from 'vite'
module.exports = defineConfig({
build: {
outDir: 'dist'
},
server: {
proxy: {
"/api": {
target: "https://www.baidu.com/",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ""),
},
},
},
})
- 在目录中创建
tsconfig.json
并写入如下内容
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"sourceMap": true,
"pretty": true,
"resolveJsonModule": true,
"allowJs": true,
"checkJs": true,
"suppressImplicitAnyIndexErrors": true,
"strictPropertyInitialization": false,
"downlevelIteration": true,
"noUnusedLocals": false,
"noImplicitAny": false,
"noImplicitThis": true,
"removeComments": false,
"strictFunctionTypes": false,
"baseUrl": ".",
"types": [
"vite/client",
"node"
],
"typeRoots": [
"./node_modules/@types/",
"./types"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx",
"types/**/*.d.ts",
"types/**/*.ts",
"preview/**/*.ts",
"preview/**/*.d.ts",
"preview/**/*.tsx"
],
"exclude": [
"node_modules",
"dist"
]
}
- 根据需要添加相应环境变量或其他所需依赖
- 运行
pnpm run dev
即可运行项目
Vite + TS 环境搭建
https://blog.cikaros.top/doc/c4a056ea.html