Skip to content

Commit 7ac4aac

Browse files
committed
initialize playground
1 parent ddfff4d commit 7ac4aac

18 files changed

+870
-6
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,10 @@
2727
"prettier": "^2.8.8",
2828
"prettier-plugin-svelte": "^2.10.0"
2929
},
30+
"pnpm": {
31+
"overrides": {
32+
"svelte": "workspace:*"
33+
}
34+
},
3035
"packageManager": "[email protected]"
3136
}

packages/playground/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

packages/playground/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Svelte + Vite
2+
3+
This template should help get you started developing with Svelte in Vite.
4+
5+
## Recommended IDE Setup
6+
7+
[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).
8+
9+
## Need an official Svelte framework?
10+
11+
Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more.
12+
13+
## Technical considerations
14+
15+
**Why use this over SvelteKit?**
16+
17+
- It brings its own routing solution which might not be preferable for some users.
18+
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
19+
20+
This template contains as little as possible to get started with Vite + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project.
21+
22+
Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate.
23+
24+
**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**
25+
26+
Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information.
27+
28+
**Why include `.vscode/extensions.json`?**
29+
30+
Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project.
31+
32+
**Why enable `checkJs` in the JS template?**
33+
34+
It is likely that most cases of changing variable types in runtime are likely to be accidental, rather than deliberate. This provides advanced typechecking out of the box. Should you like to take advantage of the dynamically-typed nature of JavaScript, it is trivial to change the configuration.
35+
36+
**Why is HMR not preserving my local component state?**
37+
38+
HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr).
39+
40+
If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.
41+
42+
```js
43+
// store.js
44+
// An extremely simple external store
45+
import { writable } from 'svelte/store'
46+
export default writable(0)
47+
```

packages/playground/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + Svelte</title>
8+
<!--app-head-->
9+
</head>
10+
<body>
11+
<div id="app"><!--app-html--></div>
12+
<script type="module" src="/src/entry-client.js"></script>
13+
</body>
14+
</html>

packages/playground/jsconfig.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "Node",
4+
"target": "ESNext",
5+
"module": "ESNext",
6+
/**
7+
* svelte-preprocess cannot figure out whether you have
8+
* a value or a type, so tell TypeScript to enforce using
9+
* `import type` instead of `import` for Types.
10+
*/
11+
"importsNotUsedAsValues": "error",
12+
"isolatedModules": true,
13+
"resolveJsonModule": true,
14+
/**
15+
* To have warnings / errors of the Svelte compiler at the
16+
* correct position, enable source maps by default.
17+
*/
18+
"sourceMap": true,
19+
"esModuleInterop": true,
20+
"skipLibCheck": true,
21+
"forceConsistentCasingInFileNames": true,
22+
/**
23+
* Typecheck JS in `.svelte` and `.js` files by default.
24+
* Disable this if you'd like to use dynamic types.
25+
*/
26+
"checkJs": true
27+
},
28+
/**
29+
* Use global.d.ts instead of compilerOptions.types
30+
* to avoid limiting type declarations.
31+
*/
32+
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
33+
}

packages/playground/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "playground",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "node server",
8+
"build": "npm run build:client && npm run build:server",
9+
"build:client": "vite build --ssrManifest --outDir dist/client",
10+
"build:server": "vite build --ssr src/entry-server.js --outDir dist/server",
11+
"preview": "cross-env NODE_ENV=production node server"
12+
},
13+
"dependencies": {
14+
"compression": "^1.7.4",
15+
"express": "^4.18.2",
16+
"sirv": "^2.0.2"
17+
},
18+
"devDependencies": {
19+
"@sveltejs/vite-plugin-svelte": "^2.4.1",
20+
"cross-env": "^7.0.3",
21+
"svelte": "^3.54.0",
22+
"vite": "^4.0.4"
23+
}
24+
}

packages/playground/public/vite.svg

Lines changed: 1 addition & 0 deletions
Loading

packages/playground/server.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import fs from 'node:fs/promises'
2+
import express from 'express'
3+
4+
// Constants
5+
const isProduction = process.env.NODE_ENV === 'production'
6+
const port = process.env.PORT || 5173
7+
const base = process.env.BASE || '/'
8+
9+
// Cached production assets
10+
const templateHtml = isProduction
11+
? await fs.readFile('./dist/client/index.html', 'utf-8')
12+
: ''
13+
const ssrManifest = isProduction
14+
? await fs.readFile('./dist/client/ssr-manifest.json', 'utf-8')
15+
: undefined
16+
17+
// Create http server
18+
const app = express()
19+
20+
// Add Vite or respective production middlewares
21+
let vite
22+
if (!isProduction) {
23+
const { createServer } = await import('vite')
24+
vite = await createServer({
25+
server: { middlewareMode: true },
26+
appType: 'custom',
27+
base
28+
})
29+
app.use(vite.middlewares)
30+
} else {
31+
const compression = (await import('compression')).default
32+
const sirv = (await import('sirv')).default
33+
app.use(compression())
34+
app.use(base, sirv('./dist/client', { extensions: [] }))
35+
}
36+
37+
// Serve HTML
38+
app.use('*', async (req, res) => {
39+
try {
40+
const url = req.originalUrl.replace(base, '')
41+
42+
let template
43+
let render
44+
if (!isProduction) {
45+
// Always read fresh template in development
46+
template = await fs.readFile('./index.html', 'utf-8')
47+
template = await vite.transformIndexHtml(url, template)
48+
render = (await vite.ssrLoadModule('/src/entry-server.js')).render
49+
} else {
50+
template = templateHtml
51+
render = (await import('./dist/server/entry-server.js')).render
52+
}
53+
54+
const rendered = await render(url, ssrManifest)
55+
56+
const html = template
57+
.replace(`<!--app-head-->`, rendered.head ?? '')
58+
.replace(`<!--app-html-->`, rendered.html ?? '')
59+
60+
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
61+
} catch (e) {
62+
vite?.ssrFixStacktrace(e)
63+
console.log(e.stack)
64+
res.status(500).end(e.stack)
65+
}
66+
})
67+
68+
// Start http server
69+
app.listen(port, () => {
70+
console.log(`Server started at http://localhost:${port}`)
71+
})

packages/playground/src/App.svelte

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<script>
2+
import svelteLogo from './assets/svelte.svg'
3+
import Counter from './lib/Counter.svelte'
4+
</script>
5+
6+
<main>
7+
<div>
8+
<a href="https://vitejs.dev" target="_blank" rel="noreferrer">
9+
<img src="/vite.svg" class="logo" alt="Vite Logo" />
10+
</a>
11+
<a href="https://svelte.dev" target="_blank" rel="noreferrer">
12+
<img src={svelteLogo} class="logo svelte" alt="Svelte Logo" />
13+
</a>
14+
</div>
15+
<h1>Vite + Svelte</h1>
16+
17+
<div class="card">
18+
<Counter />
19+
</div>
20+
21+
<p>
22+
Check out <a href="https://github.com/sveltejs/kit#readme" target="_blank" rel="noreferrer">SvelteKit</a>, the official Svelte app framework powered by Vite!
23+
</p>
24+
25+
<p class="read-the-docs">
26+
Click on the Vite and Svelte logos to learn more
27+
</p>
28+
</main>
29+
30+
<style>
31+
.logo {
32+
height: 6em;
33+
padding: 1.5em;
34+
will-change: filter;
35+
}
36+
.logo:hover {
37+
filter: drop-shadow(0 0 2em #646cffaa);
38+
}
39+
.logo.svelte:hover {
40+
filter: drop-shadow(0 0 2em #ff3e00aa);
41+
}
42+
.read-the-docs {
43+
color: #888;
44+
}
45+
</style>

packages/playground/src/app.css

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
:root {
2+
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
3+
line-height: 1.5;
4+
font-weight: 400;
5+
6+
color-scheme: light dark;
7+
color: rgba(255, 255, 255, 0.87);
8+
background-color: #242424;
9+
10+
font-synthesis: none;
11+
text-rendering: optimizeLegibility;
12+
-webkit-font-smoothing: antialiased;
13+
-moz-osx-font-smoothing: grayscale;
14+
-webkit-text-size-adjust: 100%;
15+
}
16+
17+
a {
18+
font-weight: 500;
19+
color: #646cff;
20+
text-decoration: inherit;
21+
}
22+
a:hover {
23+
color: #535bf2;
24+
}
25+
26+
body {
27+
margin: 0;
28+
display: flex;
29+
place-items: center;
30+
min-width: 320px;
31+
min-height: 100vh;
32+
}
33+
34+
h1 {
35+
font-size: 3.2em;
36+
line-height: 1.1;
37+
}
38+
39+
.card {
40+
padding: 2em;
41+
}
42+
43+
#app {
44+
max-width: 1280px;
45+
margin: 0 auto;
46+
padding: 2rem;
47+
text-align: center;
48+
}
49+
50+
button {
51+
border-radius: 8px;
52+
border: 1px solid transparent;
53+
padding: 0.6em 1.2em;
54+
font-size: 1em;
55+
font-weight: 500;
56+
font-family: inherit;
57+
background-color: #1a1a1a;
58+
cursor: pointer;
59+
transition: border-color 0.25s;
60+
}
61+
button:hover {
62+
border-color: #646cff;
63+
}
64+
button:focus,
65+
button:focus-visible {
66+
outline: 4px auto -webkit-focus-ring-color;
67+
}
68+
69+
@media (prefers-color-scheme: light) {
70+
:root {
71+
color: #213547;
72+
background-color: #ffffff;
73+
}
74+
a:hover {
75+
color: #747bff;
76+
}
77+
button {
78+
background-color: #f9f9f9;
79+
}
80+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import './app.css'
2+
import App from './App.svelte'
3+
4+
new App({
5+
target: document.getElementById('app'),
6+
hydrate: true
7+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import App from './App.svelte'
2+
3+
export function render() {
4+
// @ts-ignore
5+
return App.render()
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
let count = 0
3+
const increment = () => {
4+
count += 1
5+
}
6+
</script>
7+
8+
<button on:click={increment}>
9+
count is {count}
10+
</button>

packages/playground/src/vite-env.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// <reference types="svelte" />
2+
/// <reference types="vite/client" />

packages/playground/svelte.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
2+
3+
export default {
4+
preprocess: vitePreprocess()
5+
}

0 commit comments

Comments
 (0)