You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/integrations/svelte-scoped.md
-20Lines changed: 0 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -237,26 +237,6 @@ Add the `%unocss-svelte-scoped.global%` placeholder into your `<head>` tag. In S
237
237
</head>
238
238
```
239
239
240
-
If using SvelteKit, you also must add the following to the `transformPageChunk` hook in your `src/hooks.server.js` file:
241
-
242
-
```js [src/hooks.server.js]
243
-
/**@type{import('@sveltejs/kit').Handle}*/
244
-
exportasyncfunctionhandle({ event, resolve }) {
245
-
constresponse=awaitresolve(event, {
246
-
transformPageChunk: ({ html }) =>
247
-
html.replace(
248
-
'%unocss-svelte-scoped.global%',
249
-
'unocss_svelte_scoped_global_styles'
250
-
),
251
-
})
252
-
return response
253
-
}
254
-
```
255
-
256
-
This transformation must be in a file whose [path includes `hooks` and `server`](https://github.com/unocss/unocss/blob/main/packages-integrations/svelte-scoped/src/_vite/global.ts#L12) (e.g. `src/hooks.server.js`, `src/hooks.server.ts`) as `svelte-scoped` will be looking in your server hooks file to replace `unocss_svelte_scoped_global_styles` with your global styles. Make sure to not import this transformation from another file, such as when using [sequence](https://kit.svelte.dev/docs/modules#sveltejs-kit-hooks-sequence) from `@sveltejs/kit/hooks`.
257
-
258
-
_In a regular Svelte project, Vite's `transformIndexHtml` hook will do this automatically._
259
-
260
240
## Svelte Preprocessor
261
241
262
242
Use utility styles to build a component library that is not dependent on including a companion CSS file by using a preprocessor to place generated styles directly into built components. Check out the [SvelteKit Library example](https://github.com/unocss/unocss/tree/main/examples/sveltekit-preprocess) in Stackblitz:
exportconstDEV_GLOBAL_STYLES_DATA_TITLE='unocss-svelte-scoped global styles'// If global styles are setup properly this is the string the Vite plugin will find in the <head> tag
* It would be nice to parse the svelte config to learn if user set a custom hooks.server name but both of the following methods have problems:
9
-
* - const svelteConfigRaw = readFileSync('./svelte.config.js', 'utf-8') // manual parsing could fail if people import hooks name from elsewhere or use unstandard syntax
10
-
* - ({ default: svelteConfig } = await import(`${viteConfig.root}/svelte.config.js`)) // throws import errors when vitePreprocess is included in svelte.config.js on Windows (related to path issues)
// preset-web-fonts doesn't heed the minify option and sends through newlines (\n) that break if we use regular quotes here. Always using a backtick here is easier than removing newlines, which are actually kind of useful in dev mode. I might consider turning minify off altogether in dev mode.
constSVELTE_ERROR=`[unocss] You have not setup the svelte-scoped global styles correctly. You must place '${PLACEHOLDER_USER_SETS_IN_INDEX_HTML}' in your index.html file.
36
-
`
37
-
constSVELTE_KIT_ERROR=`[unocss] You have not setup the svelte-scoped global styles correctly. You must place '${PLACEHOLDER_USER_SETS_IN_INDEX_HTML}' in your app.html file. You also need to have a transformPageChunk hook in your hooks.server.js file with: \`html.replace('${PLACEHOLDER_USER_SETS_IN_INDEX_HTML}', '${GLOBAL_STYLES_PLACEHOLDER}')\`. You can see an example of the usage at https://github.com/unocss/unocss/tree/main/examples/sveltekit-scoped.`
server.config.logger.error(`[unocss] You have not setup the svelte-scoped global styles correctly. You must place '${PLACEHOLDER_USER_SETS_IN_INDEX_HTML}' in your \`${isSvelteKit ? 'app.html' : 'index.html'}\` file.`,{timestamp: true})
this.warn(`[unocss] You are probably using an outdated setup for your sveltekit app. The server hook to handle an unocss styles placeholder is no longer needed.`)
// SvelteKit ignores this hook, so we use the `renderChunk` and `transform` hooks instead for SvelteKit, but if they ever support running this hook inside their hooks.server.js file we can simplify to just using this hook.
0 commit comments