Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vite): add client fetch mode #3255

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/vite/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/// <reference types="vite/client" />
/// <reference lib="dom" />

function post(data: any) {
function post(data: any, config: any) {
return fetch('__POST_PATH__', {
...config,
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -33,7 +34,10 @@ function schedule() {
clearTimeout(_timer)
_timer = setTimeout(() => {
if (pendingClasses.size) {
post({ type: 'add-classes', data: Array.from(pendingClasses) })
post(
{ type: 'add-classes', data: Array.from(pendingClasses) },
{ mode: '__POST_FETCH_MODE__' },
)
include(visitedClasses, pendingClasses)
pendingClasses.clear()
}
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/devtool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Plugin, ResolvedConfig, ViteDevServer } from 'vite'
import type { IncomingMessage } from 'connect'
import type { UnocssPluginContext } from '@unocss/core'
import { toEscapedSelector } from '@unocss/core'
import type { VitePluginConfig } from './types'

const _dirname = typeof __dirname !== 'undefined' ? __dirname : dirname(fileURLToPath(import.meta.url))

Expand Down Expand Up @@ -39,7 +40,7 @@ function getBodyJson(req: IncomingMessage) {
})
}

export function createDevtoolsPlugin(ctx: UnocssPluginContext): Plugin[] {
export function createDevtoolsPlugin(ctx: UnocssPluginContext, pluginConfig: VitePluginConfig<any>): Plugin[] {
let config: ResolvedConfig
let server: ViteDevServer | undefined
let clientCode = ''
Expand Down Expand Up @@ -144,6 +145,7 @@ export function createDevtoolsPlugin(ctx: UnocssPluginContext): Plugin[] {
]
.join('\n')
.replace('__POST_PATH__', `${(config.server?.origin ?? '')}${postPath}`)
.replace('__POST_FETCH_MODE__', pluginConfig.fetchMode ?? 'cors')
}
return config.command === 'build'
? ''
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function UnocssPlugin<Theme extends object>(
const plugins = [
ConfigHMRPlugin(ctx),
...createTransformerPlugins(ctx),
...createDevtoolsPlugin(ctx),
...createDevtoolsPlugin(ctx, inlineConfig),
{
name: 'unocss:api',
api: <UnocssVitePluginAPI>{
Expand Down
10 changes: 10 additions & 0 deletions packages/vite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ export interface VitePluginConfig<Theme extends object = object> extends UserCon
* @default true
*/
hmrTopLevelAwait?: boolean

/**
* Fetch mode in devtools.
*
* Some server does not configure its CORS and you may want to set this to 'no-cors'.
* See https://github.com/unocss/unocss/issues/2822.
*
* @default 'cors'
*/
fetchMode?: 'cors' | 'navigate' | 'no-cors' | 'same-origin'
}