Skip to content

Commit 5c1f3cf

Browse files
committed
chore: move preprocessors to src-build/
1 parent 5c268a7 commit 5c1f3cf

File tree

3 files changed

+91
-45
lines changed

3 files changed

+91
-45
lines changed

src-build/print-component.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { createHighlighter } from 'shiki';
2+
import * as fs from 'node:fs';
3+
import { resolvePath } from './resolve-alias.js';
4+
5+
const theme = 'one-dark-pro';
6+
7+
const highlighter = await createHighlighter({
8+
langs: ['svelte'],
9+
themes: [theme]
10+
});
11+
12+
/**
13+
* @param {string} code
14+
* @returns {string}
15+
*/
16+
function highlight(code) {
17+
const html = highlighter.codeToHtml(code, {
18+
lang: 'svelte',
19+
theme
20+
});
21+
22+
return html;
23+
}
24+
25+
/**
26+
* @returns {import("svelte/compiler").PreprocessorGroup}
27+
*/
28+
const printComponent = () => {
29+
return {
30+
name: 'print-self',
31+
markup({ content, filename }) {
32+
const replacement = content.replace(/%import\('([^']+)'\)%/g, (match, alias) => {
33+
try {
34+
if (!filename) throw Error('filename is undefined');
35+
36+
const compPath = resolvePath(filename, alias);
37+
const content = fs.readFileSync(compPath, 'utf-8').replace('$lib', 'svelte-knobs').trim();
38+
const html = highlight(content);
39+
40+
return `{@html \`${html}\`}`;
41+
} catch (err) {
42+
console.error(err)
43+
return match;
44+
}
45+
});
46+
47+
return {
48+
code: replacement
49+
};
50+
}
51+
};
52+
};
53+
54+
export default printComponent;

src-build/resolve-alias.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as path from 'node:path';
2+
3+
const aliasMap = {
4+
$lib: 'src/lib'
5+
};
6+
7+
/**
8+
* @param {string} pth
9+
* @returns {string}
10+
*/
11+
function resolveAlias(pth) {
12+
const entries = Object.entries(aliasMap);
13+
for (const [alias, path] of entries) {
14+
if (!pth.includes(alias)) continue;
15+
return pth.replace(alias, path);
16+
}
17+
18+
return pth;
19+
}
20+
21+
/**
22+
* @param {string} filename
23+
* @param {string} pth
24+
* @returns {string}
25+
*/
26+
export function resolvePath(filename, pth) {
27+
if (pth.startsWith('./') || pth.startsWith('../')) {
28+
const currentDir = path.dirname(filename);
29+
const compPath = path.join(currentDir, pth);
30+
31+
return compPath;
32+
}
33+
34+
return resolveAlias(pth);
35+
}

svelte.config.js

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,6 @@
1-
import adapter from '@sveltejs/adapter-static';
21
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
3-
import { createHighlighter } from 'shiki';
4-
import * as path from 'node:path';
5-
import * as fs from 'node:fs';
6-
7-
const theme = 'one-dark-pro';
8-
9-
const highlighter = await createHighlighter({
10-
langs: ['svelte'],
11-
themes: [theme]
12-
});
13-
14-
function highlight(code) {
15-
const html = highlighter.codeToHtml(code, {
16-
lang: 'svelte',
17-
theme
18-
});
19-
20-
return html;
21-
}
22-
23-
function printComponent() {
24-
return {
25-
name: 'print-self',
26-
markup({ content, filename }) {
27-
const replacement = content.replace(/%import\('([^']+)'\)%/g, (match, pth) => {
28-
const currentDir = path.dirname(filename);
29-
const compPath = path.join(currentDir, pth);
30-
31-
try {
32-
const content = fs.readFileSync(compPath, 'utf-8').replace('$lib', 'svelte-knobs');
33-
const html = highlight(content);
34-
35-
return `{@html \`${html}\`}`;
36-
} catch {
37-
return match;
38-
}
39-
});
40-
41-
return {
42-
code: replacement
43-
};
44-
}
45-
};
46-
}
2+
import adapter from '@sveltejs/adapter-static';
3+
import printComponent from './src-build/print-component.js';
474

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

0 commit comments

Comments
 (0)