Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(site): failed to launch codesandbox demo #3346

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions site/plugin-tdoc/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export default function renderDemo(md, container) {
const tpl = `
<td-doc-demo demo-name="${demoName}" languages="OptionsAPI,CompositionAPI" :data-OptionsAPI="${demoCodeDefName}" :data-CompositionAPI="${demoCompositionCodeDefName}" language="markup">
<div slot="action">
<Stackblitz demo-name="${demoName}" component-name="${componentName}" />
<Codesandbox demo-name="${demoName}" component-name="${componentName}" />
<Stackblitz demo-name="${demoName}" component-name="${componentName.trim()}" />
<Codesandbox demo-name="${demoName}" component-name="${componentName.trim()}" />
</div>
<div class="tdesign-demo-item__body">
<${demoDefName} />
Expand Down
81 changes: 12 additions & 69 deletions site/src/components/codesandbox/content.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import orgPkg from '../../../../package.json';

export const htmlContent = `
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
`;
export const htmlContent = `<div id="app"></div>`;

export const mainJsContent = `
import Vue from 'vue';
Expand Down Expand Up @@ -45,86 +42,32 @@ export const styleContent = `
}
`;

export const stackblitzRc = `
{
"installDependencies": false,
"startCommand": "turbo && turbo dev"
}
`;

export const viteConfigContent = `
import { defineConfig } from 'vite';
import { createVuePlugin } from 'vite-plugin-vue2';

export default defineConfig({
plugins: [
createVuePlugin({ jsx: true }),
],
});
`;

export const viteConfigContentForComposition = `
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue2'
import vueJsx from '@vitejs/plugin-vue2-jsx';

export default defineConfig({
plugins: [
vue(),
vueJsx()
],
});
`;

export const packageJSONContent = JSON.stringify(
{
name: 'tdesign-vue-demo',
version: '0.0.0',
private: true,
scripts: {
dev: 'vite',
build: 'vite build',
serve: 'vite preview',
},
export const packageJSONContent = (name) => {
return {
name: name,
dependencies: {
'tdesign-vue': orgPkg.version,
'tdesign-icons-vue': orgPkg.dependencies['tdesign-icons-vue'],
vue: orgPkg.devDependencies.vue,
},
devDependencies: {
vite: orgPkg.devDependencies.vite,
less: orgPkg.devDependencies.less,
'vite-plugin-vue2': orgPkg.devDependencies['vite-plugin-vue2'],
'@vue/cli-plugin-babel': '~4.5.0',
'vue-template-compiler': orgPkg.devDependencies['vue-template-compiler'],
},
},
null,
2,
);
};
};

export const packageJSONContentForComposition = JSON.stringify(
{
name: 'tdesign-vue-demo',
version: '0.0.0',
private: true,
scripts: {
dev: 'vite',
build: 'vite build',
serve: 'vite preview',
},
export const packageJSONContentForComposition = (name) => {
return {
name: name,
dependencies: {
'tdesign-vue': orgPkg.version + '-naruto',
'tdesign-icons-vue': orgPkg.dependencies['tdesign-icons-vue'],
vue: '2.7.14',
},
devDependencies: {
vite: '^2.9.6',
less: orgPkg.devDependencies.less,
'@vitejs/plugin-vue2': '^1.1.2',
'vue-template-compiler': '2.7.14',
'@vitejs/plugin-vue2-jsx': '^1.1.0',
},
},
null,
2,
);
};
};
14 changes: 6 additions & 8 deletions site/src/components/codesandbox/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import {
mainJsContent,
styleContent,
packageJSONContent,
viteConfigContent,
packageJSONContentForComposition,
viteConfigContentForComposition,
} from './content';

export default {
Expand All @@ -44,10 +42,13 @@ export default {
onRunOnline() {
const tdDocDemoDom = document.querySelector(`td-doc-demo[demo-name='${this.demoName}']`);
const code = tdDocDemoDom.currentRenderCode;

const codeSandboxTitle = `tdesign-vue-${this.componentName}-${this.demoName}-demo`;
const currentLangIndex = tdDocDemoDom.currentLangIndex;
const pkgJson = currentLangIndex === 0 ? packageJSONContent : packageJSONContentForComposition;
const viteConfig = currentLangIndex === 0 ? viteConfigContent : viteConfigContentForComposition;
const pkgJson =
currentLangIndex === 0
? packageJSONContent(codeSandboxTitle)
: packageJSONContentForComposition(codeSandboxTitle);

this.loading = true;

fetch('https://codesandbox.io/api/v1/sandboxes/define?json=1', {
Expand All @@ -61,9 +62,6 @@ export default {
'package.json': {
content: pkgJson,
},
'vite.config.js': {
content: viteConfig,
},
'index.html': {
content: htmlContent,
},
Expand Down
Loading