Skip to content

Commit

Permalink
chore: move preprocessors to src-build/
Browse files Browse the repository at this point in the history
  • Loading branch information
eye-wave committed Nov 2, 2024
1 parent 5c268a7 commit 5c1f3cf
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 45 deletions.
54 changes: 54 additions & 0 deletions src-build/print-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { createHighlighter } from 'shiki';
import * as fs from 'node:fs';
import { resolvePath } from './resolve-alias.js';

const theme = 'one-dark-pro';

const highlighter = await createHighlighter({
langs: ['svelte'],
themes: [theme]
});

/**
* @param {string} code
* @returns {string}
*/
function highlight(code) {
const html = highlighter.codeToHtml(code, {
lang: 'svelte',
theme
});

return html;
}

/**
* @returns {import("svelte/compiler").PreprocessorGroup}
*/
const printComponent = () => {
return {
name: 'print-self',
markup({ content, filename }) {
const replacement = content.replace(/%import\('([^']+)'\)%/g, (match, alias) => {
try {
if (!filename) throw Error('filename is undefined');

const compPath = resolvePath(filename, alias);
const content = fs.readFileSync(compPath, 'utf-8').replace('$lib', 'svelte-knobs').trim();
const html = highlight(content);

return `{@html \`${html}\`}`;
} catch (err) {
console.error(err)
return match;
}
});

return {
code: replacement
};
}
};
};

export default printComponent;
35 changes: 35 additions & 0 deletions src-build/resolve-alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as path from 'node:path';

const aliasMap = {
$lib: 'src/lib'
};

/**
* @param {string} pth
* @returns {string}
*/
function resolveAlias(pth) {
const entries = Object.entries(aliasMap);
for (const [alias, path] of entries) {
if (!pth.includes(alias)) continue;
return pth.replace(alias, path);
}

return pth;
}

/**
* @param {string} filename
* @param {string} pth
* @returns {string}
*/
export function resolvePath(filename, pth) {
if (pth.startsWith('./') || pth.startsWith('../')) {
const currentDir = path.dirname(filename);
const compPath = path.join(currentDir, pth);

return compPath;
}

return resolveAlias(pth);
}
47 changes: 2 additions & 45 deletions svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,6 @@
import adapter from '@sveltejs/adapter-static';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
import { createHighlighter } from 'shiki';
import * as path from 'node:path';
import * as fs from 'node:fs';

const theme = 'one-dark-pro';

const highlighter = await createHighlighter({
langs: ['svelte'],
themes: [theme]
});

function highlight(code) {
const html = highlighter.codeToHtml(code, {
lang: 'svelte',
theme
});

return html;
}

function printComponent() {
return {
name: 'print-self',
markup({ content, filename }) {
const replacement = content.replace(/%import\('([^']+)'\)%/g, (match, pth) => {
const currentDir = path.dirname(filename);
const compPath = path.join(currentDir, pth);

try {
const content = fs.readFileSync(compPath, 'utf-8').replace('$lib', 'svelte-knobs');
const html = highlight(content);

return `{@html \`${html}\`}`;
} catch {
return match;
}
});

return {
code: replacement
};
}
};
}
import adapter from '@sveltejs/adapter-static';
import printComponent from './src-build/print-component.js';

/** @type {import('@sveltejs/kit').Config} */
const config = {
Expand Down

0 comments on commit 5c1f3cf

Please sign in to comment.