Files
Modrinth-plus/docs/plugins/sveld.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

import { ComponentParser } from 'sveld'
import * as svelte from 'svelte/compiler'
import fs from 'fs/promises'
import path from 'path'
import { preprocess } from '../../src/config/svelte.js'
export default function sveld() {
return {
name: 'vite-plugin-sveld',
2022-07-16 16:15:06 -07:00
// This generates a `COMPONENT_API.json` with sveld in the `/_app` folder which is used by the docs about components
// TODO: Make more efficient & handle typescript types with `svelte2tsx`
async transform(src, id) {
2022-07-16 16:15:06 -07:00
if (id.includes('/src/components/')) {
const output = {}
2022-07-16 16:15:06 -07:00
const componentFiles = await fs.readdir(path.resolve('./src/components'))
2022-07-16 16:15:06 -07:00
for (const fileName of componentFiles.filter((name) => name.endsWith('.svelte'))) {
const filePath = path.resolve('./src/components', fileName)
const raw = (await fs.readFile(filePath)).toString()
output[fileName] = await parseRaw(raw, filePath)
}
2022-07-16 16:15:06 -07:00
try {
await fs.mkdir(path.resolve('./generated'))
} catch {
// Do nothing, directory already exists
}
2022-07-16 16:15:06 -07:00
await fs.writeFile(path.resolve('./generated/COMPONENT_API.json'), JSON.stringify(output))
}
},
}
}
async function parseRaw(raw, filePath) {
let { code } = await svelte.preprocess(raw, preprocess, {
filename: filePath,
})
return new ComponentParser({
verbose: false,
}).parseSvelteComponent(code, {
filePath,
moduleName: filePath,
})
2022-05-20 00:10:45 -07:00
}