Skip to content

Commit 4fb37e4

Browse files
committed
feat(vue-playground): add custom cdn supports
1 parent 0279054 commit 4fb37e4

File tree

9 files changed

+72
-22
lines changed

9 files changed

+72
-22
lines changed

packages/doc-site/.vuepress/plugins/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import {isProd} from '../utils/common'
88
import sandboxPlugin from 'vuepress-plugin-sandbox'
99

1010
const pathResolve = (..._path: string[]) => path.resolve(__dirname, ..._path)
11-
const getPkgUrl = (name: string, version = 'latest', ending = '') =>
12-
`https://cdn.jsdelivr.net/npm/${name}@${version}${ending}`
11+
const getPkgUrl = (name: string, version = 'latest', ending = '') => `https://unpkg.com/${name}@${version}${ending}`
1312

1413
const vuepressPlugins: PluginConfig = [
1514
// for doc search

packages/vue-playground/src/core/compiler/preview-iframe.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@
278278
</script>
279279

280280
<!-- ES Module Shims: Import maps polyfill for modules browsers without import maps support (all except Chrome 89+) -->
281-
<script async src="https://unpkg.com/es-module-shims@0.10.1/dist/es-module-shims.min.js"></script>
281+
<script async src="<!--ES_MODULE_SHIMS_CDN-->"></script>
282282
<script type="importmap">
283283
<!--IMPORT_MAP-->
284284
</script>

packages/vue-playground/src/core/compiler/preview.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import {Store} from '../store'
33
import {PreviewProxy} from './preview-proxy'
44
import PreviewIframe from './preview-iframe.html?raw'
55
import {compileModulesForPreview} from './module-compiler'
6+
import {DEFAULT_ES_MODULE_SHIMS_CDN} from '../../playground/constants'
7+
import type {PlaygroundPkgCdn} from '../../playground/utils/types-helper'
68

79
export interface PreviewOptions {
10+
pkgCdn?: PlaygroundPkgCdn
811
store: Store
912
onBeforeDestroy?: () => void
1013
onBeforeLoad?: () => void
@@ -17,6 +20,7 @@ export class Preview {
1720
sandboxEl?: HTMLIFrameElement
1821
previewProxy?: PreviewProxy
1922
store!: Store
23+
pkgCdn?: PlaygroundPkgCdn
2024
onBeforeDestroy?: () => void
2125
onBeforeLoad?: () => void
2226
onLoad?: () => void
@@ -25,7 +29,9 @@ export class Preview {
2529

2630
constructor(options: PreviewOptions) {
2731
this.store = options.store
32+
this.pkgCdn = options.pkgCdn
2833
this.onBeforeDestroy = options.onBeforeDestroy
34+
this.onBeforeLoad = options.onBeforeLoad
2935
this.onLoad = options.onLoad
3036
this.onError = options.onError
3137
}
@@ -58,7 +64,12 @@ export class Preview {
5864
if (!importMap.imports) importMap.imports = {}
5965
if (!importMap.imports.vue) importMap.imports.vue = this.store.state.vueRuntimeURL
6066

61-
this.sandboxEl.srcdoc = PreviewIframe.replace(/<!--IMPORT_MAP-->/, JSON.stringify(importMap))
67+
this.sandboxEl.srcdoc = PreviewIframe.replace(/<!--IMPORT_MAP-->/, JSON.stringify(importMap)) // inject import map
68+
.replace(
69+
/<!--ES_MODULE_SHIMS_CDN-->/,
70+
this.pkgCdn?.['es-module-shims']?.('0.10.1', '/dist/es-module-shims.min.js') || DEFAULT_ES_MODULE_SHIMS_CDN
71+
) // inject es module shims
72+
6273
container.appendChild(this.sandboxEl)
6374

6475
this.previewProxy = new PreviewProxy({

packages/vue-playground/src/core/store.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {compileFile} from './compiler/transform'
55
import {utoa, atou} from './utils/common'
66
import {ImportMap, OutputModes} from './utils/types-helper'
77
import {SFCScriptCompileOptions, SFCAsyncStyleCompileOptions, SFCTemplateCompileOptions} from 'vue/compiler-sfc'
8+
import {DEFAULT_VUE_RUNTIME_DOM_CDN, DEFAULT_VUE_COMPILER_SFC_CDN} from '../playground/constants'
9+
import type {PlaygroundPkgCdn} from '../playground/utils/types-helper'
810

911
const defaultMainFile = 'App.vue'
1012

@@ -65,32 +67,37 @@ export interface Store {
6567
initialOutputMode: OutputModes
6668
}
6769

70+
interface ReplStoreOptions {
71+
serializedState?: string
72+
initFiles?: File[]
73+
showOutput?: boolean
74+
// loose type to allow getting from the URL without inducing a typing error
75+
outputMode?: OutputModes | string
76+
defaultVueRuntimeURL?: string
77+
pkgCdn?: PlaygroundPkgCdn
78+
initImportMap?: ImportMap
79+
}
80+
6881
export class ReplStore implements Store {
6982
state: StoreState
7083
compiler = defaultCompiler
7184
options?: SFCOptions
7285
initialShowOutput: boolean
7386
initialOutputMode: OutputModes
87+
pkgCdn?: PlaygroundPkgCdn
7488

7589
private defaultVueRuntimeURL: string
7690
private pendingCompiler: Promise<any> | null = null
7791

7892
constructor({
7993
serializedState = '',
80-
defaultVueRuntimeURL = `https://cdn.jsdelivr.net/npm/@vue/runtime-dom@${version}/dist/runtime-dom.esm-browser.js`,
94+
defaultVueRuntimeURL,
8195
showOutput = false,
8296
outputMode = 'preview',
8397
initFiles,
84-
initImportMap
85-
}: {
86-
serializedState?: string
87-
initFiles?: File[]
88-
showOutput?: boolean
89-
// loose type to allow getting from the URL without inducing a typing error
90-
outputMode?: OutputModes | string
91-
defaultVueRuntimeURL?: string
92-
initImportMap?: ImportMap
93-
} = {}) {
98+
initImportMap,
99+
pkgCdn
100+
}: ReplStoreOptions = {}) {
94101
let files: StoreState['files'] = {}
95102

96103
if (serializedState) {
@@ -108,9 +115,14 @@ export class ReplStore implements Store {
108115
}
109116
}
110117

111-
this.defaultVueRuntimeURL = defaultVueRuntimeURL
118+
this.defaultVueRuntimeURL =
119+
defaultVueRuntimeURL ||
120+
pkgCdn?.['@vue/runtime-dom']?.(version, '/dist/runtime-dom.esm-browser.js') ||
121+
DEFAULT_VUE_RUNTIME_DOM_CDN(version)
122+
112123
this.initialShowOutput = showOutput
113124
this.initialOutputMode = outputMode as OutputModes
125+
this.pkgCdn = pkgCdn
114126

115127
let mainFile = defaultMainFile
116128
if (!files[mainFile]) {
@@ -228,8 +240,14 @@ export class ReplStore implements Store {
228240
}
229241

230242
async setVueVersion(version: string) {
231-
const compilerUrl = `https://cdn.jsdelivr.net/npm/@vue/compiler-sfc@${version}/dist/compiler-sfc.esm-browser.js`
232-
const runtimeUrl = `https://cdn.jsdelivr.net/npm/@vue/runtime-dom@${version}/dist/runtime-dom.esm-browser.js`
243+
const compilerUrl =
244+
this.pkgCdn?.['@vue/compiler-sfc']?.(version, '/dist/compiler-sfc.esm-browser.js') ||
245+
DEFAULT_VUE_COMPILER_SFC_CDN(version)
246+
247+
const runtimeUrl =
248+
this.pkgCdn?.['@vue/runtime-dom']?.(version, '/dist/runtime-dom.esm-browser.js') ||
249+
DEFAULT_VUE_RUNTIME_DOM_CDN(version)
250+
233251
this.pendingCompiler = import(/* @vite-ignore */ compilerUrl)
234252
this.compiler = await this.pendingCompiler
235253
this.pendingCompiler = null

packages/vue-playground/src/playground/components/preview.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import {
1919
unref
2020
} from 'vue'
2121
import {Preview} from '../../core/'
22-
import {CLEAR_CONSOLE_INJECT_KEY, STORE_INJECT_KEY, THEME_INJECT_KEY} from '../constants'
22+
import {CLEAR_CONSOLE_INJECT_KEY, PKG_CDN_INJECT_KEY, STORE_INJECT_KEY, THEME_INJECT_KEY} from '../constants'
2323
import {PreviewExpose} from '../utils/types-helper'
2424
2525
const store = inject(STORE_INJECT_KEY)!
2626
const _clearConsole = inject(CLEAR_CONSOLE_INJECT_KEY, false)
2727
const clearConsole = computed(() => unref(_clearConsole))
2828
const theme = inject(THEME_INJECT_KEY)
29+
const pkgCdn = inject(PKG_CDN_INJECT_KEY, {})
2930
3031
const containerRef = ref<HTMLElement>()
3132
const runtimeError = ref()
@@ -37,6 +38,7 @@ let stopUpdateWatcher: WatchStopHandle | undefined
3738
3839
const preview = new Preview({
3940
store: store!,
41+
pkgCdn: unref(pkgCdn),
4042
onBeforeDestroy() {
4143
stopUpdateWatcher?.()
4244
},

packages/vue-playground/src/playground/constants.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {MaybeRef} from '@vueuse/core'
22
import type {InjectionKey} from 'vue'
33
import type {Store} from '../core'
4-
import type {PlaygroundTheme} from './utils/types-helper'
4+
import type {PlaygroundPkgCdn, PlaygroundTheme} from './utils/types-helper'
55

66
export const DEFAULT_TITLE = 'Demo' as const
77
export const PLAYGROUND_COMPONENT_NAME = 'Playground' as const
@@ -14,6 +14,7 @@ export const CLEAR_CONSOLE_INJECT_KEY = '__clear_console__' as unknown as Inject
1414
export const SHOW_IMPORT_MAP_INJECT_KEY = '__show_import_map__' as unknown as InjectionKey<MaybeRef<boolean>>
1515
export const THEME_INJECT_KEY = '__theme__' as unknown as InjectionKey<MaybeRef<PlaygroundTheme>>
1616
export const SHOW_DARK_MODE_INJECT_KEY = '__show_dark_mode__' as unknown as InjectionKey<MaybeRef<boolean>>
17+
export const PKG_CDN_INJECT_KEY = '__pkg_cdn__' as unknown as InjectionKey<MaybeRef<PlaygroundPkgCdn>>
1718

1819
export const DEFAULT_THEME_COLOR = '#42b883'
1920

@@ -78,3 +79,11 @@ export const GET_DARK_THEME = (theme?: PlaygroundTheme) => {
7879
...theme
7980
} as PlaygroundTheme
8081
}
82+
83+
export const DEFAULT_VUE_RUNTIME_DOM_CDN = (version: string) =>
84+
`https://unpkg.com/@vue/runtime-dom@${version}/dist/runtime-dom.esm-browser.js`
85+
86+
export const DEFAULT_VUE_COMPILER_SFC_CDN = (version: string) =>
87+
`https://unpkg.com/@vue/compiler-sfc@${version}/dist/compiler-sfc.esm-browser.js`
88+
89+
export const DEFAULT_ES_MODULE_SHIMS_CDN = 'https://unpkg.com/es-module-shims@0.10.1/dist/es-module-shims.min.js'

packages/vue-playground/src/playground/playground.type.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {PropType, ExtractPropTypes} from 'vue'
22
import type {File, ImportMap} from '../core'
33
import {DEFAULT_TITLE} from './constants'
4-
import type {PlaygroundLifeCycle, PlaygroundThemes} from './utils/types-helper'
4+
import type {PlaygroundLifeCycle, PlaygroundPkgCdn, PlaygroundThemes} from './utils/types-helper'
55

66
export const playgroundProps = () =>
77
({
@@ -24,6 +24,10 @@ export const playgroundProps = () =>
2424
importMap: {
2525
type: Object as PropType<ImportMap>,
2626
default: () => ({})
27+
},
28+
pkgCdn: {
29+
type: Object as PropType<PlaygroundPkgCdn>,
30+
default: () => ({})
2731
}
2832
} as const)
2933

packages/vue-playground/src/playground/playground.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ const editorRef = ref<EditorExpose>()
3131
3232
const store = new ReplStore({
3333
initFiles: props.files,
34-
initImportMap: props.importMap
34+
initImportMap: props.importMap,
35+
pkgCdn: props.pkgCdn
3536
})
3637
3738
const showDarkMode = inject(SHOW_DARK_MODE_INJECT_KEY, undefined)

packages/vue-playground/src/playground/utils/types-helper.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export interface TsLib {
3232
filePath?: string
3333
}
3434

35+
export interface PlaygroundPkgCdn {
36+
'@vue/runtime-dom'?: (version: string, ending: string) => string
37+
'@vue/compiler-sfc'?: (version: string, ending: string) => string
38+
'es-module-shims'?: (version: string, ending: string) => string
39+
}
40+
3541
export type PlaygroundOptions = Partial<PlaygroundProps>
3642

3743
export interface PlaygroundLifeCycle {

0 commit comments

Comments
 (0)