|
| 1 | +// @ts-check |
| 2 | +import { readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { create } from 'sv'; |
| 6 | + |
| 7 | +// This download the currente Vite template from Github, adjusts it to our needs, and saves it to static/svelte-template.json |
| 8 | +// This is used by the Svelte REPL as part of the "download project" feature |
| 9 | + |
| 10 | +const viteConfigTailwind = |
| 11 | + "import { sveltekit } from '@sveltejs/kit/vite';\nimport { defineConfig } from 'vite';\nimport tailwindcss from '@tailwindcss/vite'\nexport default defineConfig({\n\tplugins: [sveltekit(),tailwindcss()]\n});\n"; |
| 12 | + |
| 13 | +const force = process.env.FORCE_UPDATE === 'true'; |
| 14 | +const output_file = fileURLToPath( |
| 15 | + new URL('../static/svelte-tailwind-template.json', import.meta.url) |
| 16 | +); |
| 17 | +const output_dir = fileURLToPath(new URL('./svelte-tailwind-template', import.meta.url)); |
| 18 | + |
| 19 | +try { |
| 20 | + if (!force && statSync(output_file)) { |
| 21 | + console.info(`[update/template] ${output_file} exists. Skipping`); |
| 22 | + process.exit(0); |
| 23 | + } |
| 24 | +} catch { |
| 25 | + // create Svelte-Kit skelton app |
| 26 | + create(output_dir, { template: 'minimal', types: 'typescript', name: 'your-app' }); |
| 27 | + |
| 28 | + function get_all_files(dir) { |
| 29 | + const files = []; |
| 30 | + const items = readdirSync(dir, { withFileTypes: true }); |
| 31 | + |
| 32 | + for (const item of items) { |
| 33 | + const full_path = join(dir, item.name); |
| 34 | + if (item.isDirectory()) { |
| 35 | + files.push(...get_all_files(full_path)); |
| 36 | + } else { |
| 37 | + files.push(full_path.replaceAll('\\', '/')); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return files; |
| 42 | + } |
| 43 | + |
| 44 | + const all_files = get_all_files(output_dir); |
| 45 | + const files = []; |
| 46 | + |
| 47 | + for (let path of all_files) { |
| 48 | + const bytes = readFileSync(path); |
| 49 | + const string = bytes.toString(); |
| 50 | + let data = bytes.compare(Buffer.from(string)) === 0 ? string : [...bytes]; |
| 51 | + |
| 52 | + // vite config to use along with Tailwind CSS |
| 53 | + if (path.endsWith('vite.config.ts')) { |
| 54 | + files.push({ |
| 55 | + path: 'vite.config.ts', |
| 56 | + data: viteConfigTailwind |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + // add Tailwind CSS as devDependencies |
| 61 | + if (path.endsWith('package.json')) { |
| 62 | + try { |
| 63 | + const pkg = JSON.parse(string); |
| 64 | + |
| 65 | + pkg.devDependencies ||= {}; |
| 66 | + pkg.devDependencies['tailwindcss'] = '^4.1.8'; |
| 67 | + pkg.devDependencies['@tailwindcss/vite'] = '^4.1.8'; |
| 68 | + |
| 69 | + data = JSON.stringify(pkg, null, 2); // Pretty-print with 2 spaces |
| 70 | + } catch (err) { |
| 71 | + console.error('Failed to parse package.json:', err); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (path.endsWith('routes/+page.svelte')) { |
| 76 | + data = `<script>\n\timport '../app.css';\n\timport App from './App.svelte';\n</script>\n\n<App />\n`; |
| 77 | + } |
| 78 | + |
| 79 | + files.push({ path: path.slice(output_dir.length + 1), data }); |
| 80 | + } |
| 81 | + |
| 82 | + files.push({ |
| 83 | + path: 'src/routes/+page.js', |
| 84 | + data: |
| 85 | + "// Because we don't know whether or not your playground app can run in a server environment, we disable server-side rendering.\n" + |
| 86 | + '// Make sure to test whether or not you can re-enable it, as SSR improves perceived performance and site accessibility.\n' + |
| 87 | + '// Read more about this option here: https://svelte.dev/docs/kit/page-options#ssr\n' + |
| 88 | + 'export const ssr = false;\n' |
| 89 | + }); |
| 90 | + |
| 91 | + // add CSS styles from playground to the project |
| 92 | + |
| 93 | + files.push({ |
| 94 | + path: 'src/app.css', |
| 95 | + data: '@import "tailwindcss";' |
| 96 | + }); |
| 97 | + |
| 98 | + writeFileSync(output_file, JSON.stringify(files)); |
| 99 | + |
| 100 | + // remove output dir afterwards to prevent it messing with Vite watcher |
| 101 | + rmSync(output_dir, { force: true, recursive: true }); |
| 102 | +} |
0 commit comments