svelte-markup-walker
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

Svelte Markup Walker

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

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, which allows to easily modify/cut/overwrite/replace/etc its text.

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) { ... },
    };
  },
});

Readme

Keywords

Package Sidebar

Install

npm i svelte-markup-walker

Weekly Downloads

2

Version

0.0.2

License

MIT

Unpacked Size

4.78 kB

Total Files

4

Last publish

Collaborators

  • kaisermann