2022-09-03 13:03:30 +00:00
|
|
|
import { pathToFileURL } from 'node:url'
|
2022-12-11 21:44:52 +00:00
|
|
|
import type { Plugin } from 'vite'
|
2022-09-03 13:03:30 +00:00
|
|
|
import { findStaticImports } from 'mlly'
|
|
|
|
import { dirname, relative } from 'pathe'
|
|
|
|
import { genObjectFromRawEntries } from 'knitwork'
|
|
|
|
import { filename } from 'pathe/utils'
|
|
|
|
import { parseQuery, parseURL } from 'ufo'
|
2023-05-11 22:33:17 +00:00
|
|
|
import type { Component } from '@nuxt/schema'
|
2022-09-03 13:03:30 +00:00
|
|
|
|
|
|
|
interface SSRStylePluginOptions {
|
|
|
|
srcDir: string
|
2022-09-07 08:41:08 +00:00
|
|
|
chunksWithInlinedCSS: Set<string>
|
2023-05-11 22:33:17 +00:00
|
|
|
shouldInline?: ((id?: string) => boolean) | boolean
|
|
|
|
components: Component[]
|
2022-09-03 13:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function ssrStylesPlugin (options: SSRStylePluginOptions): Plugin {
|
2022-09-07 08:41:08 +00:00
|
|
|
const cssMap: Record<string, { files: string[], inBundle: boolean }> = {}
|
2022-09-03 13:03:30 +00:00
|
|
|
const idRefMap: Record<string, string> = {}
|
|
|
|
|
|
|
|
const relativeToSrcDir = (path: string) => relative(options.srcDir, path)
|
|
|
|
|
2022-09-12 15:14:11 +00:00
|
|
|
const warnCache = new Set<string>()
|
2023-05-11 22:33:17 +00:00
|
|
|
const islands = options.components.filter(component =>
|
|
|
|
component.island ||
|
|
|
|
// .server components without a corresponding .client component will need to be rendered as an island
|
|
|
|
(component.mode === 'server' && !options.components.some(c => c.pascalName === component.pascalName && c.mode === 'client'))
|
|
|
|
)
|
2022-09-12 15:14:11 +00:00
|
|
|
|
2022-09-03 13:03:30 +00:00
|
|
|
return {
|
|
|
|
name: 'ssr-styles',
|
2023-02-16 15:00:40 +00:00
|
|
|
resolveId: {
|
|
|
|
order: 'pre',
|
|
|
|
async handler (id, importer, options) {
|
|
|
|
if (!id.endsWith('.vue')) { return }
|
|
|
|
|
|
|
|
const res = await this.resolve(id, importer, { ...options, skipSelf: true })
|
|
|
|
if (res) {
|
|
|
|
return {
|
|
|
|
...res,
|
|
|
|
moduleSideEffects: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-09-03 13:03:30 +00:00
|
|
|
generateBundle (outputOptions) {
|
|
|
|
const emitted: Record<string, string> = {}
|
|
|
|
for (const file in cssMap) {
|
2022-09-07 08:41:08 +00:00
|
|
|
const { files, inBundle } = cssMap[file]
|
|
|
|
// File has been tree-shaken out of build (or there are no styles to inline)
|
|
|
|
if (!files.length || !inBundle) { continue }
|
2022-09-03 13:03:30 +00:00
|
|
|
|
|
|
|
const base = typeof outputOptions.assetFileNames === 'string'
|
|
|
|
? outputOptions.assetFileNames
|
|
|
|
: outputOptions.assetFileNames({
|
|
|
|
type: 'asset',
|
|
|
|
name: `${filename(file)}-styles.mjs`,
|
|
|
|
source: ''
|
|
|
|
})
|
|
|
|
|
|
|
|
emitted[file] = this.emitFile({
|
|
|
|
type: 'asset',
|
|
|
|
name: `${filename(file)}-styles.mjs`,
|
|
|
|
source: [
|
2022-09-07 08:41:08 +00:00
|
|
|
...files.map((css, i) => `import style_${i} from './${relative(dirname(base), this.getFileName(css))}';`),
|
|
|
|
`export default [${files.map((_, i) => `style_${i}`).join(', ')}]`
|
2022-09-03 13:03:30 +00:00
|
|
|
].join('\n')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-07 08:41:08 +00:00
|
|
|
for (const key in emitted) {
|
|
|
|
// Track the chunks we are inlining CSS for so we can omit including links to the .css files
|
|
|
|
options.chunksWithInlinedCSS.add(key)
|
|
|
|
}
|
|
|
|
|
2022-09-03 13:03:30 +00:00
|
|
|
this.emitFile({
|
|
|
|
type: 'asset',
|
|
|
|
fileName: 'styles.mjs',
|
|
|
|
source:
|
|
|
|
[
|
2022-09-10 09:43:17 +00:00
|
|
|
'const interopDefault = r => r.default || r || []',
|
2022-11-03 19:17:43 +00:00
|
|
|
`export default ${genObjectFromRawEntries(
|
|
|
|
Object.entries(emitted).map(([key, value]) => [key, `() => import('./${this.getFileName(value)}').then(interopDefault)`]) as [string, string][]
|
|
|
|
)}`
|
2022-09-03 13:03:30 +00:00
|
|
|
].join('\n')
|
|
|
|
})
|
|
|
|
},
|
|
|
|
renderChunk (_code, chunk) {
|
2022-09-07 08:41:08 +00:00
|
|
|
if (!chunk.facadeModuleId) { return null }
|
|
|
|
const id = relativeToSrcDir(chunk.facadeModuleId)
|
|
|
|
for (const file in chunk.modules) {
|
|
|
|
const relativePath = relativeToSrcDir(file)
|
|
|
|
if (relativePath in cssMap) {
|
|
|
|
cssMap[relativePath].inBundle = cssMap[relativePath].inBundle ?? !!id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-03 13:03:30 +00:00
|
|
|
return null
|
|
|
|
},
|
|
|
|
async transform (code, id) {
|
|
|
|
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
|
|
|
|
const query = parseQuery(search)
|
2023-05-11 22:33:17 +00:00
|
|
|
|
2023-05-04 16:19:03 +00:00
|
|
|
if (!pathname.match(/\.(vue|((c|m)?j|t)sx?)$/g) || query.macro || query.nuxt_component) { return }
|
2023-05-11 22:33:17 +00:00
|
|
|
|
|
|
|
if (!islands.some(c => c.filePath === pathname)) {
|
|
|
|
if (options.shouldInline === false || (typeof options.shouldInline === 'function' && !options.shouldInline(id))) { return }
|
|
|
|
}
|
2022-09-03 13:03:30 +00:00
|
|
|
|
|
|
|
const relativeId = relativeToSrcDir(id)
|
2022-09-07 08:41:08 +00:00
|
|
|
cssMap[relativeId] = cssMap[relativeId] || { files: [] }
|
2022-09-03 13:03:30 +00:00
|
|
|
|
|
|
|
let styleCtr = 0
|
|
|
|
for (const i of findStaticImports(code)) {
|
|
|
|
const { type } = parseQuery(i.specifier)
|
|
|
|
if (type !== 'style' && !i.specifier.endsWith('.css')) { continue }
|
|
|
|
|
|
|
|
const resolved = await this.resolve(i.specifier, id)
|
|
|
|
if (!resolved) { continue }
|
2022-09-12 15:14:11 +00:00
|
|
|
if (!(await this.resolve(resolved.id + '?inline&used'))) {
|
|
|
|
if (!warnCache.has(resolved.id)) {
|
|
|
|
warnCache.add(resolved.id)
|
|
|
|
this.warn(`[nuxt] Cannot extract styles for \`${i.specifier}\`. Its styles will not be inlined when server-rendering.`)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-09-03 13:03:30 +00:00
|
|
|
|
|
|
|
const ref = this.emitFile({
|
|
|
|
type: 'chunk',
|
|
|
|
name: `${filename(id)}-styles-${++styleCtr}.mjs`,
|
|
|
|
id: resolved.id + '?inline&used'
|
|
|
|
})
|
|
|
|
|
|
|
|
idRefMap[relativeToSrcDir(resolved.id)] = ref
|
2022-09-07 08:41:08 +00:00
|
|
|
cssMap[relativeId].files.push(ref)
|
2022-09-03 13:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|