Skip to content

Commit 2843939

Browse files
authored
feat(page): Add path rewrite whitelist (#9)
- 在 PluginOptions 接口中添加 rewriteWhitelist 属性,用于配置白名单 - 修改 createRewrites 函数,支持白名单路径的重写规则 - 默认添加 /__unocss 和 /__devtools__ 路径到白名单
1 parent 88bf739 commit 2843939

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

packages/page-html/src/pagePlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function createPagePlugin(pluginOptions: PluginOptions = {}): Plugin {
7979
verbose: !!process.env.DEBUG && process.env.DEBUG !== 'false',
8080
disableDotRule: undefined,
8181
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
82-
rewrites: createRewrites(pages, viteConfig),
82+
rewrites: createRewrites(pages, viteConfig, pluginOptions),
8383
}) as Connect.NextHandleFunction
8484
)
8585
},

packages/page-html/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ export interface PluginOptions {
4848
* @description inject data and tags to html.
4949
*/
5050
inject?: InjectOptions
51+
52+
/**
53+
* @description Whitelist, where no redirection is performed when matched to these paths
54+
*/
55+
rewriteWhitelist?: RegExp
5156
}
5257

5358
/** page configurations */

packages/page-html/src/utils/core.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function createRewrite(reg: string, page: PageItem, baseUrl: string, proxyKeys:
125125
/**
126126
* 生成重写规则
127127
*/
128-
export function createRewrites(pages: Pages, viteConfig: ResolvedConfig): Rewrite[] {
128+
export function createRewrites(pages: Pages, viteConfig: ResolvedConfig, options: PluginOptions = {}): Rewrite[] {
129129
const rewrites: Rewrite[] = []
130130
const indexReg = /(\S+)(\/index\/?)$/
131131
const baseUrl = viteConfig.base ?? '/'
@@ -144,7 +144,18 @@ export function createRewrites(pages: Pages, viteConfig: ResolvedConfig): Rewrit
144144
rewrites.push(createRewrite(`${_path}(/)?`, page, baseUrl, proxyKeys))
145145
}
146146
})
147-
// 3. 支持 `/` 请求
147+
// 3. 白名单,匹配到这些路径时不进行重定向
148+
rewrites.push({
149+
from: /^\/(__unocss|__devtools__)\/$/,
150+
to: ({ parsedUrl }) => parsedUrl.pathname as string
151+
})
152+
if (options.rewriteWhitelist instanceof RegExp) {
153+
rewrites.push({
154+
from: options.rewriteWhitelist,
155+
to: ({ parsedUrl }) => parsedUrl.pathname as string
156+
})
157+
}
158+
// 4. 支持 `/` 请求
148159
rewrites.push(createRewrite('', pages['index'] ?? {}, baseUrl, proxyKeys))
149160

150161
return rewrites

0 commit comments

Comments
 (0)