11// @ts -check
2- import { execSync } from 'node:child_process' ;
3- import {
4- copyFileSync ,
5- mkdirSync ,
6- readdirSync ,
7- readFileSync ,
8- rmSync ,
9- statSync ,
10- writeFileSync
11- } from 'node:fs' ;
2+ import { readdirSync , readFileSync , rmSync , statSync , writeFileSync } from 'node:fs' ;
123import { join } from 'node:path' ;
134import { fileURLToPath } from 'node:url' ;
5+ import { create } from 'sv' ;
146
157// This download the currente Vite template from Github, adjusts it to our needs, and saves it to static/svelte-template.json
168// This is used by the Svelte REPL as part of the "download project" feature
2517 process . exit ( 0 ) ;
2618 }
2719} catch {
28- // fetch svelte app
29- rmSync ( output_dir , { force : true , recursive : true } ) ;
30- execSync ( `npx degit vitejs/vite/packages/create-vite/template-svelte-ts ${ output_dir } ` , {
31- stdio : 'inherit'
32- } ) ;
33-
34- // remove everything that's not needed
35- rmSync ( join ( output_dir , 'src/assets' ) , { force : true , recursive : true } ) ;
36- rmSync ( join ( output_dir , 'src/lib' ) , { force : true , recursive : true } ) ;
37- rmSync ( join ( output_dir , 'src/app.css' ) , { force : true , recursive : true } ) ;
38- rmSync ( join ( output_dir , 'src/App.svelte' ) , { force : true , recursive : true } ) ;
39- rmSync ( join ( output_dir , 'src/main.ts' ) , { force : true , recursive : true } ) ;
40- rmSync ( join ( output_dir , 'public' ) , { force : true , recursive : true } ) ;
41-
42- // add what we need
43- mkdirSync ( join ( output_dir , 'public' ) ) ;
44- copyFileSync (
45- fileURLToPath ( new URL ( '../static/favicon.png' ) ) ,
46- join ( output_dir , 'public/favicon.png' )
47- ) ;
48-
49- // build svelte-app.json
50- const files = [ ] ;
20+ // create Svelte-Kit skelton app
21+ create ( output_dir , { template : 'minimal' , types : 'typescript' , name : 'your-app' } ) ;
5122
5223 function get_all_files ( dir ) {
5324 const files = [ ] ;
@@ -58,34 +29,37 @@ try {
5829 if ( item . isDirectory ( ) ) {
5930 files . push ( ...get_all_files ( full_path ) ) ;
6031 } else {
61- files . push ( full_path ) ;
32+ files . push ( full_path . replaceAll ( '\\' , '/' ) ) ;
6233 }
6334 }
6435
6536 return files ;
6637 }
6738
6839 const all_files = get_all_files ( output_dir ) ;
40+ const files = [ ] ;
6941
7042 for ( let path of all_files ) {
7143 const bytes = readFileSync ( path ) ;
7244 const string = bytes . toString ( ) ;
7345 let data = bytes . compare ( Buffer . from ( string ) ) === 0 ? string : [ ...bytes ] ;
7446
75- // handle some special cases
76- path = path . slice ( output_dir . length + 1 ) ;
77- if ( path . endsWith ( '_gitignore' ) ) path = path . slice ( 0 , - 10 ) + '.gitignore' ;
78-
79- if ( path . endsWith ( 'index.html' ) ) {
80- data = /** @type {any } */ ( data ) . replace (
81- '<link rel="icon" type="image/svg+xml" href="/vite.svg" />' ,
82- '<link rel="icon" type="image/png" href="/favicon.png" />'
83- ) ;
47+ if ( path . endsWith ( 'routes/+page.svelte' ) ) {
48+ data = `<script>\nimport App from './App.svelte';\n</script>\n\n<App />\n` ;
8449 }
8550
86- files . push ( { path, data } ) ;
51+ files . push ( { path : path . slice ( output_dir . length + 1 ) , data } ) ;
8752 }
8853
54+ files . push ( {
55+ path : 'src/routes/+page.js' ,
56+ data :
57+ "// Because we don't know whether or not your playground app can run in a server environment, we disable server-side rendering.\n" +
58+ '// Make sure to test whether or not you can re-enable it, as SSR improves perceived performance and site accessibility.\n' +
59+ '// Read more about this option here: https://svelte.dev/docs/kit/page-options#ssr\n' +
60+ 'export const ssr = false;\n'
61+ } ) ;
62+
8963 writeFileSync ( output_file , JSON . stringify ( files ) ) ;
9064
9165 // remove output dir afterwards to prevent it messing with Vite watcher
0 commit comments