Skip to content

Commit ad1a40a

Browse files
committed
chore: update taro-vite-runner
1 parent 7719ef5 commit ad1a40a

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed

packages/taro-vite-runner/src/h5/config.ts

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,69 @@ export default function (viteCompilerContext: ViteH5CompilerContext): PluginOpti
105105
if (isBoolean(serverOption.open) || isString(serverOption.open)) {
106106
open = serverOption.open
107107
}
108+
let cors: boolean | Record<string, any> = true // 明确声明为联合类型
109+
const corsOption = (serverOption as any).cors
110+
if (isBoolean(corsOption) || isObject<Record<string, any>>(corsOption)) {
111+
cors = corsOption
112+
}
113+
let watch: Record<string, any> = {} // 默认值为空对象
114+
const watchOption = (serverOption as any).watch
115+
if (isObject<Record<string, any>>(watchOption)) {
116+
watch = watchOption
117+
}
118+
let strictPort = false // 默认值为false
119+
const strictPortOption = (serverOption as any).strictPort
120+
if (isBoolean(strictPortOption)) {
121+
strictPort = strictPortOption
122+
}
123+
let middlewareMode: 'ssr' | 'html' | false = false // 默认值为false(禁用中间件模式)
124+
const middlewareModeOption = (serverOption as any).middlewareMode
125+
if (middlewareModeOption === 'ssr' || middlewareModeOption === 'html') {
126+
middlewareMode = middlewareModeOption
127+
}
128+
let base: string | undefined // 默认值为undefined
129+
const baseOption = (serverOption as any).base
130+
if (isString(baseOption)) {
131+
let formattedBase = baseOption
132+
if (!formattedBase.startsWith('/')) {
133+
formattedBase = '/' + formattedBase
134+
}
135+
if (!formattedBase.endsWith('/')) {
136+
formattedBase += '/'
137+
}
138+
base = formattedBase
139+
}
140+
let origin = '' // 默认值为空字符串
141+
const originOption = (serverOption as any).origin
142+
if (isString(originOption)) {
143+
origin = originOption
144+
}
145+
let fsStrict = true // 默认值为true(自Vite 2.7起默认启用)
146+
const fsStrictOption = (serverOption as any).strict
147+
if (isBoolean(fsStrictOption)) {
148+
fsStrict = fsStrictOption
149+
}
150+
let fsAllow: string[] = [] // 默认值为空数组
151+
const fsAllowOption = (serverOption as any).allow
152+
if (Array.isArray(fsAllowOption)) {
153+
fsAllow = fsAllowOption
154+
}
155+
let fsDeny: string[] = ['.env', '.env.*', '*.{crt,pem}', '**/.git/**'] // 默认值为安全的黑名单
156+
const fsDenyOption = (serverOption as any).deny
157+
if (Array.isArray(fsDenyOption)) {
158+
fsDeny = fsDenyOption
159+
}
108160
const mode = getMode(taroConfig)
109161
const mainFields = [...defaultMainFields]
110162
if (!isProd) {
111163
mainFields.unshift('main:h5')
112164
}
165+
let allowedHosts: true | string[] | undefined
166+
if ((serverOption as any).allowedHosts === true || Array.isArray((serverOption as any).allowedHosts)) {
167+
allowedHosts = (serverOption as any).allowedHosts
168+
} else if (isString((serverOption as any).allowedHosts) && (serverOption as any).allowedHosts) {
169+
allowedHosts = [(serverOption as any).allowedHosts]
170+
}
113171

114172
return {
115173
name: 'taro:vite-h5-config',
@@ -176,19 +234,18 @@ export default function (viteCompilerContext: ViteH5CompilerContext): PluginOpti
176234
proxy: (serverOption.proxy as any) || {},
177235
headers,
178236
hmr,
179-
watch: serverOption.watch ?? {},
237+
watch,
180238
fs: {
181-
strict: serverOption.strict ?? true,
182-
allow: serverOption.allow ?? [],
183-
deny: serverOption.deny ?? ['.env', '.env.*', '*.{crt,pem}', '**/.git/**'],
239+
strict: fsStrict,
240+
allow: fsAllow,
241+
deny: fsDeny,
184242
},
185-
allowedHosts: serverOption.allowedHosts || [],
186-
middlewareMode: serverOption.middlewareMode ?? false,
187-
strictPort: serverOption.strictPort ?? false,
188-
sourcemapIgnoreList: serverOption.sourcemapIgnoreList ?? ((sourcePath) => sourcePath.includes('node_modules')),
189-
origin: serverOption.origin,
190-
cors: serverOption.cors ?? true,
191-
warmup: serverOption.warmup ?? {},
243+
allowedHosts,
244+
middlewareMode,
245+
strictPort,
246+
base,
247+
origin,
248+
cors,
192249
},
193250
css: {
194251
postcss: {

0 commit comments

Comments
 (0)