Skip to content

Commit

Permalink
fix(demo): add example paths at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
davidenke committed Dec 14, 2024
1 parent c6d6339 commit be6c1ab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/demo/demo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="vite/client" />

import hljs from 'highlight.js';
import json from 'highlight.js/lib/languages/json';
import ts from 'highlight.js/lib/languages/typescript';
Expand Down Expand Up @@ -87,15 +85,13 @@ window.updateInput = event => {

// lists all examples from the `examples` folder
window.initExamples = async () => {
const examples = Object.keys(import.meta.glob('/public/examples/*.yml'));
document.querySelector('#input > nav')!.innerHTML = examples
.map(path => {
const [, , folder, name] = path.split('/');
return `
<button data-example="${`${folder}/${name}`}" onclick="window.handleClick(event)">
${name.replace(/\.yml$/, '')} example
</button>`;
})
document.querySelector('#input > nav')!.innerHTML = __EXAMPLES__
.map(
({ href, name }) => `
<button data-example="${href}" onclick="window.handleClick(event)">
${name} example
</button>`,
)
.join('');
};

Expand Down
3 changes: 3 additions & 0 deletions vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// <reference types="vite/client" />

declare const __EXAMPLES__: { href: string; name: string }[];
11 changes: 10 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync } from 'node:fs';
import { readdirSync, readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';

import { defineConfig, type Plugin } from 'vite';
Expand Down Expand Up @@ -35,4 +35,13 @@ export default defineConfig(async () => ({
],
// https://github.com/davidmyersdev/vite-plugin-node-polyfills/issues/25#issuecomment-1962228168
resolve: { alias: { 'node:fs/promises': 'node-stdlib-browser/mock/empty' } },
// defined available examples
define: {
__EXAMPLES__: JSON.stringify(
readdirSync('public/examples').map(file => ({
href: `examples/${file}`,
name: file.replace(/\.yml$/, ''),
})),
),
},
}));

0 comments on commit be6c1ab

Please sign in to comment.