chore: configurable sandbox output (#17028)

pull/16688/merge
Rich Harris 5 days ago committed by GitHub
parent 4a214f786f
commit 7434f21ed4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -5,6 +5,13 @@ import { parseArgs } from 'node:util';
import { globSync } from 'tinyglobby'; import { globSync } from 'tinyglobby';
import { compile, compileModule, parse, migrate } from 'svelte/compiler'; import { compile, compileModule, parse, migrate } from 'svelte/compiler';
// toggle these to change what gets written to sandbox/output
const AST = false;
const MIGRATE = false;
const FROM_HTML = true;
const FROM_TREE = false;
const DEV = false;
const argv = parseArgs({ options: { runes: { type: 'boolean' } }, args: process.argv.slice(2) }); const argv = parseArgs({ options: { runes: { type: 'boolean' } }, args: process.argv.slice(2) });
const cwd = fileURLToPath(new URL('.', import.meta.url)).slice(0, -1); const cwd = fileURLToPath(new URL('.', import.meta.url)).slice(0, -1);
@ -51,48 +58,52 @@ for (const generate of /** @type {const} */ (['client', 'server'])) {
mkdirp(path.dirname(output_js)); mkdirp(path.dirname(output_js));
if (generate === 'client') { if (generate === 'client') {
const ast = parse(source, { if (AST) {
modern: true const ast = parse(source, {
}); modern: true
});
write(
`${cwd}/output/ast/${file}.json`,
JSON.stringify(
ast,
(key, value) => (typeof value === 'bigint' ? ['BigInt', value.toString()] : value),
'\t'
)
);
}
write( if (MIGRATE) {
`${cwd}/output/ast/${file}.json`, try {
JSON.stringify( const migrated = migrate(source);
ast, write(`${cwd}/output/migrated/${file}`, migrated.code);
(key, value) => (typeof value === 'bigint' ? ['BigInt', value.toString()] : value), } catch (e) {
'\t' console.warn(`Error migrating ${file}`, e);
) }
);
try {
const migrated = migrate(source);
write(`${cwd}/output/migrated/${file}`, migrated.code);
} catch (e) {
console.warn(`Error migrating ${file}`, e);
} }
} }
const compiled = compile(source, { let from_html;
dev: false, let from_tree;
filename: input,
generate,
runes: argv.values.runes,
experimental: {
async: true
}
});
for (const warning of compiled.warnings) { if (generate === 'server' || FROM_HTML) {
console.warn(warning.code); from_html = compile(source, {
console.warn(warning.frame); dev: DEV,
} filename: input,
generate,
runes: argv.values.runes,
experimental: {
async: true
}
});
write(output_js, compiled.js.code + '\n//# sourceMappingURL=' + path.basename(output_map)); write(output_js, from_html.js.code + '\n//# sourceMappingURL=' + path.basename(output_map));
write(output_map, compiled.js.map.toString()); write(output_map, from_html.js.map.toString());
}
// generate with fragments: 'tree' // generate with fragments: 'tree'
if (generate === 'client') { if (generate === 'client' && FROM_TREE) {
const compiled = compile(source, { from_tree = compile(source, {
dev: false, dev: false,
filename: input, filename: input,
generate, generate,
@ -106,12 +117,21 @@ for (const generate of /** @type {const} */ (['client', 'server'])) {
const output_js = `${cwd}/output/${generate}/${file}.tree.js`; const output_js = `${cwd}/output/${generate}/${file}.tree.js`;
const output_map = `${cwd}/output/${generate}/${file}.tree.js.map`; const output_map = `${cwd}/output/${generate}/${file}.tree.js.map`;
write(output_js, compiled.js.code + '\n//# sourceMappingURL=' + path.basename(output_map)); write(output_js, from_tree.js.code + '\n//# sourceMappingURL=' + path.basename(output_map));
write(output_map, compiled.js.map.toString()); write(output_map, from_tree.js.map.toString());
} }
if (compiled.css) { const compiled = from_html ?? from_tree;
write(output_css, compiled.css.code);
if (compiled) {
for (const warning of compiled.warnings) {
console.warn(warning.code);
console.warn(warning.frame);
}
if (compiled.css) {
write(output_css, compiled.css.code);
}
} }
} }

Loading…
Cancel
Save