workflow: add ssr mode in template explorer

This commit is contained in:
Evan You 2020-02-04 15:59:37 -05:00
parent 98e3e03fd1
commit e71781dcab
2 changed files with 48 additions and 24 deletions

View File

@ -1,6 +1,7 @@
import * as m from 'monaco-editor' import * as m from 'monaco-editor'
import { compile, CompilerError } from '@vue/compiler-dom' import { compile, CompilerError, CompilerOptions } from '@vue/compiler-dom'
import { compilerOptions, initOptions } from './options' import { compile as ssrCompile } from '@vue/compiler-ssr'
import { compilerOptions, initOptions, ssrMode } from './options'
import { watch } from '@vue/runtime-dom' import { watch } from '@vue/runtime-dom'
import { SourceMapConsumer } from 'source-map' import { SourceMapConsumer } from 'source-map'
@ -12,14 +13,21 @@ declare global {
} }
} }
interface PersistedState {
src: string
ssr: boolean
options: CompilerOptions
}
window.init = () => { window.init = () => {
const monaco = window.monaco const monaco = window.monaco
const persistedState = JSON.parse( const persistedState: PersistedState = JSON.parse(
decodeURIComponent(window.location.hash.slice(1)) || decodeURIComponent(window.location.hash.slice(1)) ||
localStorage.getItem('state') || localStorage.getItem('state') ||
`{}` `{}`
) )
ssrMode.value = persistedState.ssr
Object.assign(compilerOptions, persistedState.options) Object.assign(compilerOptions, persistedState.options)
let lastSuccessfulCode: string = `/* See console for error */` let lastSuccessfulCode: string = `/* See console for error */`
@ -28,7 +36,8 @@ window.init = () => {
console.clear() console.clear()
try { try {
const errors: CompilerError[] = [] const errors: CompilerError[] = []
const { code, ast, map } = compile(source, { const compileFn = ssrMode.value ? ssrCompile : compile
const { code, ast, map } = compileFn(source, {
filename: 'template.vue', filename: 'template.vue',
...compilerOptions, ...compilerOptions,
sourceMap: true, sourceMap: true,
@ -69,8 +78,9 @@ window.init = () => {
// every time we re-compile, persist current state // every time we re-compile, persist current state
const state = JSON.stringify({ const state = JSON.stringify({
src, src,
ssr: ssrMode.value,
options: compilerOptions options: compilerOptions
}) } as PersistedState)
localStorage.setItem('state', state) localStorage.setItem('state', state)
window.location.hash = encodeURIComponent(state) window.location.hash = encodeURIComponent(state)
const res = compileCode(src) const res = compileCode(src)

View File

@ -1,6 +1,8 @@
import { h, reactive, createApp } from '@vue/runtime-dom' import { h, reactive, createApp, ref } from 'vue'
import { CompilerOptions } from '@vue/compiler-dom' import { CompilerOptions } from '@vue/compiler-dom'
export const ssrMode = ref(false)
export const compilerOptions: CompilerOptions = reactive({ export const compilerOptions: CompilerOptions = reactive({
mode: 'module', mode: 'module',
prefixIdentifiers: false, prefixIdentifiers: false,
@ -12,8 +14,11 @@ export const compilerOptions: CompilerOptions = reactive({
const App = { const App = {
setup() { setup() {
return () => { return () => {
const isSSR = ssrMode.value
const isModule = compilerOptions.mode === 'module'
const usePrefix = const usePrefix =
compilerOptions.prefixIdentifiers || compilerOptions.mode === 'module' compilerOptions.prefixIdentifiers || compilerOptions.mode === 'module'
return [ return [
h('h1', `Vue 3 Template Explorer`), h('h1', `Vue 3 Template Explorer`),
h( h(
@ -24,6 +29,7 @@ const App = {
}, },
`@${__COMMIT__}` `@${__COMMIT__}`
), ),
h('div', { id: 'options' }, [ h('div', { id: 'options' }, [
// mode selection // mode selection
h('span', { class: 'options-group' }, [ h('span', { class: 'options-group' }, [
@ -32,7 +38,7 @@ const App = {
type: 'radio', type: 'radio',
id: 'mode-module', id: 'mode-module',
name: 'mode', name: 'mode',
checked: compilerOptions.mode === 'module', checked: isModule,
onChange() { onChange() {
compilerOptions.mode = 'module' compilerOptions.mode = 'module'
} }
@ -42,7 +48,7 @@ const App = {
type: 'radio', type: 'radio',
id: 'mode-function', id: 'mode-function',
name: 'mode', name: 'mode',
checked: compilerOptions.mode === 'function', checked: !isModule,
onChange() { onChange() {
compilerOptions.mode = 'function' compilerOptions.mode = 'function'
} }
@ -50,16 +56,27 @@ const App = {
h('label', { for: 'mode-function' }, 'function') h('label', { for: 'mode-function' }, 'function')
]), ]),
// SSR
h('input', {
type: 'checkbox',
id: 'ssr',
name: 'ssr',
checked: ssrMode.value,
onChange(e: Event) {
ssrMode.value = (e.target as HTMLInputElement).checked
}
}),
h('label', { for: 'ssr' }, 'SSR'),
// toggle prefixIdentifiers // toggle prefixIdentifiers
h('input', { h('input', {
type: 'checkbox', type: 'checkbox',
id: 'prefix', id: 'prefix',
disabled: compilerOptions.mode === 'module', disabled: isModule || isSSR,
checked: usePrefix, checked: usePrefix || isSSR,
onChange(e: Event) { onChange(e: Event) {
compilerOptions.prefixIdentifiers = compilerOptions.prefixIdentifiers =
(<HTMLInputElement>e.target).checked || (e.target as HTMLInputElement).checked || isModule
compilerOptions.mode === 'module'
} }
}), }),
h('label', { for: 'prefix' }, 'prefixIdentifiers'), h('label', { for: 'prefix' }, 'prefixIdentifiers'),
@ -68,9 +85,10 @@ const App = {
h('input', { h('input', {
type: 'checkbox', type: 'checkbox',
id: 'hoist', id: 'hoist',
checked: compilerOptions.hoistStatic, checked: compilerOptions.hoistStatic && !isSSR,
disabled: isSSR,
onChange(e: Event) { onChange(e: Event) {
compilerOptions.hoistStatic = (<HTMLInputElement>e.target).checked compilerOptions.hoistStatic = (e.target as HTMLInputElement).checked
} }
}), }),
h('label', { for: 'hoist' }, 'hoistStatic'), h('label', { for: 'hoist' }, 'hoistStatic'),
@ -79,12 +97,10 @@ const App = {
h('input', { h('input', {
type: 'checkbox', type: 'checkbox',
id: 'cache', id: 'cache',
checked: usePrefix && compilerOptions.cacheHandlers, checked: usePrefix && compilerOptions.cacheHandlers && !isSSR,
disabled: !usePrefix, disabled: !usePrefix || isSSR,
onChange(e: Event) { onChange(e: Event) {
compilerOptions.cacheHandlers = (<HTMLInputElement>( compilerOptions.cacheHandlers = (e.target as HTMLInputElement).checked
e.target
)).checked
} }
}), }),
h('label', { for: 'cache' }, 'cacheHandlers'), h('label', { for: 'cache' }, 'cacheHandlers'),
@ -93,13 +109,11 @@ const App = {
h('input', { h('input', {
type: 'checkbox', type: 'checkbox',
id: 'scope-id', id: 'scope-id',
disabled: compilerOptions.mode !== 'module', disabled: !isModule,
checked: checked: isModule && compilerOptions.scopeId,
compilerOptions.mode === 'module' && compilerOptions.scopeId,
onChange(e: Event) { onChange(e: Event) {
compilerOptions.scopeId = compilerOptions.scopeId =
compilerOptions.mode === 'module' && isModule && (e.target as HTMLInputElement).checked
(<HTMLInputElement>e.target).checked
? 'scope-id' ? 'scope-id'
: null : null
} }