8000 feat(vite): add option `checkImport` (#4362) · unocss/unocss@1e5caa4 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 1e5caa4

Browse files
authored
feat(vite): add option checkImport (#4362)
1 parent 4d64ffc commit 1e5caa4

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

packages/vite/src/modes/global/build.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
resolveId,
2020
resolveLayer,
2121
} from '../../integration'
22+
import { MESSAGE_UNOCSS_ENTRY_NOT_FOUND } from './shared'
2223

2324
// https://github.com/vitejs/vite/blob/main/packages/plugin-legacy/src/index.ts#L742-L744
2425
function isLegacyChunk(chunk: RenderedChunk, options: NormalizedOutputOptions) {
@@ -279,8 +280,11 @@ export function GlobalModeBuildPlugin(ctx: UnocssPluginContext<VitePluginConfig>
279280
// to replace on current build pipeline, we can skip the warning.
280281
if (replaced)
281282
return
282-
const msg = '[unocss] Entry module not found. Did you add `import \'uno.css\'` in your main entry?'
283-
this.warn(msg)
283+
284+
if ((await getConfig() as VitePluginConfig).checkImport) {
285+
this.warn(MESSAGE_UNOCSS_ENTRY_NOT_FOUND)
286+
}
287+
284288
return
285289
}
286290

packages/vite/src/modes/global/dev.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ import process from 'node:process'
55
import { notNull } from '@unocss/core'
66
import MagicString from 'magic-string'
77
import { getHash, getPath, LAYER_MARK_ALL, resolveId, resolveLayer } from '../../integration'
8+
import { MESSAGE_UNOCSS_ENTRY_NOT_FOUND } from './shared'
89

910
const WARN_TIMEOUT = 20000
1011
const WS_EVENT_PREFIX = 'unocss:hmr'
1112
const HASH_LENGTH = 6
1213

14+
type TimeoutTimer = ReturnType<typeof setTimeout> | undefined
15+
1316
export function GlobalModeDevPlugin(ctx: UnocssPluginContext): Plugin[] {
1417
const { tokens, tasks, flushTasks, affectedModules, onInvalidate, extract, filter, getConfig } = ctx
1518
const servers: ViteDevServer[] = []
1619
const entries = new Set<string>()
1720

18-
let invalidateTimer: any
21+
let invalidateTimer: TimeoutTimer
1922
const lastServedHash = new Map<string, string>()
2023
let lastServedTime = Date.now()
2124
let resolved = false
22-
let resolvedWarnTimer: any
25+
let resolvedWarnTimer: TimeoutTimer
2326

2427
async function generateCSS(layer: string) {
2528
await flushTasks()
@@ -80,17 +83,20 @@ export function GlobalModeDevPlugin(ctx: UnocssPluginContext): Plugin[] {
8083
}
8184
}
8285

83-
function setWarnTimer() {
84-
if (!resolved && !resolvedWarnTimer) {
86+
async function setWarnTimer() {
87+
if (
88+
!resolved
89+
&& !resolvedWarnTimer
90+
&& (await getConfig() as VitePluginConfig).checkImport
91+
) {
8592
resolvedWarnTimer = setTimeout(() => {
8693
if (process.env.TEST || process.env.NODE_ENV === 'test')
8794
return
8895
if (!resolved) {
89-
const msg = '[unocss] Entry module not found. Did you add `import \'uno.css\'` in your main entry?'
90-
console.warn(msg)
96+
console.warn(MESSAGE_UNOCSS_ENTRY_NOT_FOUND)
9197
servers.forEach(({ ws }) => ws.send({
9298
type: 'error',
93-
err: { message: msg, stack: '' },
99+
err: { message: MESSAGE_UNOCSS_ENTRY_NOT_FOUND, stack: '' },
94100
}))
95101
}
96102
}, WARN_TIMEOUT)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export const VIRTUAL_ENTRY_DEFAULT = '/__unocss_entry.css'
2+
3+
export const MESSAGE_UNOCSS_ENTRY_NOT_FOUND = '[unocss] Entry module not found. Did you add `import \'uno.css\'` in your main entry?'

packages/vite/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,11 @@ export interface VitePluginConfig<Theme extends object = object> extends UserCon
5757
* @default 'cors'
5858
*/
5959
fetchMode?: 'cors' | 'navigate' | 'no-cors' | 'same-origin'
60+
61+
/**
62+
* Disable `import 'uno.css'` existing check.
63+
*
64+
* @default true
65+
*/
66+
checkImport?: boolean
6067
}

0 commit comments

Comments
 (0)
0