Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kaisermann/svelte-markup-walker

A Svelte preprocessor that allows to walk through the AST generated by the svelte parser and modify the markup with MagicString.
https://github.com/kaisermann/svelte-markup-walker

ast markup preprocess svelte walk

Last synced: 11 days ago
JSON representation

A Svelte preprocessor that allows to walk through the AST generated by the svelte parser and modify the markup with MagicString.

Awesome Lists containing this project

README

        

# Svelte Markup Walker

> A [Svelte](https://svelte.dev) preprocessor that allows to walk through the AST generated by the svelte parser and modify the markup with [MagicString](https://github.com/Rich-Harris/magic-string).

This library is a simple wrapper of svelte's exported `parse` and `walk` methods. Its default method accepts an option object of AST walker wrappers:

- `html({ content, filename })` for walking through the html AST;
- `instance({ content, filename })` for walking through the main `script` AST;
- `module({ content, filename })` for walking through the `context="module"` script AST;
- `css({ content, filename })` for walking through the `style` AST.

Each wrapper receives as arguments the current `filename` and the file `content` as a [MagicString](https://github.com/Rich-Harris/magic-string), which allows to easily modify/cut/overwrite/replace/etc its text.

```js
const markupWalker = require('svelte-markup-walker');

/**
* `preprocessor` is a svelte preprocessor,
* just add it to your `preprocess` array
*/
const preprocessor = markupWalker({
html({ content, filename }) {
return {
enter(node) { ... },
leave(node) { ... },
};
},
instance({ content, filename }) {
return {
enter(node) { ... },
leave(node) { ... },
};
},
module({ content, filename }) {
return {
enter(node) { ... },
leave(node) { ... },
};
},
css({ content, filename }) {
return {
enter(node) { ... },
leave(node) { ... },
};
},
});
```

## Example

> TODO