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
- Add
src/core/languages/svelte.ts processor using svelte/compiler via tryImport.
- Extend detection in
src/core/languages/index.ts to include .svelte.
- Add optional peer dep:
svelte.
- 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) }
- 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.
Summary
Add first-class transformation for
import.meta.*inside Svelte components. Handle both script blocks and template/expression contexts with correct offset mapping.Motivation
import.meta.*in scripts, reactive statements, and templates.Scope
<script>(instance) and<script context="module">(honorlang="ts").Important Caveat
Svelte has unique syntax and directives beyond plain HTML/JS:
$: x = ...,$: { ... }.$storereads.{#if},{#each},{#await}.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
import.meta.*with literal results.Design Proposal
svelte/compilerto obtain AST and node positions.analyzeTypeScripton instance and module scripts.on:,bind:,class:,use:directives, and reactive statements). For each expression string, runanalyzeTypeScriptand map replacements back via node offsets.Implementation Plan
src/core/languages/svelte.tsprocessor usingsvelte/compilerviatryImport.src/core/languages/index.tsto include.svelte.svelte.tests/fixtures/svelte/covering:{ import.meta.FOO },on:click={import.meta.fn()}bind:value={import.meta.V},class:active={import.meta.FLAG}$: x = import.meta.Vand$: { console.log(import.meta.V) }Testing Strategy
import.metais absent.$store, nested block structures.Risks
tryImportand pinned peer deps.Acceptance Criteria
.sveltefiles withimport.meta.*are transformed in scripts and template expressions.Open Questions
How to Contribute