Skip to content

Commit 89cd758

Browse files
committed
feat: support importMap props in plugin
1 parent 5b67bba commit 89cd758

File tree

3 files changed

+23
-36
lines changed

3 files changed

+23
-36
lines changed

src/VuePreview.vue

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface Props {
2828
useCode?: string
2929
}
3030
}
31-
importMap: Record<string, string>
31+
importMap?: Record<string, string> | string
3232
}
3333
3434
const props = withDefaults(defineProps<Props>(), {
@@ -63,38 +63,22 @@ const msg = ref('vite-plugin-vue-preview')
6363
</template>
6464
`.trim()
6565
66-
if (!props.code) {
67-
store.setFiles({
68-
[defaultMainFile]: welcomeCode,
69-
})
70-
}
71-
else if (props.encode) {
72-
store.setFiles({
73-
[defaultMainFile]: decodeURIComponent(props.code),
74-
})
75-
}
76-
else {
77-
store.setFiles({
78-
[defaultMainFile]: props.code,
79-
})
80-
}
66+
const files: Record<string, string> = {}
8167
82-
if (props.importMap) {
83-
const files = store.getFiles()
84-
const importMapCode = files[importMapFile]
68+
if (!props.code)
69+
files[defaultMainFile] = welcomeCode
70+
else if (props.encode)
71+
files[defaultMainFile] = decodeURIComponent(props.code)
72+
else
73+
files[defaultMainFile] = props.code
8574
86-
if (importMapCode) {
87-
const importMapObj: { imports: Record<string, string> } = JSON.parse(importMapCode!)
88-
Object.assign(importMapObj.imports, props.importMap)
89-
files[importMapFile] = JSON.stringify(importMapObj, null, 2)
90-
}
91-
else {
92-
files[importMapFile] = JSON.stringify({ imports: props.importMap }, null, 2)
93-
}
94-
95-
store.setFiles(files)
75+
if (props.importMap) {
76+
const importMap = typeof props.importMap === 'string' ? JSON.parse(decodeURIComponent(props.importMap)) : props.importMap
77+
files[importMapFile] = JSON.stringify({ imports: importMap }, null, 2)
9678
}
9779
80+
store.setFiles(files)
81+
9882
onMounted(() => {
9983
if (props.clearConsole)
10084
// eslint-disable-next-line no-console

src/plugin.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ function getCode(code: string, options?: VuePreviewPluginOptions) {
99
let expandProps = ''
1010

1111
if (options?.props?.previewBodyStyle)
12-
expandProps += ` previewBodyStyle="${encodeURIComponent(JSON.stringify(options?.props?.previewBodyStyle))}"`
12+
expandProps += ` previewBodyStyle="${encodeURIComponent(JSON.stringify(options.props.previewBodyStyle))}"`
1313
if (options?.props?.previewAppStyle)
14-
expandProps += ` previewAppStyle="${encodeURIComponent(JSON.stringify(options?.props?.previewAppStyle))}"`
14+
expandProps += ` previewAppStyle="${encodeURIComponent(JSON.stringify(options.props.previewAppStyle))}"`
15+
if (options?.props?.importMap)
16+
expandProps += ` importMap="${encodeURIComponent(JSON.stringify(options.props.importMap))}"`
1517

1618
return code.trim() ? `<VuePreview code="${str}" ${expandProps} encode ssr></VuePreview>\n` : '<VuePreview ssr></VuePreview>\n'
1719
}
@@ -20,6 +22,7 @@ interface VuePreviewPluginOptions {
2022
props?: {
2123
previewBodyStyle?: Props['previewBodyStyle']
2224
previewAppStyle?: Props['previewAppStyle']
25+
importMap?: Record<string, string>
2326
}
2427
}
2528

test/vue/src/DemoSfc.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ import { VuePreview } from '../../../src'
44
55
const code = encodeURIComponent(`
66
<script setup>
7-
import { ref } from 'vue'
7+
import { isString } from '@vue/shared'
88
9-
const msg = ref('test props')
9+
const res = isString('@vue/shared')
1010
<\/script>
1111
1212
<template>
13-
<h1>{{ msg }}</h1>
14-
<input v-model="msg">
13+
<h1>Test ImportMap</h1>
14+
<div>Result: {{res}}</div>
1515
</template>
1616
`.trim(),
1717
)
1818
</script>
1919

2020
<template>
2121
<div class="demo-sfc">
22-
<VuePreview :code="code" encode :preview-body-style="{ background: 'red' }" :preview-app-style="{ background: 'yellow' }" />
22+
<VuePreview :code="code" :clear-console="false" encode :import-map="{ '@vue/shared': 'https://unpkg.com/@vue/shared@latest/dist/shared.esm-bundler.js' }" />
2323
</div>
2424
</template>
2525

0 commit comments

Comments
 (0)