Skip to content

Commit

Permalink
fix: 支持多 mode 环境变量类型生成
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed May 20, 2023
1 parent 41fd4fd commit 02291f8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 39 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"vite": "^4.3.8",
"vite-auto-import-resolvers": "^3.0.5",
"vite-plugin-compression": "^0.5.1",
"vite-plugin-env-types": "^0.1.3",
"vite-plugin-md": "^0.22.5",
"vite-plugin-mock": "^2.9.8",
"vite-plugin-removelog": "^0.2.1",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 2 additions & 37 deletions presets/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { writeFile } from 'fs/promises'
import { isPackageExists } from 'local-pkg'
import Prism from 'markdown-it-prism'
import { dirname, resolve } from 'path'
Expand Down Expand Up @@ -31,6 +30,7 @@ import { fileURLToPath } from 'url'
import { loadEnv } from 'vite'
import { AutoGenerateImports } from 'vite-auto-import-resolvers'
import Compression from 'vite-plugin-compression'
import EnvTypes from 'vite-plugin-env-types'
import Markdown from 'vite-plugin-md'
import { viteMockServe as Mock } from 'vite-plugin-mock'
import Removelog from 'vite-plugin-removelog'
Expand All @@ -56,7 +56,7 @@ export default function () {
Legacy({
targets: ['defaults', 'not IE 11'],
}),
EnvDts({
EnvTypes({
dts: 'presets/types/env.d.ts',
}),
// https://github.com/bluwy/vite-plugin-warmup (依赖预热,加快渲染,未来可能会内置到 vite 中)
Expand Down Expand Up @@ -266,38 +266,3 @@ function Alias(): Plugin {
},
}
}

interface EnvOptions {
/**
* @default "env.d.ts"
*/
dts?: string
}

// 自动生成环境变量类型声明文件
function EnvDts(options: EnvOptions = {}): Plugin {
const { dts = 'env.d.ts' } = options
const ignoreKeys = ['BASE_URL', 'MODE', 'DEV', 'PROD']
return {
name: 'vite-plugin-env-dts',
enforce: 'post',
apply: 'serve',
async configResolved(config) {
const { env } = config

const keys = Object.keys(env).filter((k) => !ignoreKeys.includes(k))
await writeFile(
dts,
`/// <reference types="vite/client" />
interface ImportMetaEnv {
${keys.map((k) => `readonly ${k}: string`).join('\n ')}
}
interface ImportMeta {
readonly env: ImportMetaEnv
}`
)
},
}
}
4 changes: 2 additions & 2 deletions presets/types/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="vite/client" />

interface ImportMetaEnv {
readonly VITE_API_BASE_URL: string
readonly VITE_API_BASE_URL: string
readonly VITE_APP_TITLE: string
readonly VITE_APP_MARKDOWN: string
readonly VITE_APP_DEV_TOOLS: string
Expand All @@ -12,5 +12,5 @@ interface ImportMetaEnv {
}

interface ImportMeta {
readonly env: ImportMetaEnv
readonly env: ImportMetaEnv
}

0 comments on commit 02291f8

Please sign in to comment.