Skip to content

Commit

Permalink
Merge pull request #38 from mayinrain/master
Browse files Browse the repository at this point in the history
upd: 解决文档打包问题
  • Loading branch information
nancyzhan authored Apr 2, 2024
2 parents 205e820 + 7de83c5 commit 55a36e9
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 142 deletions.
14 changes: 6 additions & 8 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
import { defineConfig } from 'vitepress';
import vueJsx from '@vitejs/plugin-vue-jsx';

const path = require('path');
import path from 'path';

const {
genComponentDoc
} = require('./scripts/genComponentDoc');
import { genComponentDoc } from './scripts/genComponentDoc.js';
genComponentDoc();

const ssrTransformCustomDir = () => ({
Expand All @@ -30,13 +28,13 @@ export default defineConfig({
ignored: ['**/docs/.vueepress/components/**']
}
},
// optimizeDeps: {
// exclude: ['@vue/repl']
// },
optimizeDeps: {
exclude: ['@vue/repl']
},
ssr: {
// lodash-es 模块是 esm,ssr 渲染的时候编译成 cjs 的引入方式,会引发 nodejs 的模块加载异常错误
noExternal: ['lodash-es', '@fesjs/fes-design', '@fesjs/fes-design/icon'],
// external: ['@vue/repl']
external: ['@vue/repl']
},
resolve: {
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
Expand Down
30 changes: 15 additions & 15 deletions docs/.vitepress/custom/components/componentDoc.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="component-doc-content">
<slot></slot>
</div>
<div :class="['component-doc-code', visibleCode && 'visible-code']" v-html="code"></div>
<div :class="['component-doc-code', visibleCode && 'visible-code']" v-html="currentCode"></div>
<div class="component-doc-header" @click="toggleCode">
<!-- <LeftOutlined :class="['show-code-btn', visibleCode && 'active']" @click="toggleCode" /> -->
{{visibleCode ? '收起代码' : '查看代码'}}
Expand All @@ -17,31 +17,31 @@
<script setup lang="ts">
import {
watch,
ref
ref,
computed
} from 'vue';
import { DownOutlined } from '@fesjs/fes-design/icon';
import { debounce } from 'lodash-es';
import playground from './playground';
import codes from './demoCode.json';
const props = defineProps({
code: String
codeName: String,
codeSrc: String,
codeFormat: String,
});
const code = ref('');
watch(
() => props.code,
() => {
code.value = codes[`${props.code}-code`];
},
{
immediate: true
}
const currentCode = computed(() =>
decodeURIComponent(props?.codeFormat || ''),
);
const visibleCode = ref(false);
const openPlayground = () => {
playground(props.code);
};
const openPlayground = debounce(() => {
playground({
codeName: props.codeName,
codeSrc: props.codeSrc,
});
}, 500);
const toggleCode = () => {
visibleCode.value = !visibleCode.value;
Expand Down
42 changes: 42 additions & 0 deletions docs/.vitepress/custom/components/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export const DEMO_ENTRY_FILE = `
<template>
<Demo />
</template>
<script setup>
import {setupFesDesign } from './fes-design.js';
import Demo from './demo.vue';
setupFesDesign();
</script>
`;

export const FES_DESIGN_SETUP = `
// 请谨慎修改内容!!!
import { getCurrentInstance } from 'vue';
import FesDesign from '@fesjs/fes-design';
import * as Icons from '@fesjs/fes-design/icon';
export function loadStyle() {
const hasLinks = document.querySelectorAll('link');
for(let l of hasLinks) {
if (/fes-design.min.css/.test(l.href)) return;
}
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = 'https://registry.npmmirror.com/@fesjs/fes-design/latest/files/dist/fes-design.min.css';
document.head.appendChild(link)
const twcsslink = document.createElement('link')
twcsslink.rel = 'stylesheet'
twcsslink.href = 'https://cdn.jsdelivr.net/npm/@fesjs/traction-widget/dist/traction-widget.min.css';
document.head.appendChild(twcsslink)
}
export function setupFesDesign() {
loadStyle();
const instance = getCurrentInstance()
instance.appContext.app.use(FesDesign);
Object.keys(Icons).forEach((iconName) => {
instance.appContext.app.component(iconName, Icons[iconName]);
});
}
`;
154 changes: 70 additions & 84 deletions docs/.vitepress/custom/components/exampleRepl.vue
Original file line number Diff line number Diff line change
@@ -1,110 +1,96 @@
<template>
<FDrawer v-model:show="isShow" title="Playground" displayDirective="show" width="90%" @ok="isShow = false">
<Repl :store="store" :showImportMap="true" :showCompileOutput="false" :clearConsole="false" />
</FDrawer>
<FDrawer v-model:show="isShow" title="Playground" displayDirective="show" width="90%" @ok="isShow = false"
>
<Repl
v-if="props.codeName"
:editor="CodeMirror"
:store="store"
:show-ts-config="false"
:show-import-map="true"
:show-compile-output="false"
:clear-console="false"
/>
</FDrawer>
</template>
<script setup lang="ts">
import {
version,
watch,
ref
} from 'vue';

<script setup>
import { ref, version, watch } from 'vue';
import CodeMirror from '@vue/repl/codemirror-editor';
import { Repl, ReplStore } from '@vue/repl';
import { FDrawer } from '@fesjs/fes-design';
import {
Repl,
ReplStore
} from '@vue/repl';
import data from './demoCode.json';
import '@vue/repl/style.css';
import { DEMO_ENTRY_FILE, FES_DESIGN_SETUP } from './constants';
const props = defineProps({
codeName: String
codeName: String,
codeSrc: String,
});
const mainFile = 'App.vue';
const demoFile = 'demo.vue';
const mainFile = 'src/App.vue';
const demoFile = 'src/demo.vue';
const store = new ReplStore({
defaultVueRuntimeURL: `https://npm.elemecdn.com/vue@${version}/dist/vue.esm-browser.js`
defaultVueRuntimeURL: `https://registry.npmmirror.com/vue/${version}/files/dist/vue.esm-browser.js`,
});
const fesDesignSetup = `
// 不要修改此文件!!!
import { getCurrentInstance } from 'vue';
import Space from './space.vue';
import FesDesign from '@fesjs/fes-design';
import * as Icons from '@fesjs/fes-design/icon';
export function loadStyle() {
const hasLinks = document.querySelectorAll('link');
for(let l of hasLinks) {
if (/fes-design.min.css/.test(l.href)) return;
}
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = 'https://npm.elemecdn.com/@fesjs/fes-design@latest/dist/fes-design.min.css';
document.head.appendChild(link)
const twcsslink = document.createElement('link')
twcsslink.rel = 'stylesheet'
twcsslink.href = 'https://cdn.jsdelivr.net/npm/@fesjs/traction-widget/dist/traction-widget.min.css';
document.head.appendChild(twcsslink)
}
export function setupFesDesign() {
loadStyle();
const instance = getCurrentInstance()
instance.appContext.app.use(FesDesign);
Object.keys(Icons).forEach((iconName) => {
instance.appContext.app.component(iconName, Icons[iconName]);
});
instance.appContext.app.component('Space', Space)
}
`;
function resolveSFCExample (demo) {
const files = {
[mainFile]: data.app,
'space.vue': data.space,
[demoFile]: data[demo],
'fes-design.js': fesDesignSetup,
'import-map.json': JSON.stringify({
imports: {
'@fesjs/fes-design': 'https://npm.elemecdn.com/@fesjs/fes-design@latest/dist/fes-design.esm-browser.js',
'@fesjs/fes-design/icon': 'https://npm.elemecdn.com/@fesjs/fes-design@latest/dist/fes-design.icon-browser.js',
'@fesjs/traction-widget': 'https://cdn.jsdelivr.net/npm/@fesjs/traction-widget/dist/traction-widget.esm-browser.js'
}
})
};
return files;
const defaultFiles = {
[mainFile]: DEMO_ENTRY_FILE,
'src/fes-design.js': FES_DESIGN_SETUP,
'import-map.json': JSON.stringify({
imports: {
'@fesjs/fes-design':
'https://registry.npmmirror.com/@fesjs/fes-design/latest/files/dist/fes-design.esm-browser.js',
'@fesjs/fes-design/icon':
'https://registry.npmmirror.com/@fesjs/fes-design/latest/files/dist/fes-design.icon-browser.js',
'@fesjs/traction-widget':
'https://cdn.jsdelivr.net/npm/@fesjs/traction-widget/dist/traction-widget.esm-browser.js'
},
}),
};
store.setFiles(defaultFiles);
store.setActive(mainFile);
function resolveSFCExample(codeSrc) {
defaultFiles[demoFile] = codeSrc;
return defaultFiles;
}
function updateExample (fileName) {
store.setFiles(resolveSFCExample(fileName), mainFile).then(() => {
function updateExample(codeSrc) {
store.setFiles(resolveSFCExample(codeSrc), mainFile).then(() => {
store.setActive(demoFile);
});
}
watch(
() => props.codeSrc,
() => {
updateExample(decodeURIComponent(props.codeSrc));
},
{
immediate: true,
},
);
const isShow = ref(true);
const handleShow = (val) => {
function handleShow(val) {
isShow.value = val;
};
}
defineExpose({
isShow,
handleShow
});
watch(() => props.codeName, () => {
updateExample(props.codeName);
}, {
immediate: true
handleShow,
});
</script>
<style lang="less" scoped>

<style lang="less">
.vue-repl {
border-right: 1px solid var(--vt-c-divider-light);
height: calc(100vh - 88px);
border-right: 1px solid var(--vt-c-divider-light);
height: calc(100vh - 55px); // 55px 是 drawer header 的宽度
}
.fes-repl-drawer-content {
.fes-drawer-body-container {
padding-top: 0;
padding-right: 0;
padding-bottom: 0;
}
}
</style>
40 changes: 23 additions & 17 deletions docs/.vitepress/custom/components/playground.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { createVNode, render } from 'vue'
import ExampleRepl from './exampleRepl.vue'
import { createVNode, defineAsyncComponent, render } from 'vue';

let vm
export default function playground (codeName) {
console.log('codeName', codeName)
if (vm) {
vm.component.exposed.handleShow(true)
vm.component.props.codeName = codeName
return vm
}
const container = document.createElement('div')
vm = createVNode(ExampleRepl, {
codeName
})
render(vm, container)
const AsyncExampleRepl = defineAsyncComponent({
loader: () => import('./exampleRepl.vue'),
});

document.body.appendChild(container)
return vm
let vm;
export default function playground({ codeName, codeSrc }) {
if (vm) {
const exampleReplComponent = vm.component.subTree.component;
exampleReplComponent.props.codeName = codeName;
exampleReplComponent.props.codeSrc = codeSrc;
exampleReplComponent.exposed.handleShow(true);
return vm;
}
const container = document.createElement('div');
vm = createVNode(AsyncExampleRepl, {
codeName,
codeSrc,
});

render(vm, container);

document.body.appendChild(container);
return vm;
}
20 changes: 12 additions & 8 deletions docs/.vitepress/scripts/genComponentDoc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
const shiki = require('shiki');
const cheapWatch = require('cheap-watch');
import path from 'path';
import fs from 'fs';
import fse from 'fs-extra';
import shiki from 'shiki';
import cheapWatch from 'cheap-watch';

const SCRIPT_TEMPLATE = `
<script>
Expand Down Expand Up @@ -54,11 +54,15 @@ function genOutputPath (name) {
return path.join(process.cwd(), `./docs/components/${name}.md`);
}

function handleCompDoc (compCode, compName, demoName) {
function handleCompDoc(compCode, compName, demoName) {
const codeName = `${compName}.${demoName}`;
const codeSrc = encodeURIComponent(code[`${codeName}`]);
const codeFormat = encodeURIComponent(code[`${codeName}-code`]);

return compCode.replace(
/<template>([\s\S]*)<\/template>/,
(match, p1) =>
`<template><ComponentDoc code="${compName}.${demoName}"><ClientOnly>${p1}</ClientOnly></ComponentDoc></template>`
`<template><ComponentDoc codeName="${codeName}" codeSrc="${codeSrc}" codeFormat="${codeFormat}"><ClientOnly>${p1}</ClientOnly></ComponentDoc></template>`,
);
}

Expand Down Expand Up @@ -236,7 +240,7 @@ async function watch (src) {
watcher.on('-', handleDelete);
}

exports.genComponentDoc = async (useWatch = true) => {
export const genComponentDoc = async (useWatch = true) => {
const src = path.join(process.cwd(), './docs/.vitepress/components');
await genComponents(src);

Expand Down
3 changes: 3 additions & 0 deletions docs/.vitepress/scripts/generate-doc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { genComponentDoc } from './genComponentDoc.js';

genComponentDoc(false);
Loading

0 comments on commit 55a36e9

Please sign in to comment.