Skip to content

Commit cb8d99c

Browse files
committed
add sandbox run.js script to playgrounds/demo
1 parent c5c54da commit cb8d99c

File tree

4 files changed

+106
-5
lines changed

4 files changed

+106
-5
lines changed

playgrounds/demo/.gitignore

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
src/*
2-
dist/client/*
3-
dist/server/*
4-
!src/entry-client.ts
5-
!src/entry-server.ts
1+
/dist/client/*
2+
/dist/server/*
3+
/output
4+
/src/*

playgrounds/demo/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"nodemon": "^3.0.3",
1717
"polka": "^1.0.0-next.25",
1818
"svelte": "workspace:*",
19+
"tiny-glob": "^0.2.9",
1920
"vite": "^5.0.13",
2021
"vite-plugin-inspect": "^0.8.4"
2122
}

playgrounds/demo/run.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import * as fs from 'node:fs';
2+
import * as path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
import { parseArgs } from 'node:util';
5+
import glob from 'tiny-glob/sync.js';
6+
import { compile, compileModule, parse, migrate } from 'svelte/compiler';
7+
8+
const argv = parseArgs({ options: { runes: { type: 'boolean' } }, args: process.argv.slice(2) });
9+
10+
const cwd = fileURLToPath(new URL('.', import.meta.url)).slice(0, -1);
11+
12+
// empty output directory
13+
if (fs.existsSync(`${cwd}/output`)) {
14+
for (const file of fs.readdirSync(`${cwd}/output`)) {
15+
if (file === '.gitkeep') continue;
16+
try {
17+
fs.rmSync(`${cwd}/output/${file}`, { recursive: true });
18+
} catch {}
19+
}
20+
}
21+
22+
/** @param {string} dir */
23+
function mkdirp(dir) {
24+
try {
25+
fs.mkdirSync(dir, { recursive: true });
26+
} catch {}
27+
}
28+
29+
const svelte_modules = glob('**/*.svelte', { cwd: `${cwd}/src` });
30+
const js_modules = glob('**/*.js', { cwd: `${cwd}/src` });
31+
32+
for (const generate of /** @type {const} */ (['client', 'server'])) {
33+
console.error(`\n--- generating ${generate} ---\n`);
34+
for (const file of svelte_modules) {
35+
const input = `${cwd}/src/${file}`;
36+
const source = fs.readFileSync(input, 'utf-8');
37+
38+
const output_js = `${cwd}/output/${generate}/${file}.js`;
39+
const output_map = `${cwd}/output/${generate}/${file}.js.map`;
40+
const output_css = `${cwd}/output/${generate}/${file}.css`;
41+
42+
mkdirp(path.dirname(output_js));
43+
44+
if (generate === 'client') {
45+
const ast = parse(source, {
46+
modern: true
47+
});
48+
49+
fs.writeFileSync(`${cwd}/output/${file}.json`, JSON.stringify(ast, null, '\t'));
50+
51+
try {
52+
const migrated = migrate(source);
53+
fs.writeFileSync(`${cwd}/output/${file}.migrated.svelte`, migrated.code);
54+
} catch (e) {
55+
console.warn(`Error migrating ${file}`, e);
56+
}
57+
}
58+
59+
const compiled = compile(source, {
60+
dev: true,
61+
filename: input,
62+
generate,
63+
runes: argv.values.runes
64+
});
65+
66+
for (const warning of compiled.warnings) {
67+
console.warn(warning.code);
68+
console.warn(warning.frame);
69+
}
70+
71+
fs.writeFileSync(
72+
output_js,
73+
compiled.js.code + '\n//# sourceMappingURL=' + path.basename(output_map)
74+
);
75+
76+
fs.writeFileSync(output_map, compiled.js.map.toString());
77+
78+
if (compiled.css) {
79+
fs.writeFileSync(output_css, compiled.css.code);
80+
}
81+
}
82+
83+
for (const file of js_modules) {
84+
const input = `${cwd}/src/${file}`;
85+
const source = fs.readFileSync(input, 'utf-8');
86+
87+
const compiled = compileModule(source, {
88+
dev: true,
89+
filename: input,
90+
generate
91+
});
92+
93+
const output_js = `${cwd}/output/${generate}/${file}`;
94+
95+
mkdirp(path.dirname(output_js));
96+
fs.writeFileSync(output_js, compiled.js.code);
97+
}
98+
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)