Skip to content

Commit

Permalink
Add initial project files
Browse files Browse the repository at this point in the history
  • Loading branch information
m4tt72 committed Dec 31, 2023
0 parents commit 497f561
Show file tree
Hide file tree
Showing 21 changed files with 1,782 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["svelte.svelte-vscode"]
}
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Svelte + TS + Vite

This template should help get you started developing with Svelte and TypeScript 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.

This template contains as little as possible to get started with Vite + TypeScript + 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 `allowJs` in the TS template?**

While `allowJs: false` would indeed prevent the use of `.js` files in the project, it does not prevent the use of JavaScript syntax in `.svelte` files. In addition, it would force `checkJs: false`, bringing the worst of both worlds: not being able to guarantee the entire codebase is TypeScript, and also having worse typechecking for the existing JavaScript. In addition, there are valid use cases in which a mixed codebase may be relevant.

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

```ts
// store.ts
// An extremely simple external store
import { writable } from 'svelte/store'
export default writable(0)
```
17 changes: 17 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html class="dark" lang="en" style="color-scheme: dark;">

<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Svelte + TS</title>
</head>

<body class="min-h-screen font-mono antialiased">
<div id="app" class="h-screen p-4"></div>

<script type="module" src="/src/main.ts"></script>
</body>

</html>
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "svelterm",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-check --tsconfig ./tsconfig.json"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^3.0.1",
"@tsconfig/svelte": "^5.0.2",
"autoprefixer": "^10.4.16",
"postcss": "^8.4.32",
"svelte": "^4.2.8",
"svelte-check": "^3.6.2",
"tailwindcss": "^3.4.0",
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"vite": "^5.0.8"
}
}
6 changes: 6 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
1 change: 1 addition & 0 deletions public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
181 changes: 181 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<script lang="ts">
import { afterUpdate, onDestroy, onMount } from 'svelte';
import type { Command } from './interfaces/command';
let input: HTMLInputElement;
let command = '';
let history: Array<Command> = [];
let isFocused = false;
let hostname = window.location.hostname;
let historyIndex = -1;
const commands: Record<string, (args: string[]) => Promise<string> | string> =
{
help: () => 'Available commands: ' + Object.keys(commands).join(', '),
hostname: () => hostname,
pwd: () => '/home/guest',
whoami: () => 'guest',
clear: () => {
history = [];
return '';
},
echo: (args: string[]) => args.join(' '),
curl: async (args: string[]) => {
if (args.length === 0) {
return 'curl: no URL provided';
}
const url = args[0];
try {
const response = await fetch(url);
const data = await response.text();
return data;
} catch (error) {
return `curl: could not fetch URL ${url}`;
}
},
};
const handleFocus = () => {
isFocused = true;
};
const handleBlur = () => {
isFocused = false;
};
onMount(() => {
input.focus();
window.addEventListener('click', () => input.focus());
});
afterUpdate(() => {
input.scrollIntoView({ behavior: 'smooth', block: 'end' });
});
onDestroy(() => {
window.removeEventListener('click', () => input.focus());
});
const handleKeyDown = async (event: KeyboardEvent) => {
if (event.key === 'Enter') {
const [commandName, ...args] = command.split(' ');
const commandFunction = commands[commandName];
if (commandFunction) {
const output = await commandFunction(args);
if (commandName !== 'clear') {
history = [...history, { command, outputs: [output] }];
}
} else {
const output = `${commandName}: command not found`;
history = [...history, { command, outputs: [output] }];
}
command = '';
} else if (event.key === 'ArrowUp') {
if (historyIndex < history.length - 1) {
historyIndex++;
command = history[history.length - 1 - historyIndex].command;
}
event.preventDefault();
} else if (event.key === 'ArrowDown') {
if (historyIndex > -1) {
historyIndex--;
command =
historyIndex >= 0
? history[history.length - 1 - historyIndex].command
: '';
}
event.preventDefault();
} else if (event.key === 'Tab') {
event.preventDefault();
const autoCompleteCommand = Object.keys(commands).find((cmd) =>
cmd.startsWith(command),
);
if (autoCompleteCommand) {
command = autoCompleteCommand;
}
}
};
</script>

<main class="h-full border-2 rounded-md border-green-600 p-4 overflow-auto">
{#each history as { command, outputs }}
<div>
<div class="flex flex-row">
<h1 class="font-bold text-green-600">
<span class="text-orange-600">guest</span>@{hostname}:<span
class="text-white">~$</span
>
</h1>

<p class="text-white px-3">{command}</p>
</div>

{#each outputs as output}
<p class="text-white">{output}</p>
{/each}
</div>
{/each}

<div class="flex flex-row">
<h1 class="font-bold text-green-600">
<span class="text-orange-600">guest</span>@{hostname}:<span
class="text-white">~$</span
>
</h1>

<div class="input-container">
<input
class="w-full px-3 bg-transparent outline-none caret-green-600"
type="text"
bind:value={command}
on:keydown={handleKeyDown}
bind:this={input}
on:focus={handleFocus}
on:blur={handleBlur}
/>

{#if isFocused}
<div class="block-caret" style="left: {command.length}ch"></div>
{/if}
</div>
</div>
</main>

<style>
.input-container {
position: relative;
width: 100%;
}
.input-container input {
caret-color: transparent;
}
.input-container .block-caret {
position: absolute;
top: 4px; /* Add this line */
width: 10px;
height: 1em;
background-color: currentColor;
animation: blink 1s infinite;
margin-left: 14px;
}
@keyframes blink {
50% {
opacity: 0;
}
}
</style>
3 changes: 3 additions & 0 deletions src/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
1 change: 1 addition & 0 deletions src/assets/svelte.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/interfaces/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Command {
command: string;
outputs: string[];
}
4 changes: 4 additions & 0 deletions src/interfaces/filesystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface FileSystemNode {
type: 'file' | 'directory';
contents: string | Record<string, FileSystemNode>;
}
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import './app.css';
import App from './App.svelte';

const app = new App({
target: document.getElementById('app'),
});

export default app;
Empty file added src/utils/commands.ts
Empty file.
2 changes: 2 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="svelte" />
/// <reference types="vite/client" />
7 changes: 7 additions & 0 deletions svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'

export default {
// Consult https://svelte.dev/docs#compile-time-svelte-preprocess
// for more information about preprocessors
preprocess: vitePreprocess(),
}
10 changes: 10 additions & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('tailwindcss').Config} */
export default {
content: ['./index.html', './src/**/*.{svelte,js,ts,jsx,tsx}'],
theme: {
extend: {
caretColor: ['focus-visible'],
},
},
plugins: [],
};
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "@tsconfig/svelte/tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"resolveJsonModule": true,
/**
* Typecheck JS in `.svelte` and `.js` files by default.
* Disable checkJs if you'd like to use dynamic types in JS.
* Note that setting allowJs false does not prevent the use
* of JS in `.svelte` files.
*/
"allowJs": true,
"checkJs": true,
"isolatedModules": true
},
"include": ["src/**/*.ts", "src/**/*.js", "src/**/*.svelte"],
"references": [{ "path": "./tsconfig.node.json" }]
}
9 changes: 9 additions & 0 deletions tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler"
},
"include": ["vite.config.ts"]
}
Loading

0 comments on commit 497f561

Please sign in to comment.