-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff26825
commit 9c09210
Showing
5 changed files
with
107 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
const sheetsMap = new Map<string, HTMLStyleElement>() | ||
|
||
export function updateStyle(id: string, content: string): HTMLStyleElement { | ||
let style = sheetsMap.get(id) | ||
if (!style) { | ||
style = document.createElement('style') | ||
style.setAttribute('type', 'text/css') | ||
style.setAttribute('data-vite-dev-id', id) | ||
style.textContent = content | ||
|
||
} else { | ||
style.textContent = content | ||
} | ||
sheetsMap.set(id, style) | ||
return style | ||
} | ||
|
||
export function removeStyle(id: string): void { | ||
const style = sheetsMap.get(id) | ||
if (style) { | ||
style.remove() | ||
sheetsMap.delete(id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { type Plugin } from 'vite' | ||
import Unocss from 'unocss/vite' | ||
|
||
export function UnocssClientPlugin(): Plugin[] { | ||
const plugins = Unocss() | ||
|
||
const globalPlugin = plugins.find(i => i.name === 'unocss:global') | ||
const globalPostPlugin = plugins.find(i => i.name === 'unocss:global:post') | ||
const rest = plugins.filter(i => !['unocss:global', 'unocss:global:post'].includes(i.name)) | ||
|
||
return [ | ||
...rest, | ||
{ | ||
name: "redirect:client", | ||
enforce: "pre", | ||
async resolveId(source, importer, options) { | ||
|
||
if (source === '/@ud/client') { | ||
return 'plugins/client.ts' | ||
} | ||
}, | ||
}, | ||
globalPlugin, | ||
{ | ||
...globalPostPlugin, | ||
async transform(_code, id, options) { | ||
const data = await (globalPostPlugin.transform as any)(_code, id, options) | ||
|
||
if (data && id === '/__uno.css') { | ||
const { code: outcode } = data | ||
const l3 = outcode.split('\n')[2] as string | ||
const css = l3.substring('const __vite__css = "'.length, l3.length - 1) | ||
const code = [ | ||
`import {updateStyle,removeStyle} from '/@ud/client'`, | ||
`const __vite__id = ${JSON.stringify(id)}`, | ||
`const __vite__css = "${css}"`, | ||
`import.meta.hot.accept()`, | ||
`export default updateStyle(__vite__id,__vite__css)`, | ||
`import.meta.hot.prune(() => removeStyle(__vite__id)) | ||
if (import.meta.hot) { | ||
try { | ||
let hash = __vite__css.match(/__uno_hash_(\\w{6})/) | ||
hash = hash && hash[1] | ||
if (!hash) | ||
console.warn('[unocss-hmr]', 'failed to get unocss hash, hmr might not work') | ||
else | ||
await import.meta.hot.send('unocss:hmr', ['__ALL__', hash]); | ||
} catch (e) { | ||
console.warn('[unocss-hmr]', e) | ||
} | ||
if (!import.meta.url.includes('?')) | ||
await new Promise(resolve => setTimeout(resolve, 100))}` | ||
].join('\n') | ||
|
||
return { code } | ||
} | ||
}, | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import unocss from 'uno.css?inline' | ||
|
||
export function attachUnocss(root: ShadowRoot) { | ||
if (import.meta.env.DEV) { | ||
root.appendChild(unocss as any) | ||
} | ||
else { | ||
import('uno.css?raw').then(({ default: css }) => { | ||
const style = document.createElement('style') | ||
style.innerText = css as string | ||
root.appendChild(style) | ||
}) | ||
} | ||
} | ||
|
||
if (import.meta.hot) { | ||
import.meta.hot.accept(console.log) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters