Skip to content

Instantly share code, notes, and snippets.

@stevekinney
Created August 22, 2023 12:11
Show Gist options
  • Save stevekinney/ea6975f27509850baf995e5afdc685e7 to your computer and use it in GitHub Desktop.
Save stevekinney/ea6975f27509850baf995e5afdc685e7 to your computer and use it in GitHub Desktop.
import { readFile } from 'fs/promises';
import { parse, preprocess, walk } from 'svelte/compiler';
import autoprefixer from 'autoprefixer';
import type { AcceptedPlugin } from 'postcss';
import sveltePreprocess from 'svelte-preprocess';
import tailwind from 'tailwindcss';
import nesting from 'tailwindcss/nesting';
import { getProjectRoot } from '../get-project-root';
const opts = sveltePreprocess({
postcss: {
configFilePath: getProjectRoot() + '/postcss.config.js',
plugins: [tailwind, nesting as AcceptedPlugin, autoprefixer],
},
typescript: {
reportDiagnostics: false,
tsconfigFile: false,
compilerOptions: { module: 'es2015' },
},
globalStyle: true,
});
export async function parseSvelteComponent(filename: string) {
const content = await readFile(filename, 'utf-8');
const processed = await preprocess(content, opts, { filename });
const ast = parse(processed.code, { filename });
walk(ast.html, {
enter(node) {
if (node.type === 'Element') {
console.log(node);
}
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment