Init WebSerial
This commit is contained in:
parent
0d13c7604f
commit
444ca860fa
24
webserial/.gitignore
vendored
Normal file
24
webserial/.gitignore
vendored
Normal 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
webserial/.vscode/extensions.json
vendored
Normal file
3
webserial/.vscode/extensions.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"recommendations": ["svelte.svelte-vscode"]
|
||||
}
|
47
webserial/README.md
Normal file
47
webserial/README.md
Normal file
@ -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)
|
||||
```
|
12
webserial/index.html
Normal file
12
webserial/index.html
Normal file
@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Light Maymays</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/src/main.ts"></script>
|
||||
</body>
|
||||
</html>
|
2381
webserial/package-lock.json
generated
Normal file
2381
webserial/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
22
webserial/package.json
Normal file
22
webserial/package.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "webserial",
|
||||
"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": "^2.0.0",
|
||||
"@tsconfig/svelte": "^3.0.0",
|
||||
"@types/w3c-web-serial": "^1.0.3",
|
||||
"svelte": "^3.54.0",
|
||||
"svelte-check": "^2.10.0",
|
||||
"tslib": "^2.4.1",
|
||||
"typescript": "^4.9.3",
|
||||
"vite": "^4.0.0"
|
||||
}
|
||||
}
|
8
webserial/src/App.svelte
Normal file
8
webserial/src/App.svelte
Normal file
@ -0,0 +1,8 @@
|
||||
<script lang="ts">
|
||||
import SerialManager from "./serial/SerialManager.svelte";
|
||||
</script>
|
||||
|
||||
<main>
|
||||
<div>cOnTeNt</div>
|
||||
<SerialManager />
|
||||
</main>
|
7
webserial/src/main.ts
Normal file
7
webserial/src/main.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import App from './App.svelte'
|
||||
|
||||
const app = new App({
|
||||
target: document.getElementById('app'),
|
||||
})
|
||||
|
||||
export default app
|
27
webserial/src/serial/SerialManager.svelte
Normal file
27
webserial/src/serial/SerialManager.svelte
Normal file
@ -0,0 +1,27 @@
|
||||
<script lang="ts">
|
||||
import SerialReader from "./SerialReader.svelte";
|
||||
|
||||
let port: SerialPort = null;
|
||||
|
||||
async function connect() {
|
||||
port = await navigator.serial.requestPort();
|
||||
port.addEventListener("disconnect", disconnect);
|
||||
}
|
||||
|
||||
async function disconnect() {
|
||||
port = null;
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if navigator.serial}
|
||||
<p>serial baby</p>
|
||||
{#if port !== null}
|
||||
<SerialReader {port}/>
|
||||
<p><button on:click={disconnect}>disconnect</button></p>
|
||||
{:else}
|
||||
<p><button on:click={connect}>connect</button></p>
|
||||
{/if}
|
||||
{:else}
|
||||
<p>Looks like your browser does not support WebSerial 😢</p>
|
||||
<p>Check <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Serial_API#browser_compatibility">here</a> for a list of compatible browsers</p>
|
||||
{/if}
|
57
webserial/src/serial/SerialReader.svelte
Normal file
57
webserial/src/serial/SerialReader.svelte
Normal file
@ -0,0 +1,57 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte";
|
||||
|
||||
export let port: SerialPort;
|
||||
|
||||
let lines = [];
|
||||
|
||||
const handleLine = (line: string) => {
|
||||
lines.push(line);
|
||||
if (lines.length > 10) lines.shift();
|
||||
lines = lines;
|
||||
}
|
||||
|
||||
let running = true;
|
||||
async function runner() {
|
||||
console.log("runner started");
|
||||
await port.open({baudRate: 500_000});
|
||||
let reader = port.readable.getReader();
|
||||
let data = "";
|
||||
try {
|
||||
while (running) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
const chunk = new TextDecoder().decode(value);
|
||||
data += chunk;
|
||||
|
||||
const lines = data.split("\n");
|
||||
data = lines.pop();
|
||||
for (let line of lines) {
|
||||
handleLine(line);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
}
|
||||
await port.close();
|
||||
console.log("runner stopped");
|
||||
}
|
||||
let handle: Promise<void> = null;
|
||||
|
||||
onMount(() => {
|
||||
console.log("mount");
|
||||
handle = runner();
|
||||
|
||||
return () => {
|
||||
console.log("unmount");
|
||||
running = false;
|
||||
};
|
||||
});
|
||||
</script>
|
||||
|
||||
<div>
|
||||
{#each lines as line}
|
||||
<div>{line}</div>
|
||||
{/each}
|
||||
</div>
|
2
webserial/src/vite-env.d.ts
vendored
Normal file
2
webserial/src/vite-env.d.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/// <reference types="svelte" />
|
||||
/// <reference types="vite/client" />
|
7
webserial/svelte.config.js
Normal file
7
webserial/svelte.config.js
Normal file
@ -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(),
|
||||
}
|
20
webserial/tsconfig.json
Normal file
20
webserial/tsconfig.json
Normal file
@ -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/**/*.d.ts", "src/**/*.ts", "src/**/*.js", "src/**/*.svelte"],
|
||||
"references": [{ "path": "./tsconfig.node.json" }]
|
||||
}
|
8
webserial/tsconfig.node.json
Normal file
8
webserial/tsconfig.node.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Node"
|
||||
},
|
||||
"include": ["vite.config.ts"]
|
||||
}
|
7
webserial/vite.config.ts
Normal file
7
webserial/vite.config.ts
Normal 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()],
|
||||
})
|
Loading…
Reference in New Issue
Block a user