Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/example-sveltekit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
node_modules

# Output
.output
.vercel
.netlify
.wrangler
/.svelte-kit
/build

# OS
.DS_Store
Thumbs.db

# Env
.env
.env.*
!.env.example
!.env.test

# Vite
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
1 change: 1 addition & 0 deletions examples/example-sveltekit/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
38 changes: 38 additions & 0 deletions examples/example-sveltekit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# sv

Everything you need to build a Svelte project, powered by [`sv`](https://github.com/sveltejs/cli).

## Creating a project

If you're seeing this, you've probably already done this step. Congrats!

```sh
# create a new project in the current directory
npx sv create

# create a new project in my-app
npx sv create my-app
```

## Developing

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:

```sh
npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open
```

## Building

To create a production version of your app:

```sh
npm run build
```

You can preview the production build with `npm run preview`.

> To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
27 changes: 27 additions & 0 deletions examples/example-sveltekit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "example-sveltekit",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"prepare": "svelte-kit sync || echo ''",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
},
"dependencies": {
"@stylexjs/stylex": "0.17.5"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^7.0.0",
"@sveltejs/kit": "^2.49.1",
"@sveltejs/vite-plugin-svelte": "^6.2.1",
"svelte": "^5.45.6",
"svelte-check": "^4.3.4",
"typescript": "^5.9.3",
"vite": "^7.2.6",
"@stylexjs/unplugin": "0.17.5"
}
}
Empty file.
13 changes: 13 additions & 0 deletions examples/example-sveltekit/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// See https://svelte.dev/docs/kit/types#app.d.ts
// for information about these interfaces
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface PageState {}
// interface Platform {}
}
}

export {};
11 changes: 11 additions & 0 deletions examples/example-sveltekit/src/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
1 change: 1 addition & 0 deletions examples/example-sveltekit/src/lib/assets/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions examples/example-sveltekit/src/lib/attrs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type * as stylex from '@stylexjs/stylex';

/**
* Translates stylex.props to fit svelte attributes.
* @example
* <div {...attrs(stylex.props(...styles))} />
*/
export function attrs({ className, 'data-style-src': dataStyleSrc, style }: ReturnType<typeof stylex.props>) {
const result: {class?: string, style?: string, 'data-style-src'?: string} = {};
// Convert className to class
if (className != null && className !== '') {
result.class = className;
}
// Convert style object to string
if (style != null && Object.keys(style).length > 0) {
result.style = Object.keys(style)
.map((key) => `${key}:${style[key]};`)
.join('');
}
if (dataStyleSrc != null && dataStyleSrc !== '') {
result['data-style-src'] = dataStyleSrc;
}
return result;
}
3 changes: 3 additions & 0 deletions examples/example-sveltekit/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// place files you want to import through the `$lib` alias in this folder.

export * from "./attrs";
19 changes: 19 additions & 0 deletions examples/example-sveltekit/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script lang="ts">
import favicon from '$lib/assets/favicon.svg';
import "../app.css";
if (import.meta.env.DEV) {
// @ts-expect-error // Virtual modules are not typed
$effect(() => import('virtual:stylex:runtime'))
}

let { children } = $props();
</script>

<svelte:head>
<link rel="icon" href={favicon} />
{#if import.meta.env.DEV}
<link rel="stylesheet" href="/virtual:stylex.css" />
{/if}
</svelte:head>

{@render children()}
30 changes: 30 additions & 0 deletions examples/example-sveltekit/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<script lang="ts" module>
import * as stylex from '@stylexjs/stylex';
import { attrs } from '$lib';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see an attrs function in this PR? I'm guessing your lib directory got ignored because of the .gitignore in the root of the repo


const styles = stylex.create({
main: {
width: '100vw',
height: '100vh',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'lightblue',
},
card: {
backgroundColor: '#fefefe',
padding: '1rem',
borderRadius: 10,
justifyContent: 'center',
display: 'flex',
alignItems: 'center',
color: '#333',
fontFamily: 'Arial',
},
});
</script>


<div {...attrs(stylex.props(styles.main))}>
<div {...attrs(stylex.props(styles.card))}>Content</div>
</div>
3 changes: 3 additions & 0 deletions examples/example-sveltekit/static/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# allow crawling everything by default
User-agent: *
Disallow:
24 changes: 24 additions & 0 deletions examples/example-sveltekit/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://svelte.dev/docs/kit/integrations
// for more information about preprocessors
preprocess: vitePreprocess(),

kit: {
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
// See https://svelte.dev/docs/kit/adapters for more information about adapters.
adapter: adapter(),
},
};

export default config;
20 changes: 20 additions & 0 deletions examples/example-sveltekit/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"rewriteRelativeImportExtensions": true,
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "bundler"
}
// Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias
// except $lib which is handled by https://svelte.dev/docs/kit/configuration#files
//
// To make changes to top-level options such as include and exclude, we recommend extending
// the generated config; see https://svelte.dev/docs/kit/configuration#typescript
}
23 changes: 23 additions & 0 deletions examples/example-sveltekit/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import stylex from '@stylexjs/unplugin';

export default defineConfig({
plugins: [
sveltekit(),
// @ts-expect-error - ignore for now
{
...stylex.vite({
useCSSLayers: true,
enableFontSizePxToRem: true,
}),
enforce: undefined,
},
],
});
2 changes: 1 addition & 1 deletion packages/@stylexjs/unplugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ const unpluginInstance = createUnplugin((userOptions = {}, metaOptions) => {
// Core code transform
async transform(code, id) {
// Only handle JS-like files; avoid parsing CSS/JSON/etc
const JS_LIKE_RE = /\.[cm]?[jt]sx?(\?|$)/;
const JS_LIKE_RE = /\.([cm]?[jt]sx?|svelte)(\?|$)/;
if (!JS_LIKE_RE.test(id)) return null;
if (!shouldHandle(code)) return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"./vite-react.mdx",
"./vite-rsc.mdx",
"./react-router.mdx",
"./sveltekit.mdx",
"./redwoodsdk.mdx",
"./waku.mdx"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: 'SvelteKit'
---

[`examples/example-sveltekit`](https://github.com/facebook/stylex/tree/main/examples/example-sveltekit)
uses SvelteKit with StyleX compiled by `@stylexjs/unplugin`.

## Vite configuration

```ts title="vite.config.ts"
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import stylex from '@stylexjs/unplugin';

export default defineConfig({
plugins: [
sveltekit(),
{
...stylex.vite({
useCSSLayers: true,
}),
enforce: undefined,
},
],
});
```

## CSS entrypoint

Ensure that there is one CSS file that is imported from your root layout
component.

## CSS Link and Hot Reloading during development

Ensure that the virtual CSS file and script for hot reloading styles are part of
your bundle to enable hot reloading during development.

In your root `+layout.svelte` file, add the following to the script tag and `<svelte:head>`:

```svelte title="src/routes/+layout.svelte"
<script>
import "../app.css";
if (import.meta.env.DEV) {
$effect(() => import('virtual:stylex:runtime'))
}

let { children } = $props();
</script>

<svelte:head>
{#if import.meta.env.DEV}
<link rel="stylesheet" href="/virtual:stylex.css" />
{/if}
</svelte:head>

{@render children()}
```
Loading