-
Notifications
You must be signed in to change notification settings - Fork 383
Add support for Svelte #1454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srikrsna
wants to merge
3
commits into
facebook:main
Choose a base branch
from
srikrsna:svelte
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add support for Svelte #1454
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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-* |
This file contains hidden or 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 @@ | ||
| engine-strict=true |
This file contains hidden or 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,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. |
This file contains hidden or 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,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.
This file contains hidden or 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,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 {}; |
This file contains hidden or 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,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> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,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; | ||
| } |
This file contains hidden or 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 @@ | ||
| // place files you want to import through the `$lib` alias in this folder. | ||
|
|
||
| export * from "./attrs"; |
This file contains hidden or 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,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()} |
This file contains hidden or 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,30 @@ | ||
| <script lang="ts" module> | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| import { attrs } from '$lib'; | ||
|
|
||
| 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> | ||
This file contains hidden or 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 @@ | ||
| # allow crawling everything by default | ||
| User-agent: * | ||
| Disallow: |
This file contains hidden or 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,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; |
This file contains hidden or 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,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 | ||
| } |
This file contains hidden or 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,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, | ||
| }, | ||
| ], | ||
| }); |
This file contains hidden or 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 hidden or 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
57 changes: 57 additions & 0 deletions
57
packages/docs/content/docs/learn/installation/vite/sveltekit.mdx
This file contains hidden or 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,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()} | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
attrsfunction in this PR? I'm guessing yourlibdirectory got ignored because of the.gitignorein the root of the repo