This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt, vite): inline global and component styles in server respon…
…se (#7160) Co-authored-by: Pooya Parsa <pooya@pi0.io>
- Loading branch information
Showing
15 changed files
with
241 additions
and
12 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
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
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,101 @@ | ||
import { pathToFileURL } from 'node:url' | ||
import { Plugin } from 'vite' | ||
import { findStaticImports } from 'mlly' | ||
import { dirname, relative } from 'pathe' | ||
import { genObjectFromRawEntries } from 'knitwork' | ||
import { filename } from 'pathe/utils' | ||
import { parseQuery, parseURL } from 'ufo' | ||
import { isCSS } from '../utils' | ||
|
||
interface SSRStylePluginOptions { | ||
srcDir: string | ||
shouldInline?: (id?: string) => boolean | ||
} | ||
|
||
export function ssrStylesPlugin (options: SSRStylePluginOptions): Plugin { | ||
const cssMap: Record<string, string[]> = {} | ||
const idRefMap: Record<string, string> = {} | ||
const globalStyles = new Set<string>() | ||
|
||
const relativeToSrcDir = (path: string) => relative(options.srcDir, path) | ||
|
||
return { | ||
name: 'ssr-styles', | ||
generateBundle (outputOptions) { | ||
const emitted: Record<string, string> = {} | ||
for (const file in cssMap) { | ||
if (!cssMap[file].length) { continue } | ||
|
||
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: [ | ||
...cssMap[file].map((css, i) => `import style_${i} from './${relative(dirname(base), this.getFileName(css))}';`), | ||
`export default [${cssMap[file].map((_, i) => `style_${i}`).join(', ')}]` | ||
].join('\n') | ||
}) | ||
} | ||
|
||
const globalStylesArray = Array.from(globalStyles).map(css => idRefMap[css] && this.getFileName(idRefMap[css])).filter(Boolean) | ||
|
||
this.emitFile({ | ||
type: 'asset', | ||
fileName: 'styles.mjs', | ||
source: | ||
[ | ||
...globalStylesArray.map((css, i) => `import style_${i} from './${css}';`), | ||
`const globalStyles = [${globalStylesArray.map((_, i) => `style_${i}`).join(', ')}]`, | ||
'const resolveStyles = r => globalStyles.concat(r.default || r || [])', | ||
`export default ${genObjectFromRawEntries( | ||
Object.entries(emitted).map(([key, value]) => [key, `() => import('./${this.getFileName(value)}').then(resolveStyles)`]) | ||
)}` | ||
].join('\n') | ||
}) | ||
}, | ||
renderChunk (_code, chunk) { | ||
if (!chunk.isEntry) { return null } | ||
// Entry | ||
for (const mod in chunk.modules) { | ||
if (isCSS(mod) && !mod.includes('&used')) { | ||
globalStyles.add(relativeToSrcDir(mod)) | ||
} | ||
} | ||
return null | ||
}, | ||
async transform (code, id) { | ||
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href)) | ||
const query = parseQuery(search) | ||
if (!pathname.match(/\.(vue|((c|m)?j|t)sx?)$/g) || query.macro) { return } | ||
if (options.shouldInline && !options.shouldInline(id)) { return } | ||
|
||
const relativeId = relativeToSrcDir(id) | ||
cssMap[relativeId] = cssMap[relativeId] || [] | ||
|
||
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 } | ||
|
||
const ref = this.emitFile({ | ||
type: 'chunk', | ||
name: `${filename(id)}-styles-${++styleCtr}.mjs`, | ||
id: resolved.id + '?inline&used' | ||
}) | ||
|
||
idRefMap[relativeToSrcDir(resolved.id)] = ref | ||
cssMap[relativeId].push(ref) | ||
} | ||
} | ||
} | ||
} |
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
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,3 @@ | ||
:root { | ||
--assets: 'assets'; | ||
} |
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,3 @@ | ||
:root { | ||
--functional: 'functional'; | ||
} |
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,3 @@ | ||
:root { | ||
--global: 'global'; | ||
} |
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,3 @@ | ||
:root { | ||
--plugin: 'plugin'; | ||
} |
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,5 @@ | ||
import '~/assets/functional.css' | ||
|
||
export default defineComponent({ | ||
render: () => 'hi' | ||
}) |
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,22 @@ | ||
<template> | ||
<div> | ||
<ClientOnlyScript /> | ||
<FunctionalComponent /> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
import '~/assets/assets.css' | ||
</script> | ||
|
||
<style lang="postcss"> | ||
:root { | ||
--postcss: 'postcss'; | ||
} | ||
</style> | ||
|
||
<style scoped> | ||
div { | ||
--scoped: 'scoped'; | ||
} | ||
</style> |
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,5 @@ | ||
import '~/assets/plugin.css' | ||
|
||
export default defineNuxtPlugin(() => { | ||
// | ||
}) |