From ace258079ea820972d643224cd15e07e3474c3bf Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Sat, 6 Nov 2021 10:08:21 +0800 Subject: [PATCH] implement virtual example plugin This vite plugin will read from the examples directory and compose all the example manifests into a JSON. --- examples/box/index.json | 117 ++++++++++++++++++++++++++++++++ packages/runtime/index.html | 20 ++++++ packages/runtime/vite.config.ts | 41 ++++++++++- 3 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 examples/box/index.json create mode 100644 packages/runtime/index.html diff --git a/examples/box/index.json b/examples/box/index.json new file mode 100644 index 00000000..417513bd --- /dev/null +++ b/examples/box/index.json @@ -0,0 +1,117 @@ +{ + "app": { + "version": "example/v1", + "metadata": { "name": "box", "description": "box" }, + "spec": { + "components": [ + { + "id": "test_btn", + "type": "plain/v1/button", + "properties": { "text": { "raw": "Click", "format": "plain" } }, + "traits": [ + { + "type": "core/v1/state", + "properties": { "key": "count", "initialValue": 0 } + }, + { + "type": "core/v1/event", + "properties": { + "handlers": [ + { + "type": "onClick", + "componentId": "test_btn", + "method": { + "name": "setValue", + "parameters": { + "key": "count", + "value": "{{ test_btn.count > 0 ? 0 : test_btn.count + 1 }}" + } + }, + "wait": {}, + "disabled": false + } + ] + } + } + ] + }, + { + "id": "box_container", + "type": "chakra_ui/v1/box", + "properties": { + "my": 8, + "p": 8, + "display": "flex", + "bg": "{{ test_btn.count % 2 ? 'teal' : 'white' }}", + "border": "1px solid", + "borderColor": "{{ test_btn.count % 2 ? 'white' : 'teal' }}", + "borderRadius": 8, + "h": 200 + }, + "traits": [] + }, + { + "id": "box_child_1", + "type": "chakra_ui/v1/box", + "properties": { + "mr": 4, + "flex": 1, + "bg": "{{ test_btn.count % 2 ? 'white' : 'teal' }}", + "color": "{{ test_btn.count % 2 ? 'teal' : 'white' }}", + "display": "flex", + "alignItems": "center", + "justifyContent": "center", + "borderRadius": 4 + }, + "traits": [ + { + "type": "core/v1/slot", + "properties": { "container": { "id": "box_container", "slot": "content" } } + } + ] + }, + { + "id": "text", + "type": "core/v1/text", + "properties": { "value": { "raw": "in box child 1", "format": "plain" } }, + "traits": [ + { + "type": "core/v1/slot", + "properties": { "container": { "id": "box_child_1", "slot": "content" } } + } + ] + }, + { + "id": "box_child_2", + "type": "chakra_ui/v1/box", + "properties": { + "flex": 1, + "bg": "{{ test_btn.count % 2 ? 'white' : 'teal' }}", + "color": "{{ test_btn.count % 2 ? 'teal' : 'white' }}", + "display": "flex", + "alignItems": "center", + "justifyContent": "center", + "borderRadius": 4 + }, + "traits": [ + { + "type": "core/v1/slot", + "properties": { "container": { "id": "box_container", "slot": "content" } } + } + ] + }, + { + "id": "text", + "type": "core/v1/text", + "properties": { "value": { "raw": "in box child 2", "format": "plain" } }, + "traits": [ + { + "type": "core/v1/slot", + "properties": { "container": { "id": "box_child_2", "slot": "content" } } + } + ] + } + ] + } + } +} diff --git a/packages/runtime/index.html b/packages/runtime/index.html new file mode 100644 index 00000000..bd4ce2dc --- /dev/null +++ b/packages/runtime/index.html @@ -0,0 +1,20 @@ + + + + + + meta-ui runtime examples + + +
+ + + diff --git a/packages/runtime/vite.config.ts b/packages/runtime/vite.config.ts index e4cd6c75..277587b1 100644 --- a/packages/runtime/vite.config.ts +++ b/packages/runtime/vite.config.ts @@ -1,5 +1,43 @@ -import { defineConfig } from 'vite'; +import { defineConfig, Plugin } from 'vite'; import react from '@vitejs/plugin-react'; +import fs from 'fs'; +import path from 'path'; + +function virtualExamplePlugin(): Plugin { + const virtualFileId = '@example.json'; + + const exampleDir = path.join(__dirname, '../../examples'); + const examples = []; + + function walk(dirOrFile: string, frags: string[]) { + if (fs.statSync(dirOrFile).isDirectory()) { + for (const subDir of fs.readdirSync(dirOrFile)) { + frags.push(subDir); + walk(path.join(dirOrFile, subDir), frags); + } + } else { + const value = JSON.parse(fs.readFileSync(dirOrFile, 'utf-8')); + const name = frags.filter(frag => frag !== 'index.json').join('_'); + examples.push({ name, value }); + } + } + + walk(exampleDir, []); + + return { + name: 'virtual-example-plugin', + resolveId(id) { + if (id === virtualFileId) { + return virtualFileId; + } + }, + load(id) { + if (id === virtualFileId) { + return JSON.stringify(examples); + } + }, + }; +} // https://vitejs.dev/config/ export default defineConfig({ @@ -18,6 +56,7 @@ export default defineConfig({ ], }, }), + virtualExamplePlugin(), ], define: { // https://github.com/satya164/react-simple-code-editor/issues/86