Skip to content

Commit 9d39cd4

Browse files
committed
workflow(sfc-playground): support selecting versions
1 parent ad6c124 commit 9d39cd4

File tree

5 files changed

+166
-39
lines changed

5 files changed

+166
-39
lines changed

packages/sfc-playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@types/codemirror": "^0.0.108",
1919
"@vitejs/plugin-vue": "^1.2.0",
2020
"codemirror": "^5.60.0",
21-
"vite": "^2.1.4"
21+
"vite": "^2.1.5"
2222
},
2323
"dependencies": {
2424
"file-saver": "^2.0.5",

packages/sfc-playground/src/App.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ body {
2424
font-size: 13px;
2525
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
2626
Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
27-
color: #444;
27+
color:var(--base);
2828
margin: 0;
2929
background-color: #f8f8f8;
30+
--base: #444;
3031
--nav-height: 50px;
3132
--font-code: 'Source Code Pro', monospace;
3233
--color-branding: #3ca877;

packages/sfc-playground/src/Header.vue

Lines changed: 121 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@
55
<span>Vue SFC Playground</span>
66
</h1>
77
<div class="links">
8-
<a class="commit-link" href="https://app.netlify.com/sites/vue-sfc-playground/deploys" target="_blank">
9-
@{{ commit }}
10-
</a>
8+
<div class="version" @click.stop>
9+
<span class="active-version" @click="toggle">
10+
Version: {{ activeVersion }}
11+
</span>
12+
<ul class="versions" :class="{ expanded }">
13+
<li v-if="!publishedVersions"><a>loading versions...</a></li>
14+
<li v-for="version of publishedVersions">
15+
<a @click="setVueVersion(version)">v{{ version }}</a>
16+
</li>
17+
<li><a @click="resetVueVersion">This Commit ({{ commit }})</a></li>
18+
<li>
19+
<a href="https://app.netlify.com/sites/vue-sfc-playground/deploys" target="_blank">Commits History</a>
20+
</li>
21+
</ul>
22+
</div>
1123
<button class="share" @click="copyLink">
1224
<svg width="1.4em" height="1.4em" viewBox="0 0 24 24">
1325
<g fill="none" stroke="#626262" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
@@ -36,13 +48,56 @@
3648

3749
<script setup lang="ts">
3850
import { downloadProject } from './download/download'
51+
import { setVersion, resetVersion } from './sfcCompiler'
52+
import { ref, onMounted } from 'vue'
3953
4054
const commit = __COMMIT__
55+
const activeVersion = ref(`@${commit}`)
56+
const publishedVersions = ref<string[]>()
57+
const expanded = ref(false)
58+
59+
async function toggle() {
60+
expanded.value = !expanded.value
61+
if (!publishedVersions.value) {
62+
publishedVersions.value = await fetchVersions()
63+
}
64+
}
65+
66+
async function setVueVersion(v: string) {
67+
activeVersion.value = `loading...`
68+
await setVersion(v)
69+
activeVersion.value = `v${v}`
70+
expanded.value = false
71+
}
72+
73+
function resetVueVersion() {
74+
resetVersion()
75+
activeVersion.value = `@${commit}`
76+
expanded.value = false
77+
}
4178
4279
async function copyLink() {
4380
await navigator.clipboard.writeText(___location.href)
4481
alert('Sharable URL has been copied to clipboard.')
4582
}
83+
84+
onMounted(async () => {
85+
window.addEventListener('click', () => {
86+
expanded.value = false
87+
})
88+
})
89+
90+
async function fetchVersions(): Promise<string[]> {
91+
const res = await fetch(
92+
`https://api.github.com/repos/vuejs/vue-next/releases?per_page=100`
93+
)
94+
const releases: any[] = await res.json()
95+
const versions = releases.map(
96+
r => (/^v/.test(r.tag_name) ? r.tag_name.substr(1) : r.tag_name)
97+
)
98+
const minVersion = versions.findIndex(v => v === '3.0.10')
99+
return versions.slice(0, minVersion + 1)
100+
}
46101
</script>
47102

48103
<style>
@@ -74,33 +129,81 @@ h1 img {
74129
top: -2px;
75130
}
76131
77-
@media (max-width:400px) {
132+
@media (max-width: 400px) {
78133
h1 span {
79134
display: none;
80135
}
81136
}
82137
83-
.commit-link {
84-
color: var(--color-branding);
85-
text-decoration: none;
86-
margin-left: 6px;
87-
vertical-align: middle;
88-
line-height: var(--nav-height);
138+
.links {
139+
display: flex;
89140
}
90141
91-
.share {
142+
.version {
143+
display: inline-block;
144+
margin-right: 12px;
92145
position: relative;
93-
top: 6px;
94-
margin: 0 2px;
95146
}
96147
97-
.download {
148+
.active-version {
149+
cursor: pointer;
98150
position: relative;
99-
top: 8px;
100-
margin: 0 2px;
151+
display: inline-block;
152+
vertical-align: middle;
153+
line-height: var(--nav-height);
154+
padding-right: 15px;
155+
}
156+
157+
.active-version:after {
158+
content: '';
159+
width: 0;
160+
height: 0;
161+
border-left: 4px solid transparent;
162+
border-right: 4px solid transparent;
163+
border-top: 6px solid #aaa;
164+
position: absolute;
165+
right: 0;
166+
top: 22px;
167+
}
168+
169+
.version:hover .active-version:after {
170+
border-top-color: var(--base);
101171
}
102172
103-
.commit-link {
104-
margin: 0 5px;
173+
.versions {
174+
display: none;
175+
position: absolute;
176+
left: 0;
177+
top: 40px;
178+
background-color: white;
179+
border: 1px solid #ddd;
180+
border-radius: 4px;
181+
list-style-type: none;
182+
padding: 8px;
183+
margin: 0;
184+
width: 200px;
185+
max-height: calc(100vh - 70px);
186+
overflow: scroll;
187+
}
188+
189+
.versions a {
190+
display: block;
191+
padding: 6px 12px;
192+
text-decoration: none;
193+
cursor: pointer;
194+
color: var(--base);
195+
}
196+
197+
.versions a:hover {
198+
color: var(--color-branding);
199+
}
200+
201+
.versions.expanded {
202+
display: block;
203+
}
204+
205+
.share,
206+
.download {
207+
margin: 0 2px;
105208
}
106209
</style>

packages/sfc-playground/src/sfcCompiler.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
import { store, File } from './store'
2-
import {
3-
parse,
4-
compileTemplate,
5-
compileStyleAsync,
6-
compileScript,
7-
rewriteDefault,
8-
SFCDescriptor,
9-
BindingMetadata
10-
} from '@vue/compiler-sfc'
2+
import { SFCDescriptor, BindingMetadata } from '@vue/compiler-sfc'
3+
import * as defaultCompiler from '@vue/compiler-sfc'
114

125
export const MAIN_FILE = 'App.vue'
136
export const COMP_IDENTIFIER = `__sfc__`
147

8+
/**
9+
* The default SFC compiler we are using is built from each commit
10+
* but we may swap it out with a version fetched from CDNs
11+
*/
12+
let SFCCompiler: typeof defaultCompiler = defaultCompiler
13+
1514
// @ts-ignore
16-
export const SANDBOX_VUE_URL = import.meta.env.PROD
15+
const defaultVueUrl = import.meta.env.PROD
1716
? '/vue.runtime.esm-browser.js' // to be copied on build
1817
: '/src/vue-dev-proxy'
1918

19+
export let SANDBOX_VUE_URL = defaultVueUrl
20+
21+
export async function setVersion(version: string) {
22+
const compilerUrl = `https://unpkg.com/@vue/compiler-sfc@${version}/dist/compiler-sfc.esm-browser.js`
23+
const runtimeUrl = `https://cdn.skypack.dev/@vue/runtime-dom@${version}`
24+
const [compiler] = await Promise.all([
25+
import(/* @vite-ignore */ compilerUrl),
26+
import(/* @vite-ignore */ runtimeUrl)
27+
])
28+
SFCCompiler = compiler
29+
SANDBOX_VUE_URL = runtimeUrl
30+
console.info(`Now using Vue version: ${version}`)
31+
}
32+
33+
export function resetVersion() {
34+
SFCCompiler = defaultCompiler
35+
SANDBOX_VUE_URL = defaultVueUrl
36+
}
37+
2038
export async function compileFile({ filename, code, compiled }: File) {
2139
if (!code.trim()) {
2240
store.errors = []
@@ -30,7 +48,10 @@ export async function compileFile({ filename, code, compiled }: File) {
3048
}
3149

3250
const id = await hashId(filename)
33-
const { errors, descriptor } = parse(code, { filename, sourceMap: true })
51+
const { errors, descriptor } = SFCCompiler.parse(code, {
52+
filename,
53+
sourceMap: true
54+
})
3455
if (errors.length) {
3556
store.errors = errors
3657
return
@@ -121,7 +142,7 @@ export async function compileFile({ filename, code, compiled }: File) {
121142
return
122143
}
123144

124-
const styleResult = await compileStyleAsync({
145+
const styleResult = await SFCCompiler.compileStyleAsync({
125146
source: style.content,
126147
filename,
127148
id,
@@ -156,7 +177,7 @@ function doCompileScript(
156177
): [string, BindingMetadata | undefined] | undefined {
157178
if (descriptor.script || descriptor.scriptSetup) {
158179
try {
159-
const compiledScript = compileScript(descriptor, {
180+
const compiledScript = SFCCompiler.compileScript(descriptor, {
160181
id,
161182
refSugar: true,
162183
inlineTemplate: true,
@@ -173,7 +194,9 @@ function doCompileScript(
173194
2
174195
)} */`
175196
}
176-
code += `\n` + rewriteDefault(compiledScript.content, COMP_IDENTIFIER)
197+
code +=
198+
`\n` +
199+
SFCCompiler.rewriteDefault(compiledScript.content, COMP_IDENTIFIER)
177200
return [code, compiledScript.bindings]
178201
} catch (e) {
179202
store.errors = [e]
@@ -190,7 +213,7 @@ function doCompileTemplate(
190213
bindingMetadata: BindingMetadata | undefined,
191214
ssr: boolean
192215
) {
193-
const templateResult = compileTemplate({
216+
const templateResult = SFCCompiler.compileTemplate({
194217
source: descriptor.template!.content,
195218
filename: descriptor.filename,
196219
id,

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7223,10 +7223,10 @@ [email protected]:
72237223
core-util-is "1.0.2"
72247224
extsprintf "^1.2.0"
72257225

7226-
vite@^2.1.4:
7227-
version "2.1.4"
7228-
resolved "https://registry.yarnpkg.com/vite/-/vite-2.1.4.tgz#66396823701e54cf3bfb9f73dbd386c9b4329c86"
7229-
integrity sha512-j/p0RZQvNY/auSPfazsDfo1PHtFp8ktwXPbzI6NqplzHcR3Cn/dfQWiMxL6zp8j9IWdcJP1Zfms7mxruBhStJw==
7226+
vite@^2.1.5:
7227+
version "2.1.5"
7228+
resolved "https://registry.yarnpkg.com/vite/-/vite-2.1.5.tgz#4857da441c62f7982c83cbd5f42a00330f20c9c1"
7229+
integrity sha512-tYU5iaYeUgQYvK/CNNz3tiJ8vYqPWfCE9IQ7K0iuzYovWw7lzty7KRYGWwV3CQPh0NKxWjOczAqiJsCL0Xb+Og==
72307230
dependencies:
72317231
esbuild "^0.9.3"
72327232
postcss "^8.2.1"

0 commit comments

Comments
 (0)