diff --git a/common/play/index.js b/common/play/index.js index a536ea3..40b3233 100644 --- a/common/play/index.js +++ b/common/play/index.js @@ -1,18 +1,58 @@ import { loading } from 'dls-graphics' +import sdk from '@stackblitz/sdk' /* eslint-disable import/no-webpack-loader-syntax */ -import meta from '!!raw-loader!./templates/package.raw' -import prettier from '!!raw-loader!./templates/.prettierrc.raw' -import index from '!!raw-loader!./templates/index.raw.html' -import landing from '!!raw-loader!./templates/landing.raw.html' -import main from '!!raw-loader!./templates/main.raw.js' -import app from '!!raw-loader!./templates/App.raw.vue' +import packageCodeSandbox from '!!raw-loader!./templates/package.codesandbox' +import packageStackBlitz from '!!raw-loader!./templates/package.stackblitz' +import rcStackBlitz from '!!raw-loader!./templates/.stackblitzrc' +import vueConfig from '!!raw-loader!./templates/vue.config.js' +import prettier from '!!raw-loader!./templates/.prettierrc' +import index from '!!raw-loader!./templates/index.html' +import landing from '!!raw-loader!./templates/landing.html' +import main from '!!raw-loader!./templates/main.js' +import app from '!!raw-loader!./templates/App.vue' /* eslint-enable import/no-webpack-loader-syntax */ +const PLAYGROUND_FILES = { + CodeSandbox: { + 'package.json': packageCodeSandbox, + '.prettierrc': prettier, + 'public/index.html': index, + 'src/main.js': main, + 'src/App.vue': app + }, + StackBlitz: { + 'package.json': packageStackBlitz, + '.prettierrc': prettier, + 'public/index.html': index, + 'src/main.js': main, + 'src/App.vue': app, + '.stackblitzrc': rcStackBlitz, + 'vue.config.js': vueConfig + } +} + +const PLAY_IMPLS = { + CodeSandbox: createCodeSandbox, + StackBlitz: createStackBlitz +} + +export function play (sfc, { locale, vendor }) { + PLAY_IMPLS[vendor](sfc, { locale }) +} + const API_CSB = 'https://codesandbox.io/api/v1/sandboxes/define' export function createCodeSandbox (sfc, { locale }) { const win = window.open() win.document.write(landing.replace('{{loading}}', genLoading())) + + const files = Object.entries( + getPlaygroundFiles({ locale, vendor: 'CodeSandbox' }) + ).reduce((acc, [key, value]) => { + acc[key] = { content: value } + return acc + }, {}) + fetch(`${API_CSB}?json=1`, { method: 'POST', headers: { @@ -21,9 +61,9 @@ export function createCodeSandbox (sfc, { locale }) { }, body: JSON.stringify({ files: { - ...getTemplateFiles({ locale }), + ...files, 'src/Demo.vue': { - content: sfc + content: sfc.replace(/from 'veui'/g, `from 'veui/dist/veui.esm'`) } } }) @@ -34,24 +74,31 @@ export function createCodeSandbox (sfc, { locale }) { }) } -function getTemplateFiles ({ locale }) { - return { - 'package.json': { - content: meta +export function createStackBlitz (sfc, { locale }) { + sdk.openProject( + { + title: 'VEUI Playground', + files: { + ...getPlaygroundFiles({ locale, vendor: 'StackBlitz' }), + 'src/Demo.vue': sfc + }, + description: 'Online playground for VEUI.', + template: 'node' }, - '.prettierrc': { - content: prettier - }, - 'public/index.html': { - content: index - }, - 'src/main.js': { - content: main.replace('{{locale}}', locale) - }, - 'src/App.vue': { - content: app + { + openFile: 'src/Demo.vue' } - } + ) +} + +function getPlaygroundFiles ({ locale, vendor }) { + return Object.entries(PLAYGROUND_FILES[vendor]).reduce( + (acc, [key, value]) => { + acc[key] = value.replace('{{locale}}', locale) + return acc + }, + {} + ) } function genLoading () { diff --git a/common/play/templates/.prettierrc.raw b/common/play/templates/.prettierrc similarity index 100% rename from common/play/templates/.prettierrc.raw rename to common/play/templates/.prettierrc diff --git a/common/play/templates/.stackblitzrc b/common/play/templates/.stackblitzrc new file mode 100644 index 0000000..a6e32d6 --- /dev/null +++ b/common/play/templates/.stackblitzrc @@ -0,0 +1,4 @@ +{ + "installDependencies": true, + "startCommand": "npx vue-cli-service serve" +} diff --git a/common/play/templates/App.raw.vue b/common/play/templates/App.vue similarity index 100% rename from common/play/templates/App.raw.vue rename to common/play/templates/App.vue diff --git a/common/play/templates/index.raw.html b/common/play/templates/index.html similarity index 90% rename from common/play/templates/index.raw.html rename to common/play/templates/index.html index 65413b2..5af9f94 100644 --- a/common/play/templates/index.raw.html +++ b/common/play/templates/index.html @@ -1,5 +1,5 @@ - +
diff --git a/common/play/templates/landing.raw.html b/common/play/templates/landing.html similarity index 100% rename from common/play/templates/landing.raw.html rename to common/play/templates/landing.html diff --git a/common/play/templates/main.raw.js b/common/play/templates/main.js similarity index 100% rename from common/play/templates/main.raw.js rename to common/play/templates/main.js diff --git a/common/play/templates/package.raw b/common/play/templates/package.codesandbox similarity index 100% rename from common/play/templates/package.raw rename to common/play/templates/package.codesandbox diff --git a/common/play/templates/package.stackblitz b/common/play/templates/package.stackblitz new file mode 100644 index 0000000..ce244e2 --- /dev/null +++ b/common/play/templates/package.stackblitz @@ -0,0 +1,12 @@ +{ + "dependencies": { + "veui": "^2.1.3", + "veui-theme-dls": "^2.1.3", + "veui-theme-dls-icons": "^2.1.3", + "vue": "^2.6.14" + }, + "devDependencies": { + "@vue/cli-service": "~5.0.0-beta.4", + "vue-template-compiler": "^2.6.14" + } +} diff --git a/common/play/templates/vue.config.js b/common/play/templates/vue.config.js new file mode 100644 index 0000000..782276c --- /dev/null +++ b/common/play/templates/vue.config.js @@ -0,0 +1,5 @@ +module.exports = { + chainWebpack (config) { + config.resolve.alias.set('veui$', 'veui/dist/veui.esm') + } +} diff --git a/components/OneDemo.vue b/components/OneDemo.vue index 27fa578..f0b5d32 100644 --- a/components/OneDemo.vue +++ b/components/OneDemo.vue @@ -22,13 +22,21 @@