Nuxt/packages/vite/src/plugins/dev-ssr-css.ts

31 lines
858 B
TypeScript
Raw Normal View History

2022-01-18 16:59:14 +00:00
import { joinURL } from 'ufo'
import type { Plugin } from 'vite'
import { isCSS } from '../utils'
interface DevStyleSSRPluginOptions {
srcDir: string
2022-01-18 16:59:14 +00:00
buildAssetsURL: string
}
export function devStyleSSRPlugin (options: DevStyleSSRPluginOptions): Plugin {
return {
name: 'nuxt:dev-style-ssr',
apply: 'serve',
enforce: 'post',
transform (code, id) {
if (!isCSS(id) || !code.includes('import.meta.hot')) {
return
}
let moduleId = id
if (moduleId.startsWith(options.srcDir)) {
moduleId = moduleId.slice(options.srcDir.length)
}
// When dev `<style>` is injected, remove the `<link>` styles from manifest
2022-01-18 16:59:14 +00:00
const selector = joinURL(options.buildAssetsURL, moduleId)
return code + `\ndocument.querySelectorAll(\`link[href="${selector}"]\`).forEach(i=>i.remove())`
}
}
}