Skip to content

Commit 74102e3

Browse files
authored
Compatibility with script setup (#100)
* add setup example * add test for checkTemplate * upgrade compiler and fix checkTemplate * fix ci from missing script * remove empty line on top of example
1 parent 17c5244 commit 74102e3

File tree

7 files changed

+98
-71
lines changed

7 files changed

+98
-71
lines changed

.github/workflows/pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
run: npm ci
2121

2222
- name: Check types
23-
run: npm run types:check
23+
run: npm run types
2424

2525
- name: Test unit
2626
run: npm run test:unit

demo/App.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
file components as well
2828
</p>
2929
<VueLive :code="codeSfc" :layout="CustomLayout" />
30+
<h2>SFC with setup</h2>
31+
<VueLive :code="codeSfcSetup" :layout="CustomLayout" />
3032
<h2>Pure JavaScript code</h2>
3133
<p>Or if you prefer to, use the <b>new Vue()</b> format</p>
3234
<VueLive :code="codeJs" :layout="CustomLayout" />
@@ -87,6 +89,7 @@ import DatePicker from "vue3-datepicker";
8789
import { VueLive, VueLiveEditor, VueLivePreview } from "../src";
8890
import CustomLayout from "./CustomLayout.vue";
8991
import codeSfc from "./assets/Button.vue?raw";
92+
import codeSfcSetup from "./assets/ButtonSetup.vue?raw";
9093
import codeJs from "./assets/input.js?raw";
9194
import realjsx from "./assets/real.jsx?raw";
9295
import codeTemplate from "./assets/PureTemplate.html?raw";
@@ -106,6 +109,7 @@ export default defineComponent({
106109
return {
107110
registeredComponents: { DatePicker: markRaw(DatePicker) },
108111
codeSfc,
112+
codeSfcSetup,
109113
codeTemplate,
110114
codeJs,
111115
codeChicago,

demo/assets/ButtonSetup.vue

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script lang="ts" setup>
2+
import { ref } from 'vue'
3+
4+
const msg = ref("Push Me")
5+
</script>
6+
7+
<template>
8+
<div class="hello">
9+
<h1>Colored Text</h1>
10+
<button>{{ msg }}</button>
11+
</div>
12+
</template>
13+
14+
<style>
15+
.hello {
16+
text-align: center;
17+
color: #900;
18+
}
19+
</style>

package-lock.json

Lines changed: 52 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"debounce": "^1.2.1",
2424
"hash-sum": "^2.0.0",
2525
"prismjs": "^1.29.0",
26-
"vue-inbrowser-compiler-sucrase": "^4.54.1",
26+
"vue-inbrowser-compiler-sucrase": "^4.56.6",
2727
"vue-prism-editor": "^2.0.0-alpha.2"
2828
},
2929
"devDependencies": {

src/utils/checkTemplate.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ test("parse valid template without error with a value in methods", () => {
6060
).not.toThrow();
6161
});
6262

63+
test("parse valid template without error with values returned from setup", () => {
64+
expect(() =>
65+
checkTemplate({
66+
template: '<div><compo :value="today">{{ msg }}</compo></div>',
67+
setup() {
68+
return {
69+
today() {
70+
return "bonjour";
71+
},
72+
msg: "hello"
73+
}
74+
},
75+
})
76+
).not.toThrow();
77+
});
78+
6379
test("parse false value as a valid value", () => {
6480
expect(() =>
6581
checkTemplate({

src/utils/checkTemplate.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface Options {
1515
computed?: Record<string, any>;
1616
methods?: Record<string, any>;
1717
attrAllowList?: string[];
18+
setup?: () => (Record<string, any> | void)
1819
}
1920

2021
export default function (
@@ -48,12 +49,16 @@ export default function (
4849

4950
const methodsArray =
5051
$options && $options.methods ? Object.keys($options.methods) : [];
52+
53+
const setupOutput =
54+
$options && typeof $options.setup === 'function' ? Object.keys($options.setup() ?? {}) : [];
5155

5256
const scriptVars = [
5357
...propNamesArray,
5458
...dataArray,
5559
...computedArray,
5660
...methodsArray,
61+
...setupOutput
5762
];
5863

5964
// Define list of attributes for which name check will be skipped. Defaults to known camelCased SVG attributes.

0 commit comments

Comments
 (0)