2022-01-18 16:59:14 +00:00
|
|
|
import { joinURL } from 'ufo'
|
2021-11-05 08:55:53 +00:00
|
|
|
import { Plugin } from 'vite'
|
|
|
|
import { isCSS } from '../utils'
|
|
|
|
|
2022-01-18 16:59:14 +00:00
|
|
|
export interface DevStyleSSRPluginOptions {
|
2022-08-12 09:11:09 +00:00
|
|
|
srcDir: string
|
2022-01-18 16:59:14 +00:00
|
|
|
buildAssetsURL: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export function devStyleSSRPlugin (options: DevStyleSSRPluginOptions): Plugin {
|
2021-11-05 08:55:53 +00:00
|
|
|
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
|
2022-08-12 09:11:09 +00:00
|
|
|
if (moduleId.startsWith(options.srcDir)) {
|
|
|
|
moduleId = moduleId.slice(options.srcDir.length)
|
2021-11-05 08:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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())`
|
2021-11-05 08:55:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|