Skip to content

Commit 6419fb1

Browse files
committed
initial commit
0 parents  commit 6419fb1

17 files changed

+791
-0
lines changed

.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?

.vscode/extensions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["svelte.svelte-vscode"]
3+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Make Use Of
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

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/sveltejs/svelte-hmr/tree/master/packages/svelte-hmr#preservation-of-local-state).
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+
```

index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/src/main.js"></script>
12+
</body>
13+
</html>

jsconfig.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "bundler",
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+
"verbatimModuleSyntax": true,
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+
/**
22+
* Typecheck JS in `.svelte` and `.js` files by default.
23+
* Disable this if you'd like to use dynamic types.
24+
*/
25+
"checkJs": true
26+
},
27+
/**
28+
* Use global.d.ts instead of compilerOptions.types
29+
* to avoid limiting type declarations.
30+
*/
31+
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
32+
}

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "svelte-hangman",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"preview": "vite preview"
10+
},
11+
"devDependencies": {
12+
"@sveltejs/vite-plugin-svelte": "^2.4.2",
13+
"svelte": "^4.0.5",
14+
"vite": "^4.4.5"
15+
}
16+
}

public/vite.svg

Lines changed: 1 addition & 0 deletions
Loading

src/App.svelte

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<script>
2+
import { hangmanArt } from "./hangmanArt";
3+
let words = [
4+
"appetizer",
5+
"roommates",
6+
"shrinking",
7+
"freedom",
8+
"happiness",
9+
"development",
10+
];
11+
let userInput;
12+
let randomNum = Math.floor(Math.random() * (words.length - 1));
13+
let selectedWord = words[randomNum].toUpperCase();
14+
let initial = selectedWord;
15+
let match;
16+
let message;
17+
let hangmanStages = hangmanArt;
18+
let output = "";
19+
[...selectedWord].forEach(() => (output += "-"));
20+
match = output;
21+
22+
23+
function generateOutput(input1, input2) {
24+
output = "";
25+
for (let i = 0; i < input1.length; i++) {
26+
if (input2[i] === "-") {
27+
output += input1[i];
28+
} else {
29+
output += "-";
30+
}
31+
}
32+
}
33+
34+
function evaluate() {
35+
let guess = userInput.toUpperCase().trim();
36+
if (!guess) {
37+
return;
38+
}
39+
if (selectedWord.includes(guess)) {
40+
selectedWord = selectedWord.replaceAll(guess, "-");
41+
generateOutput(initial, selectedWord);
42+
} else {
43+
hangmanStages.shift();
44+
hangmanStages = hangmanStages;
45+
}
46+
userInput = "";
47+
}
48+
</script>
49+
50+
<main>
51+
<h1 class="title">
52+
Hangman
53+
</h1>
54+
<div class="tagline">
55+
I'm thinking of a word. Could you guess the letters in that word?
56+
</div>
57+
{#if hangmanStages.length > 0}
58+
<pre class="hangman">
59+
{hangmanStages[0]}</pre>
60+
{/if}
61+
{#if hangmanStages.length === 1}
62+
<div class="message" bind:this={message}>You Lose...</div>
63+
{/if}
64+
{#if selectedWord === match}
65+
<div class="message" bind:this={message}>You Win...</div>
66+
{/if}
67+
68+
{#if !message}
69+
<div class="output">
70+
{#each output as letter}
71+
{#if letter !== "-"}
72+
<span class="complete box">{letter}</span>
73+
{:else}
74+
<span class="incomplete box" />
75+
{/if}
76+
{/each}
77+
</div>
78+
<form on:submit|preventDefault={() => evaluate()}>
79+
<input
80+
type="text"
81+
placeholder="Enter a letter"
82+
maxlength="1"
83+
bind:value={userInput}
84+
/>
85+
<button type="submit">Submit</button>
86+
</form>
87+
{/if}
88+
</main>
89+
90+
<style>
91+
* {
92+
color: green;
93+
text-align: center;
94+
}
95+
96+
main {
97+
display: flex;
98+
width: 100%;
99+
flex-direction: column;
100+
justify-content: center;
101+
align-items: center;
102+
}
103+
104+
input,
105+
button {
106+
text-transform: uppercase;
107+
background-color: transparent;
108+
border: solid 1.2px green;
109+
height:40px;
110+
font-size: 15px;
111+
}
112+
113+
.box {
114+
display: flex;
115+
align-items: center;
116+
justify-content: center;
117+
width: 45px;
118+
height: inherit;
119+
border: dotted 5.2px green;
120+
}
121+
122+
.output {
123+
display: flex;
124+
font-size: 23px;
125+
font-weight: 600;
126+
height: 45px;
127+
gap: 10px;
128+
justify-content: center;
129+
}
130+
131+
.hangman {
132+
font-size: 32px;
133+
}
134+
135+
form {
136+
margin-top: 50px;
137+
}
138+
.tagline,
139+
.message {
140+
font-size: 20px;
141+
}
142+
</style>

src/app.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:root{
2+
background-color: rgb(0, 0, 0);
3+
color:green;
4+
font-family: monospace;
5+
}

src/hangmanArt.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
export let hangmanArt = [
2+
`
3+
+---+
4+
|
5+
|
6+
|
7+
|
8+
|
9+
=========`,
10+
`
11+
+---+
12+
| |
13+
|
14+
|
15+
|
16+
|
17+
=========`,
18+
`
19+
+---+
20+
| |
21+
O |
22+
|
23+
|
24+
|
25+
=========`,
26+
`
27+
+---+
28+
| |
29+
O |
30+
| |
31+
|
32+
|
33+
=========`,
34+
`
35+
+---+
36+
| |
37+
O |
38+
/| |
39+
|
40+
|
41+
=========`,
42+
`
43+
+---+
44+
| |
45+
O |
46+
/|\\ |
47+
|
48+
|
49+
=========`,
50+
`
51+
+---+
52+
| |
53+
O |
54+
/|\\ |
55+
/ |
56+
|
57+
=========`,
58+
`
59+
+---+
60+
| |
61+
O |
62+
/|\\ |
63+
/ \\ |
64+
|
65+
=========`,
66+
];

src/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import './app.css'
2+
import App from './App.svelte'
3+
4+
const app = new App({
5+
target: document.getElementById('app'),
6+
})
7+
8+
export default app

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" />

0 commit comments

Comments
 (0)