Skip to content

Commit 7434f21

Browse files
authored
chore: configurable sandbox output (#17028)
1 parent 4a214f7 commit 7434f21

File tree

1 file changed

+58
-38
lines changed

1 file changed

+58
-38
lines changed

playgrounds/sandbox/run.js

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import { parseArgs } from 'node:util';
55
import { globSync } from 'tinyglobby';
66
import { compile, compileModule, parse, migrate } from 'svelte/compiler';
77

8+
// toggle these to change what gets written to sandbox/output
9+
const AST = false;
10+
const MIGRATE = false;
11+
const FROM_HTML = true;
12+
const FROM_TREE = false;
13+
const DEV = false;
14+
815
const argv = parseArgs({ options: { runes: { type: 'boolean' } }, args: process.argv.slice(2) });
916

1017
const cwd = fileURLToPath(new URL('.', import.meta.url)).slice(0, -1);
@@ -51,48 +58,52 @@ for (const generate of /** @type {const} */ (['client', 'server'])) {
5158
mkdirp(path.dirname(output_js));
5259

5360
if (generate === 'client') {
54-
const ast = parse(source, {
55-
modern: true
56-
});
61+
if (AST) {
62+
const ast = parse(source, {
63+
modern: true
64+
});
65+
66+
write(
67+
`${cwd}/output/ast/${file}.json`,
68+
JSON.stringify(
69+
ast,
70+
(key, value) => (typeof value === 'bigint' ? ['BigInt', value.toString()] : value),
71+
'\t'
72+
)
73+
);
74+
}
5775

58-
write(
59-
`${cwd}/output/ast/${file}.json`,
60-
JSON.stringify(
61-
ast,
62-
(key, value) => (typeof value === 'bigint' ? ['BigInt', value.toString()] : value),
63-
'\t'
64-
)
65-
);
66-
67-
try {
68-
const migrated = migrate(source);
69-
write(`${cwd}/output/migrated/${file}`, migrated.code);
70-
} catch (e) {
71-
console.warn(`Error migrating ${file}`, e);
76+
if (MIGRATE) {
77+
try {
78+
const migrated = migrate(source);
79+
write(`${cwd}/output/migrated/${file}`, migrated.code);
80+
} catch (e) {
81+
console.warn(`Error migrating ${file}`, e);
82+
}
7283
}
7384
}
7485

75-
const compiled = compile(source, {
76-
dev: false,
77-
filename: input,
78-
generate,
79-
runes: argv.values.runes,
80-
experimental: {
81-
async: true
82-
}
83-
});
86+
let from_html;
87+
let from_tree;
8488

85-
for (const warning of compiled.warnings) {
86-
console.warn(warning.code);
87-
console.warn(warning.frame);
88-
}
89+
if (generate === 'server' || FROM_HTML) {
90+
from_html = compile(source, {
91+
dev: DEV,
92+
filename: input,
93+
generate,
94+
runes: argv.values.runes,
95+
experimental: {
96+
async: true
97+
}
98+
});
8999

90-
write(output_js, compiled.js.code + '\n//# sourceMappingURL=' + path.basename(output_map));
91-
write(output_map, compiled.js.map.toString());
100+
write(output_js, from_html.js.code + '\n//# sourceMappingURL=' + path.basename(output_map));
101+
write(output_map, from_html.js.map.toString());
102+
}
92103

93104
// generate with fragments: 'tree'
94-
if (generate === 'client') {
95-
const compiled = compile(source, {
105+
if (generate === 'client' && FROM_TREE) {
106+
from_tree = compile(source, {
96107
dev: false,
97108
filename: input,
98109
generate,
@@ -106,12 +117,21 @@ for (const generate of /** @type {const} */ (['client', 'server'])) {
106117
const output_js = `${cwd}/output/${generate}/${file}.tree.js`;
107118
const output_map = `${cwd}/output/${generate}/${file}.tree.js.map`;
108119

109-
write(output_js, compiled.js.code + '\n//# sourceMappingURL=' + path.basename(output_map));
110-
write(output_map, compiled.js.map.toString());
120+
write(output_js, from_tree.js.code + '\n//# sourceMappingURL=' + path.basename(output_map));
121+
write(output_map, from_tree.js.map.toString());
111122
}
112123

113-
if (compiled.css) {
114-
write(output_css, compiled.css.code);
124+
const compiled = from_html ?? from_tree;
125+
126+
if (compiled) {
127+
for (const warning of compiled.warnings) {
128+
console.warn(warning.code);
129+
console.warn(warning.frame);
130+
}
131+
132+
if (compiled.css) {
133+
write(output_css, compiled.css.code);
134+
}
115135
}
116136
}
117137

0 commit comments

Comments
 (0)