Skip to content

Support Svelte #4

@sushichan044

Description

@sushichan044

Summary

Add first-class transformation for import.meta.* inside Svelte components. Handle both script blocks and template/expression contexts with correct offset mapping.

Motivation

  • Parity with JS/TS and Astro support.
  • Svelte apps may reference import.meta.* in scripts, reactive statements, and templates.

Scope

  • Parse Svelte files with the official compiler.
  • Transform in:
    • <script> (instance) and <script context="module"> (honor lang="ts").
    • Template expressions: mustache, attribute expressions, and directive expressions.

Important Caveat

Svelte has unique syntax and directives beyond plain HTML/JS:

  • Reactive statements/declarations: $: x = ..., $: { ... }.
  • Store shorthand: $store reads.
  • Block syntax: {#if}, {#each}, {#await}.
  • Directives: on:, bind:, class:, use:, as well as transitions/animations.
    We must only transform JavaScript expression substrings and keep offsets precise when applying replacements.

Non-Goals

  • Implementing a Svelte compiler.
  • Transforming raw HTML or string-only attributes.
  • Changing Svelte semantics; only replace import.meta.* with literal results.

Design Proposal

  • Parser: use svelte/compiler to obtain AST and node positions.
  • Scripts: run analyzeTypeScript on instance and module scripts.
  • Template: traverse expression-bearing nodes (mustache, attribute values, on:, bind:, class:, use: directives, and reactive statements). For each expression string, run analyzeTypeScript and map replacements back via node offsets.
  • Special cases: do not modify directive names/modifiers; operate only on expression content.

Implementation Plan

  1. Add src/core/languages/svelte.ts processor using svelte/compiler via tryImport.
  2. Extend detection in src/core/languages/index.ts to include .svelte.
  3. Add optional peer dep: svelte.
  4. Tests & fixtures under tests/fixtures/svelte/ covering:
    • Instance and module scripts (TS on/off)
    • { import.meta.FOO }, on:click={import.meta.fn()}
    • bind:value={import.meta.V}, class:active={import.meta.FLAG}
    • Reactive $: x = import.meta.V and $: { console.log(import.meta.V) }
  5. Docs: update README (Language Support) once merged.

Testing Strategy

  • Snapshot-based fixtures; ensure no-op when import.meta is absent.
  • Edge cases: store shorthand $store, nested block structures.

Risks

  • Offset mapping bugs; mitigate with helper utilities and thorough fixtures.
  • Compiler version drift; guard with tryImport and pinned peer deps.

Acceptance Criteria

  • .svelte files with import.meta.* are transformed in scripts and template expressions.
  • Unsupported contexts remain unchanged. All tests pass.

Open Questions

  • Should certain directives be ignored by default to reduce false positives?

How to Contribute

  • Comment to claim. Incremental PRs welcome.
  • Use AST node positions from the official compiler; avoid ad-hoc string parsing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions