Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit 1a45248

Browse files
committed
chore: add a browser demo
1 parent f4bee8a commit 1a45248

18 files changed

+589
-12
lines changed

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

demo/.vscode/extensions.json

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

demo/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Vue 3 + TypeScript + Vite
2+
3+
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
4+
5+
## Recommended IDE Setup
6+
7+
- [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (and disable Vetur) + [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin).
8+
9+
## Type Support For `.vue` Imports in TS
10+
11+
TypeScript cannot handle type information for `.vue` imports by default, so we replace the `tsc` CLI with `vue-tsc` for type checking. In editors, we need [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=Vue.vscode-typescript-vue-plugin) to make the TypeScript language service aware of `.vue` types.
12+
13+
If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a [Take Over Mode](https://github.com/johnsoncodehk/volar/discussions/471#discussioncomment-1361669) that is more performant. You can enable it by the following steps:
14+
15+
1. Disable the built-in TypeScript Extension
16+
1. Run `Extensions: Show Built-in Extensions` from VSCode's command palette
17+
2. Find `TypeScript and JavaScript Language Features`, right click and select `Disable (Workspace)`
18+
2. Reload the VSCode window by running `Developer: Reload Window` from the command palette.

demo/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 + Vue + TS</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/src/main.ts"></script>
12+
</body>
13+
</html>

demo/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "demo",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vue-tsc && vite build",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"vue": "^3.3.11",
13+
"@scalar/snippetz": "workspace:*"
14+
},
15+
"devDependencies": {
16+
"@vitejs/plugin-vue": "^4.5.2",
17+
"typescript": "^5.2.2",
18+
"vite": "^5.0.8",
19+
"vue-tsc": "^1.8.25"
20+
}
21+
}

demo/public/vite.svg

Lines changed: 1 addition & 0 deletions
Loading

demo/src/App.vue

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script setup lang="ts">
2+
import CodeExample from './components/CodeExample.vue'
3+
</script>
4+
5+
<template>
6+
<div class="examples">
7+
<CodeExample target="undici" :request="{ url: 'https://example.com' }"/>
8+
<CodeExample target="undici" :request="{ url: 'https://example.com', method: 'POST' }"/>
9+
<CodeExample target="undici" :request="{ url: 'https://example.com', method: 'POST', headers: [
10+
{
11+
name: 'Content-Type',
12+
value: 'application/json'
13+
}
14+
]
15+
}"/>
16+
</div>
17+
</template>
18+
19+
<style scoped>
20+
.examples {
21+
display: flex;
22+
flex-direction: column;
23+
gap: 1rem;
24+
}
25+
</style>

demo/src/components/CodeExample.vue

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<script setup lang="ts">
2+
import { ref, onMounted } from 'vue'
3+
import { snippetz } from '@scalar/snippetz'
4+
import { undici } from '@scalar/snippetz-plugin-undici'
5+
6+
const props = defineProps<{
7+
target: 'undici',
8+
request: any,
9+
}>()
10+
11+
const code = ref('')
12+
13+
onMounted(async () => {
14+
code.value = await snippetz().get(undici(props.request))
15+
})
16+
</script>
17+
18+
<template>
19+
<div class="code-block">
20+
<div class="title">
21+
{{ target }}
22+
</div>
23+
<div class="container">
24+
<pre><code>{{ code }}</code></pre>
25+
</div>
26+
</div>
27+
</template>
28+
29+
<style scoped>
30+
.code-block {
31+
border: 1px solid #999;
32+
border-radius: 4px;
33+
}
34+
35+
.title {
36+
background: #999;
37+
color: #333;
38+
padding: 0.25rem 1rem;
39+
}
40+
41+
.container {
42+
padding: 1rem;
43+
}
44+
45+
pre {
46+
margin: 0;
47+
}
48+
49+
pre code {
50+
display: block;
51+
background: none;
52+
white-space: pre;
53+
-webkit-overflow-scrolling: touch;
54+
overflow-x: auto;
55+
max-width: 100%;
56+
min-width: 100px;
57+
padding: 0;
58+
}
59+
</style>

demo/src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createApp } from 'vue'
2+
import './style.css'
3+
import App from './App.vue'
4+
5+
createApp(App).mount('#app')

demo/src/style.css

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}
15+
16+
a {
17+
font-weight: 500;
18+
color: #646cff;
19+
text-decoration: inherit;
20+
}
21+
a:hover {
22+
color: #535bf2;
23+
}
24+
25+
body {
26+
margin: 0;
27+
display: flex;
28+
place-items: center;
29+
min-width: 320px;
30+
min-height: 100vh;
31+
}
32+
33+
h1 {
34+
font-size: 3.2em;
35+
line-height: 1.1;
36+
}
37+
38+
button {
39+
border-radius: 8px;
40+
border: 1px solid transparent;
41+
padding: 0.6em 1.2em;
42+
font-size: 1em;
43+
font-weight: 500;
44+
font-family: inherit;
45+
background-color: #1a1a1a;
46+
cursor: pointer;
47+
transition: border-color 0.25s;
48+
}
49+
button:hover {
50+
border-color: #646cff;
51+
}
52+
button:focus,
53+
button:focus-visible {
54+
outline: 4px auto -webkit-focus-ring-color;
55+
}
56+
57+
.card {
58+
padding: 2em;
59+
}
60+
61+
#app {
62+
max-width: 1280px;
63+
margin: 0 auto;
64+
padding: 2rem;
65+
}
66+
67+
@media (prefers-color-scheme: light) {
68+
:root {
69+
color: #213547;
70+
background-color: #ffffff;
71+
}
72+
a:hover {
73+
color: #747bff;
74+
}
75+
button {
76+
background-color: #f9f9f9;
77+
}
78+
}

demo/src/vite-env.d.ts

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

demo/tsconfig.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"useDefineForClassFields": true,
5+
"module": "ESNext",
6+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
7+
"skipLibCheck": true,
8+
9+
/* Bundler mode */
10+
"moduleResolution": "bundler",
11+
"allowImportingTsExtensions": true,
12+
"resolveJsonModule": true,
13+
"isolatedModules": true,
14+
"noEmit": true,
15+
"jsx": "preserve",
16+
17+
/* Linting */
18+
"strict": true,
19+
"noUnusedLocals": true,
20+
"noUnusedParameters": true,
21+
"noFallthroughCasesInSwitch": true
22+
},
23+
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"],
24+
"references": [{ "path": "./tsconfig.node.json" }]
25+
}

demo/tsconfig.node.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"composite": true,
4+
"skipLibCheck": true,
5+
"module": "ESNext",
6+
"moduleResolution": "bundler",
7+
"allowSyntheticDefaultImports": true
8+
},
9+
"include": ["vite.config.ts"]
10+
}

demo/vite.config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineConfig } from 'vite'
2+
import vue from '@vitejs/plugin-vue'
3+
import path from 'path'
4+
5+
// https://vitejs.dev/config/
6+
export default defineConfig({
7+
plugins: [vue()],
8+
resolve: {
9+
alias: [
10+
// Resolve the uncompiled source code for all @scalar packages.
11+
{
12+
find: /^@scalar\/([^/]+)$/,
13+
replacement: path.resolve(__dirname, '../packages/$1/src/index.ts'),
14+
},
15+
],
16+
},
17+
})

packages/snippetz/src/format.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import * as prettier from 'prettier'
1+
import babel from 'prettier/plugins/babel'
2+
import estree from 'prettier/plugins/estree'
3+
import * as prettier from 'prettier/standalone'
24

35
export async function format(source: any) {
46
const target = source.target
@@ -7,6 +9,7 @@ export async function format(source: any) {
79
return await prettier.format(source.code, {
810
semi: false,
911
parser: 'babel',
12+
plugins: [babel, estree],
1013
singleQuote: true,
1114
})
1215
}

packages/snippetz/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default defineConfig({
1515
// Resolve the uncompiled source code for all @scalar packages.
1616
{
1717
find: /^@scalar\/([^/]+)$/,
18-
replacement: path.resolve(__dirname, '../../packages/$1/src/index.ts'),
18+
replacement: path.resolve(__dirname, '../packages/$1/src/index.ts'),
1919
},
2020
],
2121
},

0 commit comments

Comments
 (0)