You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/playgrounds/sandbox/run.js

92 lines
2.4 KiB

import * as fs from 'node:fs';
import * as path from 'node:path';
import { fileURLToPath } from 'node:url';
import glob from 'tiny-glob/sync.js';
import minimist from 'minimist';
import { compile, compileModule, parse, migrate } from 'svelte/compiler';
const argv = minimist(process.argv.slice(2));
const cwd = fileURLToPath(new URL('.', import.meta.url)).slice(0, -1);
// empty output directory
if (fs.existsSync(`${cwd}/output`)) {
for (const file of fs.readdirSync(`${cwd}/output`)) {
if (file === '.gitkeep') continue;
try {
fs.rmSync(`${cwd}/output/${file}`, { recursive: true });
} catch {}
}
}
/** @param {string} dir */
function mkdirp(dir) {
try {
fs.mkdirSync(dir, { recursive: true });
} catch {}
}
const svelte_modules = glob('**/*.svelte', { cwd: `${cwd}/input` });
const js_modules = glob('**/*.js', { cwd: `${cwd}/input` });
for (const generate of /** @type {const} */ (['client', 'server'])) {
console.error(`\n--- generating ${generate} ---\n`);
for (const file of svelte_modules) {
const input = `${cwd}/input/${file}`;
const source = fs.readFileSync(input, 'utf-8');
const output_js = `${cwd}/output/${generate}/${file}.js`;
`$inspect` rune (#9705) * feat: add $log rune * fix issues * fix issues * tune * avoid static state reference validation * work around unfortunate browser behavior * call it ExpectedError * cleanup * Fix docs * tweaks * tweaks * lint * repl, dev: true * repl dev mode * Update sites/svelte-5-preview/src/lib/Repl.svelte * squelch static-state-reference warning * simplify * remove redundant code * Update packages/svelte/src/main/ambient.d.ts Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * Update packages/svelte/src/main/ambient.d.ts Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * Update packages/svelte/src/main/ambient.d.ts Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * only pause/trace on change * Update packages/svelte/src/main/ambient.d.ts * Update .changeset/chatty-hotels-grin.md * Update sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * $log.break and $log.trace no-op during SSR * Update sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * update test * improve break experience * fix ts * remove unnecessary if (DEV) checks - log runes are removed in prod * ensure hoisting doesnt mess up source maps * check visited for cyclical values * rename $log to $inspect, remove children * custom inspect function * implement custom inspect functions * changeset * update docs * only fire on change * lint * make inspect take a single argument * ugh eslint * document console.trace trick * demos * fix site --------- Co-authored-by: Dominic Gannaway <dg@domgan.com> Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com> Co-authored-by: Rich Harris <rich.harris@vercel.com> Co-authored-by: Dominic Gannaway <trueadm@users.noreply.github.com>
12 months ago
const output_map = `${cwd}/output/${generate}/${file}.js.map`;
const output_css = `${cwd}/output/${generate}/${file}.css`;
mkdirp(path.dirname(output_js));
if (generate === 'client') {
const ast = parse(source, {
modern: true
});
fs.writeFileSync(`${cwd}/output/${file}.json`, JSON.stringify(ast, null, '\t'));
try {
const migrated = migrate(source);
fs.writeFileSync(`${cwd}/output/${file}.migrated.svelte`, migrated.code);
} catch (e) {
console.warn(`Error migrating ${file}`, e);
}
}
const compiled = compile(source, {
`$inspect` rune (#9705) * feat: add $log rune * fix issues * fix issues * tune * avoid static state reference validation * work around unfortunate browser behavior * call it ExpectedError * cleanup * Fix docs * tweaks * tweaks * lint * repl, dev: true * repl dev mode * Update sites/svelte-5-preview/src/lib/Repl.svelte * squelch static-state-reference warning * simplify * remove redundant code * Update packages/svelte/src/main/ambient.d.ts Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * Update packages/svelte/src/main/ambient.d.ts Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * Update packages/svelte/src/main/ambient.d.ts Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * only pause/trace on change * Update packages/svelte/src/main/ambient.d.ts * Update .changeset/chatty-hotels-grin.md * Update sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * $log.break and $log.trace no-op during SSR * Update sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * update test * improve break experience * fix ts * remove unnecessary if (DEV) checks - log runes are removed in prod * ensure hoisting doesnt mess up source maps * check visited for cyclical values * rename $log to $inspect, remove children * custom inspect function * implement custom inspect functions * changeset * update docs * only fire on change * lint * make inspect take a single argument * ugh eslint * document console.trace trick * demos * fix site --------- Co-authored-by: Dominic Gannaway <dg@domgan.com> Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com> Co-authored-by: Rich Harris <rich.harris@vercel.com> Co-authored-by: Dominic Gannaway <trueadm@users.noreply.github.com>
12 months ago
dev: true,
filename: input,
generate,
runes: argv.runes
});
`$inspect` rune (#9705) * feat: add $log rune * fix issues * fix issues * tune * avoid static state reference validation * work around unfortunate browser behavior * call it ExpectedError * cleanup * Fix docs * tweaks * tweaks * lint * repl, dev: true * repl dev mode * Update sites/svelte-5-preview/src/lib/Repl.svelte * squelch static-state-reference warning * simplify * remove redundant code * Update packages/svelte/src/main/ambient.d.ts Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * Update packages/svelte/src/main/ambient.d.ts Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * Update packages/svelte/src/main/ambient.d.ts Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * only pause/trace on change * Update packages/svelte/src/main/ambient.d.ts * Update .changeset/chatty-hotels-grin.md * Update sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * $log.break and $log.trace no-op during SSR * Update sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * update test * improve break experience * fix ts * remove unnecessary if (DEV) checks - log runes are removed in prod * ensure hoisting doesnt mess up source maps * check visited for cyclical values * rename $log to $inspect, remove children * custom inspect function * implement custom inspect functions * changeset * update docs * only fire on change * lint * make inspect take a single argument * ugh eslint * document console.trace trick * demos * fix site --------- Co-authored-by: Dominic Gannaway <dg@domgan.com> Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com> Co-authored-by: Rich Harris <rich.harris@vercel.com> Co-authored-by: Dominic Gannaway <trueadm@users.noreply.github.com>
12 months ago
fs.writeFileSync(
output_js,
compiled.js.code + '\n//# sourceMappingURL=' + path.basename(output_map)
);
fs.writeFileSync(output_map, compiled.js.map.toString());
if (compiled.css) {
fs.writeFileSync(output_css, compiled.css.code);
}
}
for (const file of js_modules) {
const input = `${cwd}/input/${file}`;
const source = fs.readFileSync(input, 'utf-8');
const compiled = compileModule(source, {
dev: true,
filename: input,
generate
});
const output_js = `${cwd}/output/${generate}/${file}`;
mkdirp(path.dirname(output_js));
fs.writeFileSync(output_js, compiled.js.code);
}
}