Skip to content

Commit

Permalink
fix: wip - added loadComponent method
Browse files Browse the repository at this point in the history
  • Loading branch information
kouts committed Aug 16, 2024
1 parent fbcac38 commit 2b25d2e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
9 changes: 5 additions & 4 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { defaultTheme } from '@vuepress/theme-default'
import { defineUserConfig } from 'vuepress'
import { searchPlugin } from '@vuepress/plugin-search'
import { viteBundler } from '@vuepress/bundler-vite'
// import { vueExamplePlugin } from '../../src/index.js'
import { vueExamplePlugin } from '../../src/index.js'
import { fileURLToPath } from 'url'

const examplesDir = fileURLToPath(new URL('../', import.meta.url)) + '.examples/'

export default defineUserConfig({
bundler: viteBundler(),
Expand All @@ -27,11 +30,9 @@ export default defineUserConfig({
]
}),
plugins: [
/*
vueExamplePlugin({
componentsPath: '/docs/.examples/'
componentsPath: examplesDir
}),
*/
searchPlugin({
// options
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@vuepress/client": "2.0.0-rc.2",
"@vuepress/plugin-search": "2.0.0-rc.41",
"@vuepress/theme-default": "2.0.0-rc.41",
"@vuepress/utils": "2.0.0-rc.14",
"eslint": "^8.57.0",
"husky": "^8.0.3",
"jest-raw-loader": "^1.0.1",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/VueExample.vue
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@

<script>
// SVG icons from // https://tablericons.com/
import VueExampleHighlight from './VueExampleHighlight'
import VueExampleHighlight from './VueExampleHighlight.vue'
import { loadComponent, loadComponentAsString } from '@temp/loadComponent'
import { markRaw } from 'vue'
Expand Down Expand Up @@ -165,14 +165,14 @@ export default {
},
created() {
this.createComponent()
this.createSections()
// this.createSections()
this.expanded = this.startExpanded
},
methods: {
createComponent() {
loadComponent(this.$props.file).then((component) => {
this.component = markRaw(component)
})
async createComponent() {
const res = await loadComponent(this.$props.file)
this.component = markRaw(res)
},
createSections() {
loadComponentAsString(this.$props.file).then((contents) => {
Expand Down
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@ export const vueExamplePlugin = (options) => {
return {
name: 'vue-example',
async onPrepared(app) {
const opts = Object.assign({}, { componentsPath: '/docs/.vuepress/components' }, options)
const opts = Object.assign({}, { componentsPath: '' }, options)

await app.writeTemp(
'loadComponent.js',
`
import { defineAsyncComponent } from 'vue';
export function loadComponent (file) {
try {
return import('${opts.componentsPath}' + file + '.vue').then(component => component.default);
return defineAsyncComponent(() => import(/* @vite-ignore */ '${opts.componentsPath}' + file + '.vue'));
} catch (err) {
console.log(err);
}
}
export function loadComponentAsString (file) {
/*
try {
return import(/* webpackChunkName: "vue-examples-source" */ /* webpackMode: "lazy-once" */ '!raw-loader!${opts.componentsPath}' + file + '.vue')
.then(component => component.default);
return import('${opts.componentsPath}' + file + '.vue?raw')
} catch (err) {
console.log(err);
}
*/
return 'test';
}
`
)
Expand Down

0 comments on commit 2b25d2e

Please sign in to comment.