This commit is contained in:
Jonathan Jenne 2022-05-31 23:44:59 +02:00
commit 7a402fa02e
18 changed files with 1467 additions and 0 deletions

24
.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

3
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"recommendations": ["svelte.svelte-vscode"]
}

48
README.md Normal file
View File

@ -0,0 +1,48 @@
# Svelte + Vite
This template should help get you started developing with Svelte in Vite.
## Recommended IDE Setup
[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).
## Need an official Svelte framework?
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.
## Technical considerations
**Why use this over SvelteKit?**
- It brings its own routing solution which might not be preferable for some users.
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
`vite dev` and `vite build` wouldn't work in a SvelteKit environment, for example.
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.
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.
**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**
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.
**Why include `.vscode/extensions.json`?**
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.
**Why enable `checkJs` in the JS template?**
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.
**Why is HMR not preserving my local component state?**
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).
If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.
```js
// store.js
// An extremely simple external store
import { writable } from 'svelte/store'
export default writable(0)
```

13
index.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Svelte + Vite App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>

34
jsconfig.json Normal file
View File

@ -0,0 +1,34 @@
{
"compilerOptions": {
"moduleResolution": "node",
"target": "esnext",
"module": "esnext",
/**
* svelte-preprocess cannot figure out whether you have
* a value or a type, so tell TypeScript to enforce using
* `import type` instead of `import` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
* To have warnings / errors of the Svelte compiler at the
* correct position, enable source maps by default.
*/
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
/**
* Typecheck JS in `.svelte` and `.js` files by default.
* Disable this if you'd like to use dynamic types.
*/
"checkJs": true
},
/**
* Use global.d.ts instead of compilerOptions.types
* to avoid limiting type declarations.
*/
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}

1058
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

16
package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "notes",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.30",
"svelte": "^3.44.0",
"vite": "^2.9.9"
}
}

BIN
public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

59
src/App.svelte Normal file
View File

@ -0,0 +1,59 @@
<script>
import MainInput from './lib/MainInput.svelte'
import NoteList from './lib/NoteList.svelte'
import Storage from './storage'
const storage = new Storage()
let notes = storage.getNotes()
function handleNewNote(e) {
console.log(e)
notes.push({
title: e.detail,
tags: ["one", "two"],
done: false
})
notes = notes // Trigger reactiveness
storage.saveNotes(notes)
}
function handleToggleNote(e) {
storage.saveNotes(notes)
}
</script>
<main>
<MainInput on:added={handleNewNote} />
<NoteList notes={notes} on:toggle={handleToggleNote} />
</main>
<style>
:root {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
--gray-50: #F8FAFC;
--gray-100: #F1F5F9;
--gray-200: #E2E8F0;
--gray-300: #CBD5E1;
--gray-400: #94A3B8;
--gray-500: #64748B;
--gray-600: #475569;
--gray-700: #334155;
--gray-800: #1E293B;
--gray-900: #0F172A;
}
:global(body) {
padding: 0;
margin: 0;
background: var(--gray-100);
}
main {
padding: 3rem;
max-width: 50rem;
margin: 0 auto;
}
</style>

65
src/AppOld.svelte Normal file
View File

@ -0,0 +1,65 @@
<script>
import logo from './assets/svelte.png'
import Counter from './lib/Counter.svelte'
</script>
<main>
<img src={logo} alt="Svelte Logo" />
<h1>Hello world!</h1>
<Counter />
<p>
Visit <a href="https://svelte.dev">svelte.dev</a> to learn how to build Svelte
apps.
</p>
<p>
Check out <a href="https://github.com/sveltejs/kit#readme">SvelteKit</a> for
the officially supported framework, also powered by Vite!
</p>
</main>
<style>
:root {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
main {
text-align: center;
padding: 1em;
margin: 0 auto;
}
img {
height: 16rem;
width: 16rem;
}
h1 {
color: #ff3e00;
text-transform: uppercase;
font-size: 4rem;
font-weight: 100;
line-height: 1.1;
margin: 2rem auto;
max-width: 14rem;
}
p {
max-width: 14rem;
margin: 1rem auto;
line-height: 1.35;
}
@media (min-width: 480px) {
h1 {
max-width: none;
}
p {
max-width: none;
}
}
</style>

BIN
src/assets/svelte.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

24
src/lib/MainInput.svelte Normal file
View File

@ -0,0 +1,24 @@
<script>
import { createEventDispatcher } from 'svelte'
const dispatch = createEventDispatcher()
function handleKeyup(e) {
if (e.key === "Enter") {
e.preventDefault()
dispatch('added', e.srcElement.value)
e.srcElement.value = ""
}
}
</script>
<input type="text" on:keyup={handleKeyup}>
<style>
input {
width: 100%;
padding: 0.5rem;
box-sizing: border-box;
border: 1px solid var(--gray-400);
border-radius: 0.15rem;
}
</style>

64
src/lib/Note.svelte Normal file
View File

@ -0,0 +1,64 @@
<script>
import { createEventDispatcher } from 'svelte'
const dispatch = createEventDispatcher()
export let note = {}
function toggle(e) {
dispatch('toggle', note.title)
}
</script>
<article class="note flex" class:done={note.done} class:todo={!note.done}>
<aside class="check">
<input
type="checkbox"
bind:checked={note.done}
on:change={toggle} />
</aside>
<div class="title">
{note.title}
</div>
<aside class="tags">
{#each note.tags as tag}
<a href="#">#{tag}</a>
{/each}
</aside>
</article>
<style>
article.note {
display: flex;
gap: 0.5rem;
padding: 0.5rem;
background: white;
border: 1px solid var(--gray-400);
border-radius: 0.15rem;
margin-bottom: 0.25rem;
}
article.done {
text-decoration: line-through;
background: var(--gray-200);
color: var(--gray-500);
}
article.done a {
color: var(--gray-500);
}
.tags {
display: flex;
gap: 0.5rem;
}
.title {
flex: 1;
}
</style>

24
src/lib/NoteList.svelte Normal file
View File

@ -0,0 +1,24 @@
<script>
import Note from './Note.svelte'
import { createEventDispatcher } from 'svelte'
const dispatch = createEventDispatcher()
function handleToggle(e) {
dispatch("toggle", e.detail)
}
export let notes = []
</script>
<section>
{#each notes as note}
<Note note={note} on:toggle={handleToggle} />
{/each}
</section>
<style>
section {
margin-top: 1rem;
}
</style>

7
src/main.js Normal file
View File

@ -0,0 +1,7 @@
import App from './App.svelte'
const app = new App({
target: document.getElementById('app')
})
export default app

19
src/storage.js Normal file
View File

@ -0,0 +1,19 @@
export default class Storage {
constructor() {
this.prefix = "notesapp"
}
getNotes() {
const notesString = localStorage.getItem(`${this.prefix}-notes`)
if (notesString) {
return JSON.parse(notesString)
} else {
return []
}
}
saveNotes(notes) {
console.log(notes)
localStorage.setItem(`${this.prefix}-notes`, JSON.stringify(notes))
}
}

2
src/vite-env.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
/// <reference types="svelte" />
/// <reference types="vite/client" />

7
vite.config.js Normal file
View File

@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [svelte()]
})