File tree Expand file tree Collapse file tree 6 files changed +103
-4
lines changed Expand file tree Collapse file tree 6 files changed +103
-4
lines changed Original file line number Diff line number Diff line change 1212
1313 < script type ="module ">
1414 import { mount , hydrate , unmount } from 'svelte' ;
15- import App from '/src/main .svelte' ;
15+ import App from '/src/App .svelte' ;
1616
1717 const root = document . getElementById ( 'root' ) ;
1818 const render = root . firstChild ?. nextSibling ? hydrate : mount ;
Original file line number Diff line number Diff line change 99 "ssr" : " node --conditions=development ./ssr-dev.js" ,
1010 "build" : " vite build --outDir dist/client && vite build --outDir dist/server --ssr ssr-prod.js" ,
1111 "prod" : " npm run build && node dist/server/ssr-prod" ,
12- "preview" : " vite preview"
12+ "preview" : " vite preview" ,
13+ "download" : " node scripts/download.js" ,
14+ "hash" : " node scripts/hash.js | pbcopy && echo \" copied URL to clipboard\" "
1315 },
1416 "devDependencies" : {
1517 "@sveltejs/vite-plugin-svelte" : " ^4.0.0-next.6" ,
Original file line number Diff line number Diff line change 1+ import fs from 'node:fs' ;
2+
3+ const arg = process . argv [ 2 ] ;
4+
5+ /** @type {URL } */
6+ let url ;
7+
8+ try {
9+ url = new URL ( arg ) ;
10+ } catch ( e ) {
11+ console . error ( `${ arg } is not a URL` ) ;
12+ process . exit ( 1 ) ;
13+ }
14+
15+ if ( url . origin !== 'https://svelte.dev' || ! url . pathname . startsWith ( '/playground/' ) ) {
16+ console . error ( `${ arg } is not a Svelte playground URL` ) ;
17+ process . exit ( 1 ) ;
18+ }
19+
20+ let files ;
21+
22+ if ( url . hash . length > 1 ) {
23+ const decoded = atob ( url . hash . slice ( 1 ) . replaceAll ( '-' , '+' ) . replaceAll ( '_' , '/' ) ) ;
24+ // putting it directly into the blob gives a corrupted file
25+ const u8 = new Uint8Array ( decoded . length ) ;
26+ for ( let i = 0 ; i < decoded . length ; i ++ ) {
27+ u8 [ i ] = decoded . charCodeAt ( i ) ;
28+ }
29+ const stream = new Blob ( [ u8 ] ) . stream ( ) . pipeThrough ( new DecompressionStream ( 'gzip' ) ) ;
30+ const json = await new Response ( stream ) . text ( ) ;
31+
32+ files = JSON . parse ( json ) . files ;
33+ } else {
34+ const id = url . pathname . split ( '/' ) [ 2 ] ;
35+ const response = await fetch ( `https://svelte.dev/playground/api/${ id } .json` ) ;
36+
37+ files = ( await response . json ( ) ) . components . map ( ( data ) => {
38+ const basename = `${ data . name } .${ data . type } ` ;
39+
40+ return {
41+ type : 'file' ,
42+ name : basename ,
43+ basename,
44+ contents : data . source ,
45+ text : true
46+ } ;
47+ } ) ;
48+ }
49+
50+ for ( const file of files ) {
51+ fs . writeFileSync ( `src/${ file . name } ` , file . contents ) ;
52+ }
Original file line number Diff line number Diff line change 1+ import fs from 'node:fs' ;
2+
3+ const files = [ ] ;
4+
5+ for ( const basename of fs . readdirSync ( 'src' ) ) {
6+ if ( fs . statSync ( `src/${ basename } ` ) . isDirectory ( ) ) continue ;
7+
8+ files . push ( {
9+ type : 'file' ,
10+ name : basename ,
11+ basename,
12+ contents : fs . readFileSync ( `src/${ basename } ` , 'utf-8' ) ,
13+ text : true // TODO might not be
14+ } ) ;
15+ }
16+
17+ const payload = JSON . stringify ( {
18+ name : 'sandbox' ,
19+ files
20+ } ) ;
21+
22+ async function compress ( payload ) {
23+ const reader = new Blob ( [ payload ] )
24+ . stream ( )
25+ . pipeThrough ( new CompressionStream ( 'gzip' ) )
26+ . getReader ( ) ;
27+
28+ let buffer = '' ;
29+ for ( ; ; ) {
30+ const { done, value } = await reader . read ( ) ;
31+
32+ if ( done ) {
33+ reader . releaseLock ( ) ;
34+ return btoa ( buffer ) . replaceAll ( '+' , '-' ) . replaceAll ( '/' , '_' ) ;
35+ } else {
36+ for ( let i = 0 ; i < value . length ; i ++ ) {
37+ // decoding as utf-8 will make btoa reject the string
38+ buffer += String . fromCharCode ( value [ i ] ) ;
39+ }
40+ }
41+ }
42+ }
43+
44+ const hash = await compress ( payload ) ;
45+ console . log ( `https://svelte.dev/playground/untitled#${ hash } ` ) ;
Original file line number Diff line number Diff line change @@ -22,7 +22,7 @@ polka()
2222 . use ( async ( req , res ) => {
2323 const template = fs . readFileSync ( path . resolve ( __dirname , 'index.html' ) , 'utf-8' ) ;
2424 const transformed_template = await vite . transformIndexHtml ( req . url , template ) ;
25- const { default : App } = await vite . ssrLoadModule ( '/src/main .svelte' ) ;
25+ const { default : App } = await vite . ssrLoadModule ( '/src/App .svelte' ) ;
2626 const { head, body } = render ( App ) ;
2727
2828 const html = transformed_template
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import fs from 'node:fs';
22import path from 'node:path' ;
33import polka from 'polka' ;
44import { render } from 'svelte/server' ;
5- import App from './src/main .svelte' ;
5+ import App from './src/App .svelte' ;
66
77const { head, body } = render ( App ) ;
88
You can’t perform that action at this time.
0 commit comments