core-ast-ts-plugin-svelte
v0.1.0
Published
Svelte 5 component parser plugin for core-ast-ts — parses .svelte files using svelte/compiler and produces ESTree AST
Maintainers
Readme
core-ast-ts-plugin-svelte
Svelte 5 component parser plugin for core-ast-ts.
Parses .svelte files using svelte/compiler and produces an ESTree AST from the instance script block, with the full Svelte AST attached as meta.
Install
npm install core-ast-ts-plugin-svelteUsage
import { coreAst } from 'core-ast-ts'
import svelte from 'core-ast-ts-plugin-svelte'
const ast = coreAst({ plugins: [svelte()] })
const { ast: program, meta } = ast.get('App.svelte', source)
// meta.svelteAst — full Svelte AST from svelte/compiler
// meta.scriptContent — extracted instance script source
// meta.moduleScriptContent — extracted module script source
// meta.componentName — derived from filename (e.g. 'App')
// meta.hasInstanceScript — whether <script> exists
// meta.hasModuleScript — whether <script context="module"> exists
// meta.lang — script language (e.g. 'ts', 'js')Options
import svelte from 'core-ast-ts-plugin-svelte'
const ast = coreAst({
plugins: [svelte({
modern: true, // use Svelte 5 modern AST (default: true)
})],
})| Option | Default | Description |
|--------|---------|-------------|
| modern | true | Use Svelte 5 modern AST format |
How it works
- Calls
svelte/compilerparse()to get the Svelte AST - Extracts the
<script>(instance) and<script context="module">blocks - Uses the ESTree Program from the instance script directly (Svelte's AST includes it), or falls back to parsing with
core-ast-tscore parser - Returns
{ ast, source, meta }wheremetacontains the Svelte AST and derived info
This keeps svelte/compiler as a dependency of this plugin only — core-ast-ts core stays lightweight.
License
MIT
