Skip to content

Commit 68722f8

Browse files
authored
Merge pull request #17 from vue-styleguidist/refactor-useinbrowsercompiler
feat: use vue-inbrowser-compiler
2 parents c4596c0 + 2587d28 commit 68722f8

21 files changed

+708
-1159
lines changed

demo/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default {
4343
codeJs,
4444
codeChicago,
4545
CustomLayout,
46-
chicagoRequires: { chicagoNeighbourhoods: all }
46+
chicagoRequires: { "./chicagoNeighbourhoods": all }
4747
};
4848
}
4949
};

demo/assets/Chicago.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import all from "chicagoNeighbourhoods";
1+
import all from "./chicagoNeighbourhoods";
22
let i = 4;
33
const sel = [];
44
const getIndex = () => {

package.json

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,18 @@
1212
"test:unit": "vue-cli-service test:unit"
1313
},
1414
"dependencies": {
15-
"acorn": "^6.1.1",
16-
"acorn-jsx": "^5.0.1",
17-
"buble": "^0.19.7",
1815
"lodash.debounce": "^4.0.8",
1916
"prismjs": "^1.16.0",
20-
"rewrite-imports": "^2.0.3",
2117
"vue": "^2.6.10",
22-
"vue-prism-editor": "^0.2.0",
23-
"vue-template-compiler": "^2.5.21",
24-
"walkes": "^0.2.1"
18+
"vue-inbrowser-compiler": "^3.13.8",
19+
"vue-prism-editor": "^0.2.0"
2520
},
2621
"devDependencies": {
27-
"@vue/cli-plugin-babel": "^3.6.0",
28-
"@vue/cli-plugin-e2e-cypress": "^3.7.0",
29-
"@vue/cli-plugin-eslint": "^3.6.0",
30-
"@vue/cli-plugin-unit-jest": "^3.6.3",
31-
"@vue/cli-service": "^3.6.0",
22+
"@vue/cli-plugin-babel": "^3.8.0",
23+
"@vue/cli-plugin-e2e-cypress": "^3.8.0",
24+
"@vue/cli-plugin-eslint": "^3.8.0",
25+
"@vue/cli-plugin-unit-jest": "^3.8.0",
26+
"@vue/cli-service": "^3.8.0",
3227
"@vue/test-utils": "1.0.0-beta.29",
3328
"babel-core": "7.0.0-bridge.0",
3429
"babel-eslint": "^10.0.1",
@@ -37,12 +32,12 @@
3732
"eslint": "^5.16.0",
3833
"eslint-plugin-vue": "^5.0.0",
3934
"raw-loader": "^2.0.0",
40-
"rollup": "^1.11.2",
35+
"rollup": "^1.12.4",
4136
"rollup-plugin-babel": "^4.3.2",
4237
"rollup-plugin-commonjs": "^9.3.4",
4338
"rollup-plugin-css-only": "^1.0.0",
4439
"rollup-plugin-json": "^4.0.0",
45-
"rollup-plugin-node-resolve": "^4.2.3",
40+
"rollup-plugin-node-resolve": "^5.0.0",
4641
"rollup-plugin-vue": "^5.0.0",
4742
"validate-commit-msg": "^2.14.0",
4843
"vuejs-datepicker": "^1.5.4"

src/Preview.vue

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
</template>
77

88
<script>
9-
import compileCode, { isCodeVueSfc } from "./utils/compileCode";
10-
import getVars from "./utils/getVars";
11-
import getVueConfigObject from "./utils/getVueConfigObject";
12-
import addScopedStyle from "./utils/addScopedStyle";
9+
import { compile, isCodeVueSfc, addScopedStyle } from "vue-inbrowser-compiler";
10+
import evalInContext from "./utils/evalInContext";
11+
import requireAtRuntime from "./utils/requireAtRuntime";
1312
1413
export default {
1514
name: "VueLivePreviewComponent",
@@ -69,21 +68,10 @@ export default {
6968
},
7069
renderComponent(code) {
7170
let data = {};
72-
let listVars = [];
73-
let script;
7471
let style;
75-
let template;
7672
try {
77-
const renderedComponent = compileCode(code);
73+
const renderedComponent = compile(code);
7874
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-
}
8775
if (renderedComponent.script) {
8876
// if the compiled code contains a script it might be "just" a script
8977
// if so, change scheme used by editor
@@ -93,17 +81,20 @@ export default {
9381
// it can be:
9482
// - a script setting up variables => we set up the data property of renderedComponent
9583
// - a `new Vue()` script that will return a full config object
96-
script = renderedComponent.script;
97-
data = getVueConfigObject(script, listVars, this.requires) || {};
84+
const script = renderedComponent.script;
85+
data =
86+
evalInContext(script, filepath =>
87+
requireAtRuntime(this.requires, filepath)
88+
) || {};
9889
}
99-
if (renderedComponent.html) {
90+
if (renderedComponent.template) {
10091
// if this is a pure template or if we are in hybrid vsg mode,
10192
// we need to set the template up.
102-
template = `<div>${renderedComponent.html}</div>`;
103-
data.template = template;
93+
data.template = `<div>${renderedComponent.template}</div>`;
10494
}
10595
} catch (e) {
10696
this.handleError(e);
97+
return;
10798
}
10899
109100
data.components = this.components;
@@ -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
}

src/VueLive.vue

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
2-
<component :is="this.layout ? this.layout : VueLiveDefaultLayout">
2+
<component :is="layout ? layout : VueLiveDefaultLayout">
33
<template v-slot:editor>
4-
<PrismEditor :code="code" @change="updatePreview" :language="prismLang"/>
4+
<PrismEditor v-model="stableCode" @change="updatePreview" :language="prismLang"/>
55
</template>
66
<template v-slot:preview>
77
<Preview
@@ -31,6 +31,8 @@ const LANG_TO_PRISM = {
3131
js: "jsx"
3232
};
3333
34+
const UPDATE_DELAY = 300;
35+
3436
export default {
3537
name: "VueLivePreview",
3638
components: { PrismEditor, Preview },
@@ -73,7 +75,13 @@ export default {
7375
return {
7476
model: this.code,
7577
prismLang: "html",
76-
VueLiveDefaultLayout
78+
VueLiveDefaultLayout,
79+
/**
80+
* this data only gets changed when changing language.
81+
* it allows for copy and pasting without having the code
82+
* editor repainted every keystroke
83+
*/
84+
stableCode: this.code
7785
};
7886
},
7987
computed: {
@@ -83,11 +91,15 @@ export default {
8391
},
8492
methods: {
8593
switchLanguage(newLang) {
86-
this.prismLang = LANG_TO_PRISM[newLang];
94+
const newPrismLang = LANG_TO_PRISM[newLang];
95+
if (this.prismLang !== newPrismLang) {
96+
this.prismLang = newPrismLang;
97+
this.stableCode = this.model;
98+
}
8799
},
88100
updatePreview: debounce(function(value) {
89101
this.model = value;
90-
}, 300)
102+
}, UPDATE_DELAY)
91103
}
92104
};
93105
</script>

src/utils/__tests__/addScopedStyle.unit.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/utils/__tests__/compileCode.unit.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/utils/__tests__/getVars.unit.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/utils/__tests__/getVueConfigObject.unit.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/utils/__tests__/normalizeSfcComponent.unit.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)