diff --git a/packages/taro-vite-runner/src/h5/config.ts b/packages/taro-vite-runner/src/h5/config.ts index d0e8e892f0f5..e5dd741f1177 100644 --- a/packages/taro-vite-runner/src/h5/config.ts +++ b/packages/taro-vite-runner/src/h5/config.ts @@ -14,6 +14,7 @@ import type { PostcssOption } from '@tarojs/taro/types/compile' import type { ViteH5CompilerContext } from '@tarojs/taro/types/compile/viteCompilerContext' import type { PluginOption } from 'vite' + export default function (viteCompilerContext: ViteH5CompilerContext): PluginOption { const { taroConfig, cwd: appPath, app, sourceDir } = viteCompilerContext const routerMode = taroConfig.router?.mode || 'hash' @@ -97,20 +98,77 @@ export default function (viteCompilerContext: ViteH5CompilerContext): PluginOpti if (isObject>(serverOption.headers)) { headers = serverOption.headers } + let hmr = true - if (isBoolean(serverOption.hot)) { - hmr = serverOption.hot + if (isBoolean(serverOption.hmr)) { + hmr = serverOption.hmr } + let open: string | boolean = true if (isBoolean(serverOption.open) || isString(serverOption.open)) { open = serverOption.open } + + + let cors: boolean | Record = true + if (isBoolean(serverOption.cors) || isObject>(serverOption.cors)) { + cors = serverOption.cors + } + + + let watch: Record = {} + if (isObject>(serverOption.watch)) { + watch = serverOption.watch + } + + let strictPort = false + if (isBoolean(serverOption.strictPort)) { + strictPort = serverOption.strictPort + } + + let middlewareMode: 'ssr' | 'html' | false = false + if (serverOption.middlewareMode === 'ssr' || serverOption.middlewareMode === 'html') { + middlewareMode = serverOption.middlewareMode + } + + let origin = '' + if (isString(serverOption.origin)) { + origin = serverOption.origin + } + + let fsStrict = true + if (serverOption.fs && isBoolean(serverOption.fs.strict)) { + fsStrict = serverOption.fs.strict + } + + let fsAllow: string[] = [] + if (serverOption.fs && Array.isArray(serverOption.fs.allow)) { + fsAllow = serverOption.fs.allow + } + + let fsDeny: string[] = ['.env', '.env.*', '*.{crt,pem}', '**/.git/**'] + if (serverOption.fs && Array.isArray(serverOption.fs.deny)) { + fsDeny = serverOption.fs.deny + } + const mode = getMode(taroConfig) const mainFields = [...defaultMainFields] if (!isProd) { mainFields.unshift('main:h5') } + let allowedHosts: true | string[] | undefined + if (serverOption.allowedHosts === true || Array.isArray(serverOption.allowedHosts)) { + allowedHosts = serverOption.allowedHosts + } else if (isString(serverOption.allowedHosts) && serverOption.allowedHosts) { + allowedHosts = [serverOption.allowedHosts] + } + + let sourcemapIgnoreList: false | ((sourcePath: string, sourcemapPath: string) => boolean) = (sourcePath) => sourcePath.includes('node_modules') + if (typeof serverOption.sourcemapIgnoreList === 'boolean' || typeof serverOption.sourcemapIgnoreList === 'function') { + sourcemapIgnoreList = serverOption.sourcemapIgnoreList + } + return { name: 'taro:vite-h5-config', enforce: 'pre', @@ -173,9 +231,21 @@ export default function (viteCompilerContext: ViteH5CompilerContext): PluginOpti port: serverOption.port ? Number(serverOption.port) : 10086, https: typeof serverOption.https !== 'boolean' ? serverOption.https : undefined, open, - proxy: (serverOption.proxy as any) || {}, + proxy: serverOption.proxy || {} as Record>, headers, hmr, + watch, + fs: { + strict: fsStrict, + allow: fsAllow, + deny: fsDeny, + }, + allowedHosts, + middlewareMode, + strictPort, + sourcemapIgnoreList, + origin, + cors, }, css: { postcss: { diff --git a/packages/taro/types/compile/config/h5.d.ts b/packages/taro/types/compile/config/h5.d.ts index d0fb0ee7f538..43883d311b67 100644 --- a/packages/taro/types/compile/config/h5.d.ts +++ b/packages/taro/types/compile/config/h5.d.ts @@ -6,6 +6,7 @@ import type { IOption, IPostcssOption, IUrlLoaderOption } from './util' import type { OutputOptions as RollupOutputOptions } from 'rollup' import type { Compiler, CompilerTypes, CompilerWebpackTypes } from '../compiler' import type { OutputExt } from './project' +import type { ServerOptions as ViteServerOptions } from 'vite' export interface IH5RouterConfig { /** 配置路由模式 */ @@ -51,7 +52,8 @@ export interface IH5Config { router?: IH5RouterConfig /** 预览服务的配置,可以更改端口等参数。具体配置参考 [webpack-dev-server](https://webpack.js.org/configuration/dev-server) */ - devServer?: webpackDevServer.Configuration + // 修改后:同时支持 Webpack 和 Vite + devServer?: T extends 'vite' ? ViteServerOptions : webpackDevServer.Configuration /** 用于控制是否生成 js、css 对应的 sourceMap (默认值:watch 模式下为 true,否则为 false) */ enableSourceMap?: boolean