Skip to content

Commit 36cfe8a

Browse files
elevatebartsusnux
andauthored
feat: support import statements and directives (#101)
* feat: Support directives Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de> * fix: Support `import` statements Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de> * fix typings --------- Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de> Co-authored-by: Ferdinand Thiessen <rpm@fthiessen.de>
1 parent 74102e3 commit 36cfe8a

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

src/Preview.vue

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ export default defineComponent({
4444
type: Object,
4545
default: () => { },
4646
},
47+
/**
48+
* Hashtable of auto-registered directives
49+
* @example { Tooltip: VueTooltip }
50+
* @example { VueTooltip }
51+
*/
52+
directives: {
53+
type: Object,
54+
default: () => { },
55+
},
4756
/**
4857
* Hashtable of modules available in require and import statements
4958
* in the code prop
@@ -84,7 +93,7 @@ export default defineComponent({
8493
};
8594
},
8695
computed: {
87-
requiresPlusVue() {
96+
requiresPlusVue(): Record<string, any> {
8897
return { vue: Vue, ...this.requires };
8998
},
9099
},
@@ -131,7 +140,7 @@ export default defineComponent({
131140
this.removeScopedStyle();
132141
}
133142
},
134-
renderComponent(code: string) {
143+
async renderComponent(code: string) {
135144
let options: Record<string, any> = {};
136145
let style;
137146
try {
@@ -155,11 +164,15 @@ export default defineComponent({
155164
// it can be:
156165
// - a script setting up variables => we set up the data property of renderedComponent
157166
// - a `new Vue()` script that will return a full config object
158-
const calcOptions = () => {
167+
const calcOptions = async () => {
159168
const script = renderedComponent.script;
169+
const requires: typeof this.requires = {}
170+
await Promise.allSettled(Object.keys(this.requiresPlusVue).map(async (key) => {
171+
requires[key] = this.requiresPlusVue[key] instanceof Promise ? (await this.requiresPlusVue[key]).default : this.requiresPlusVue[key]
172+
}))
160173
options = (evalInContext(
161174
script,
162-
(filepath) => requireAtRuntime(this.requiresPlusVue, filepath),
175+
(filepath) => requireAtRuntime(requires, filepath),
163176
adaptCreateElement,
164177
concatenate,
165178
h
@@ -178,7 +191,7 @@ export default defineComponent({
178191
}
179192
options.name = "VueLiveCompiledExample";
180193
};
181-
calcOptions();
194+
await calcOptions();
182195
183196
// In case the template is inlined in the script,
184197
// we need to compile it
@@ -214,6 +227,14 @@ export default defineComponent({
214227
}
215228
}
216229
230+
if (this.directives) {
231+
if (!options.directives) {
232+
options.directives = this.directives;
233+
} else {
234+
options.directives = { ...options.directives, ...this.directives };
235+
}
236+
}
237+
217238
this.removeStyle();
218239
219240
if (style) {

src/VueLive.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @property { Error } - the error thrown
1414
-->
1515
<Preview :key="codeKey" :code="model" @detect-language="switchLanguage" @error="handleError"
16-
@success="handleSuccess" :components="components" :requires="requires" :jsx="jsx" :data-scope="dataScope"
16+
@success="handleSuccess" :components="components" :directives="directives" :requires="requires" :jsx="jsx" :data-scope="dataScope"
1717
:check-variable-availability="checkVariableAvailability" />
1818
</template>
1919
</component>
@@ -60,11 +60,20 @@ export default defineComponent({
6060
type: Object,
6161
default: () => { },
6262
},
63+
/**
64+
* Hashtable of auto-registered directives
65+
* @example { Tooltip: VueTooltip }
66+
* @example { VueTooltip }
67+
*/
68+
directives: {
69+
type: Object,
70+
default: () => { },
71+
},
6372
/**
6473
* Hashtable of modules available in require and import statements
6574
* in the Preview component
6675
* @example { lodash: require("lodash") }
67-
* @example { moment: require("moment") }
76+
* @example { "foo/bar.js": import("foo/bar.js") }
6877
*/
6978
requires: {
7079
type: Object,

0 commit comments

Comments
 (0)