vuejs / repl

Vue SFC REPL as a Vue 3 component
https://repl-vuejs.vercel.app
MIT License
930 stars 172 forks source link

store.setImportMap doesn't work in VitePress #170

Closed YangTingTing-tech closed 10 months ago

YangTingTing-tech commented 1 year ago

I use @vue/reply in vitepress to show my UI demo, but it doesn't work to setImportMap. The wrong Message in iframe is "Failed to resolve module specifier "dayjs". Tip: edit the "Import Map" tab to specify import paths for dependencies." There already exist a imports about 'element-plus' in Import Map, like this: image

Here is my code in a .vue file.

<script setup>
import { watchEffect, ref } from 'vue'
import { Repl, ReplStore } from '@vue/repl'
import Monaco from '@vue/repl/monaco-editor'
import '@vue/repl/style.css'

const query = new URLSearchParams(location.search)
const store = new ReplStore()

watchEffect(() => history.replaceState({}, '', store.serialize()))

store.setImportMap({
    imports: {
        'element-plus': 'https://unpkg.com/element-plus@2.2.5/es/index.mjs'
    }
})

const customImportStatements = ref([`import ElementPlus from 'element-plus'`])
const customAppUsageCodes = ref([`app.use(ElementPlus)`])
const customHeadTags = ref([
    `<link ref="stylesheet" href="https://unpkg.com/element-plus@2.2.5/dist/index.css">`
])
</script>

<template>
    <Repl :editor="Monaco" :store="store" :clearConsole="false" :previewOptions="{
        headHTML: customHeadTags,
        customCode: {
            importCode: customImportStatements,
            useCode: customAppUsageCodes,
        },
    }" />
</template>

<style>
.vue-repl {
    height: 700px !important;
}
</style>
YangTingTing-tech commented 1 year ago

here is the wrong message in chrome console: image

alexsommers7 commented 11 months ago

I had run into the same issue. It's a bit wonky, but I was able to solve it like so:

const store = new ReplStore();

store.setImportMap({
    imports: {
        myLib: "myLib"
    }
});

const previewOptions = {
    headHTML: '<link rel="stylesheet" href="/myLib.css">',
    customCode: {
        importCode: `import myLib from 'myLib'`,
        useCode: `app.use(myLib)`
    }
};

const defaultFiles = store.getFiles();
delete defaultFiles["App.vue"];
store.setFiles({ ...defaultFiles, "myDefaultComponent.vue": myDefaultComponent}, "myDefaultComponent.vue");
sxzz commented 10 months ago

We temporarily close this due to the lack of enough information. Please provide a minimal reproduction to reopen the issue. Thanks.

Why reproduction is required