|
| 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 | +} |
0 commit comments