From 4d14fc2d6d84aab88c3a7f05fff9e0748365b806 Mon Sep 17 00:00:00 2001 From: wires Date: Sun, 29 Mar 2026 20:36:40 -0400 Subject: [PATCH] Svelte MCP --- .cursor/mcp.json | 7 - .cursor/skills/svelte-code-writer/SKILL.md | 176 ++++++++++++++++++ .../skills/svelte-core-bestpractices/SKILL.md | 66 +++++++ .github/copilot-instructions.md | 3 - .github/publish.yaml | 50 ----- .github/workflows/.keep | 0 .gitignore | 1 + 7 files changed, 243 insertions(+), 60 deletions(-) delete mode 100644 .cursor/mcp.json create mode 100644 .cursor/skills/svelte-code-writer/SKILL.md create mode 100644 .cursor/skills/svelte-core-bestpractices/SKILL.md delete mode 100644 .github/copilot-instructions.md delete mode 100644 .github/publish.yaml delete mode 100644 .github/workflows/.keep diff --git a/.cursor/mcp.json b/.cursor/mcp.json deleted file mode 100644 index 751eff8..0000000 --- a/.cursor/mcp.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "mcpServers": { - "svelte": { - "url": "https://mcp.svelte.dev/mcp" - } - } -} diff --git a/.cursor/skills/svelte-code-writer/SKILL.md b/.cursor/skills/svelte-code-writer/SKILL.md new file mode 100644 index 0000000..341d23d --- /dev/null +++ b/.cursor/skills/svelte-code-writer/SKILL.md @@ -0,0 +1,176 @@ +--- +name: svelte-code-writer +description: CLI tools for Svelte 5 documentation lookup and code analysis. MUST be used whenever creating, editing or analyzing any Svelte component (.svelte) or Svelte module (.svelte.ts/.svelte.js). If possible, this skill should be executed within the svelte-file-editor agent for optimal results. +--- + +## `$state` + +Only use the `$state` rune for variables that should be _reactive_ — in other words, variables that cause an `$effect`, `$derived` or template expression to update. Everything else can be a normal variable. + +Objects and arrays (`$state({...})` or `$state([...])`) are made deeply reactive, meaning mutation will trigger updates. This has a trade-off: in exchange for fine-grained reactivity, the objects must be proxied, which has performance overhead. In cases where you're dealing with large objects that are only ever reassigned (rather than mutated), use `$state.raw` instead. This is often the case with API responses, for example. + +## `$derived` + +To compute something from state, use `$derived` rather than `$effect`: + +```js +// do this +let square = $derived(num * num); + +// don't do this +let square; + +$effect(() => { + square = num * num; +}); +``` + +> [!NOTE] `$derived` is given an expression, _not_ a function. If you need to use a function (because the expression is complex, for example) use `$derived.by`. + +Deriveds are writable — you can assign to them, just like `$state`, except that they will re-evaluate when their expression changes. + +If the derived expression is an object or array, it will be returned as-is — it is _not_ made deeply reactive. You can, however, use `$state` inside `$derived.by` in the rare cases that you need this. + +## `$effect` + +Effects are an escape hatch and should mostly be avoided. In particular, avoid updating state inside effects. + +- If you need to sync state to an external library such as D3, it is often neater to use [`{@attach ...}`](references/@attach.md) +- If you need to run some code in response to user interaction, put the code directly in an event handler or use a [function binding](references/bind.md) as appropriate +- If you need to log values for debugging purposes, use [`$inspect`](references/$inspect.md) +- If you need to observe something external to Svelte, use [`createSubscriber`](references/svelte-reactivity.md) + +Never wrap the contents of an effect in `if (browser) {...}` or similar — effects do not run on the server. + +## `$props` + +Treat props as though they will change. For example, values that depend on props should usually use `$derived`: + +```js +// @errors: 2451 +let { type } = $props(); + +// do this +let color = $derived(type === 'danger' ? 'red' : 'green'); + +// don't do this — `color` will not update if `type` changes +let color = type === 'danger' ? 'red' : 'green'; +``` + +## `$inspect.trace` + +`$inspect.trace` is a debugging tool for reactivity. If something is not updating properly or running more than it should you can add `$inspect.trace(label)` as the first line of an `$effect` or `$derived.by` (or any function they call) to trace their dependencies and discover which one triggered an update. + +## Events + +Any element attribute starting with `on` is treated as an event listener: + +```svelte + + + + + + + +``` + +If you need to attach listeners to `window` or `document` you can use `` and ``: + +```svelte + + +``` + +Avoid using `onMount` or `$effect` for this. + +## Snippets + +[Snippets](references/snippet.md) are a way to define reusable chunks of markup that can be instantiated with the [`{@render ...}`](references/@render.md) tag, or passed to components as props. They must be declared within the template. + +```svelte +{#snippet greeting(name)} +

hello {name}!

+{/snippet} + +{@render greeting('world')} +``` + +> [!NOTE] Snippets declared at the top level of a component (i.e. not inside elements or blocks) can be referenced inside `' + +# Analyze a file +npx @sveltejs/mcp svelte-autofixer ./src/lib/Component.svelte + +# Target Svelte 4 +npx @sveltejs/mcp svelte-autofixer ./Component.svelte --svelte-version 4 +``` + +**Important:** When passing code with runes (`$state`, `$derived`, etc.) via the terminal, escape the `$` character as `\$` to prevent shell variable substitution. + +## Workflow + +1. **Uncertain about syntax?** Run `list-sections` then `get-documentation` for relevant topics +2. **Reviewing/debugging?** Run `svelte-autofixer` on the code to detect issues +3. **Always validate** - Run `svelte-autofixer` before finalizing any Svelte component diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md deleted file mode 100644 index af2ec8c..0000000 --- a/.github/copilot-instructions.md +++ /dev/null @@ -1,3 +0,0 @@ -When talking about Svelte code, please always use Svelte 5 syntax in instructions and code samples unless explicitly asked to use Svelte 4 syntax. Refer to the Svelte llms.txt document at [bin/llms/svelte/llms-small.txt](../bin/llms/svelte/llms-small.txt) in this repo for details on Svelte 5 syntax. Also refer to the SvelteKit llms.txt document at [bin/llms/svelte-kit/llms-small.txt](../bin/llms/svelte-kit/llms-small.txt) in this repo for details on SvelteKit, the framework we use in this repo. - -Many components added to projects come from the `@reuters-graphics/graphics-components` library, which is our team's component library including many Svelte components and other utilities. Refer to the graphics components llms.txt document at [bin/llms/graphics-components/llms.txt](../bin/llms/graphics-components/llms.txt) in this repo for details on working with components from that library and especially for examples of our usual conventions for adding graphics components to a page. diff --git a/.github/publish.yaml b/.github/publish.yaml deleted file mode 100644 index e6f96f4..0000000 --- a/.github/publish.yaml +++ /dev/null @@ -1,50 +0,0 @@ -############################### -# To publish the project via GitHub Actions: -# - move this file into .github/workflows -# - uncomment the following lines and configure -############################### - -# name: Publish page - -# # These line define what will trigger your project to publish -# on: -# # ... when you hit the API endpoint for this repo -# # Read more: https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event -# workflow_dispatch: -# # ... whenever you push code to the master branch on GitHub -# push: -# branches: -# - master -# # ... on a cron schedule -# schedule: -# # Customize to whatever interval you need: -# # https://crontab.guru/ -# - cron: '35 * * * *' -# -# jobs: -# publish-page: -# name: Publish page -# runs-on: ubuntu-latest -# env: -# SERVER_WORKFLOW: true -# GRAPHICS_SERVER_USERNAME: ${{ secrets.GRAPHICS_SERVER_USERNAME }} -# GRAPHICS_SERVER_PASSWORD: ${{ secrets.GRAPHICS_SERVER_PASSWORD }} -# GRAPHICS_SERVER_API_KEY: ${{ secrets.GRAPHICS_SERVER_API_KEY }} -# SKIP_BUILD_GIT_COMMIT: true -# GRAPHICS_SERVER_PUBLISH: true -# # This line will notify a Teams channel everytime your project successfully publishes -# # GRAPHICS_SERVER_NOTIFY_TEAMS_CHANNEL: # Add a Teams webhook URL here -# steps: -# - uses: actions/checkout@v4 -# - uses: pnpm/action-setup@v4 -# with: -# version: 9 -# - uses: actions/setup-node@v4 -# with: -# node-version: 20 -# cache: pnpm -# - run: git config user.name github-actions -# - run: git config user.email github-actions@github.com -# - run: pnpm install -# - run: pnpm upload -# - run: pnpm publish:publish diff --git a/.github/workflows/.keep b/.github/workflows/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/.gitignore b/.gitignore index 385b77f..61d77c6 100644 --- a/.gitignore +++ b/.gitignore @@ -200,3 +200,4 @@ graphics-pack/ project-files/docs-ai-ps/ src/statics/images/docs-ai-ps .lefthook-local/ +.pnpm-store