66</template >
77
88<script >
9- import compileCode , { isCodeVueSfc } from " ./utils/compileCode" ;
10- import getVars from " ./utils/getVars" ;
11- import getVueConfigObject from " ./utils/getVueConfigObject" ;
9+ import { compile , isCodeVueSfc } from " vue-inbrowser-compiler" ;
1210import addScopedStyle from " ./utils/addScopedStyle" ;
11+ import evalInContext from " ./utils/evalInContext" ;
12+ import requireAtRuntime from " ./utils/requireAtRuntime" ;
1313
1414export default {
1515 name: " VueLivePreviewComponent" ,
@@ -69,21 +69,10 @@ export default {
6969 },
7070 renderComponent (code ) {
7171 let data = {};
72- let listVars = [];
73- let script;
7472 let style;
75- let template;
7673 try {
77- const renderedComponent = compileCode (code);
74+ const renderedComponent = compile (code);
7875 style = renderedComponent .style ;
79- if (renderedComponent .html && renderedComponent .script .length ) {
80- // When it's a template preceeded by a script (vsg format)
81- // NOTA: if it is an SFC, the html template will be added in the script
82-
83- // extract all variable to set them up as data in the component
84- // this way we can use in the template what is defined in the script
85- listVars = getVars (renderedComponent .script );
86- }
8776 if (renderedComponent .script ) {
8877 // if the compiled code contains a script it might be "just" a script
8978 // if so, change scheme used by editor
@@ -93,14 +82,16 @@ export default {
9382 // it can be:
9483 // - a script setting up variables => we set up the data property of renderedComponent
9584 // - a `new Vue()` script that will return a full config object
96- script = renderedComponent .script ;
97- data = getVueConfigObject (script, listVars, this .requires ) || {};
85+ const script = renderedComponent .script ;
86+ data =
87+ evalInContext (script, filepath =>
88+ requireAtRuntime (this .requires , filepath)
89+ ) || {};
9890 }
99- if (renderedComponent .html ) {
91+ if (renderedComponent .template ) {
10092 // if this is a pure template or if we are in hybrid vsg mode,
10193 // we need to set the template up.
102- template = ` <div>${ renderedComponent .html } </div>` ;
103- data .template = template;
94+ data .template = ` <div>${ renderedComponent .template } </div>` ;
10495 }
10596 } catch (e) {
10697 this .handleError (e);
@@ -116,6 +107,11 @@ export default {
116107
117108 if (data .template || data .render ) {
118109 this .previewedComponent = data;
110+ } else {
111+ this .handleError ({
112+ message:
113+ " [Vue Live] no template or render function specified, you might have an issue in your example"
114+ });
119115 }
120116 }
121117 }
0 commit comments