Skip to content

Commit 72f7111

Browse files
authored
setup-import-components (#102)
* add test for imported component * refactor: remove code * add rendering for normal jsx and tsx * use tsx when needed * add directives to the readme * update compiler * build with the right stuff and an example * fix types * add cypress cloud integration * fix e2e tests
1 parent e1c116a commit 72f7111

15 files changed

+145
-77
lines changed

Diff for: .github/workflows/pr.yml

+11-2
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,14 @@ jobs:
2828
- name: Build Demo
2929
run: npm run build:demo
3030

31-
- name: Test e2e
32-
run: npm run test:e2e
31+
- name: Run E2E Test
32+
uses: cypress-io/github-action@v5
33+
with:
34+
start: npm run preview
35+
record: true
36+
browser: chrome
37+
env:
38+
# Recommended: pass the GitHub token lets this action correctly
39+
# determine the unique run id necessary to re-run the checks
40+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}

Diff for: .github/workflows/release.yml

+10-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ jobs:
3333
run: npm run build:demo
3434

3535
- name: Run E2E Test
36-
run: npm run test:e2e
36+
uses: cypress-io/github-action@v5
37+
with:
38+
start: npm run preview
39+
record: true
40+
browser: chrome
41+
env:
42+
# Recommended: pass the GitHub token lets this action correctly
43+
# determine the unique run id necessary to re-run the checks
44+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45+
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
3746

3847
- name: Deploy to vue-live.surge.sh
3948
if: ${{ github.ref == 'refs/heads/master' }}

Diff for: README.md

+9
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ export default {
178178
</script>
179179
```
180180

181+
### `directives`
182+
183+
**Type** Object:
184+
185+
- key: registration name
186+
- value: VueJs directive object
187+
188+
You can use this prop in the same fashion as `components` above.
189+
181190
### `requires`
182191

183192
**Type** Object:

Diff for: cypress.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig } from "cypress";
22

33
export default defineConfig({
4+
projectId: 'wbesaj',
45
e2e: {
56
specPattern: "cypress/e2e/**/*.{cy,spec}.{js,jsx,ts,tsx}",
67
baseUrl: "http://localhost:4173",

Diff for: cypress/e2e/LiveRefresh.cy.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@ describe("Live Refresh", () => {
1010
});
1111

1212
it("changes the render after code change", () => {
13+
const textToReplace = "inline component";
14+
const textReplaced = "red component";
15+
1316
cy.get("@preview")
14-
.find(".v3dp__datepicker input")
15-
.should("not.have.value", "");
17+
.get("[data-cy=my-button]")
18+
.should("have.text", textToReplace);
1619

17-
const codeToDelete = ' v-model="today"/>';
1820
cy.get("@container")
19-
.find(".prism-editor-wrapper textarea")
20-
.type(`${"{backspace}".repeat(codeToDelete.length)}/>`);
21+
.find(".prism-editor-wrapper textarea").as("editor");
22+
23+
cy.get("@editor").invoke("val")
24+
.then((val) => {
25+
cy.get("@editor")
26+
.clear()
27+
.invoke('val', `${val}`.replace(textToReplace, textReplaced))
28+
.trigger('input');
2129

22-
cy.get("@preview").find(".v3dp__datepicker input").should("have.value", "");
30+
cy.get("@preview")
31+
.get("[data-cy=my-button]")
32+
.should("have.text", textReplaced);
33+
});
2334
});
2435
});

Diff for: demo/App.vue

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<div class="livebox">
1616
<div class="hint">You can edit this <span>-></span></div>
17-
<VueLive :editorProps="{ lineNumbers: true }" :code="codeTemplate" :layout="CustomLayout"
17+
<VueLive :editorProps="{ lineNumbers: true }" :code="codeSfcSetup" :layout="CustomLayout"
1818
:components="registeredComponents" @error="(e: any) => log('Error on first example', e)" />
1919
</div>
2020

@@ -27,8 +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" />
30+
<h2>VSG partial mode or pure template</h2>
31+
<VueLive :code="codeTemplate" :layout="CustomLayout" />
3232
<h2>Pure JavaScript code</h2>
3333
<p>Or if you prefer to, use the <b>new Vue()</b> format</p>
3434
<VueLive :code="codeJs" :layout="CustomLayout" />
@@ -65,6 +65,9 @@
6565
<h2>JSX</h2>
6666
<VueLive :code="realjsx" :layout="CustomLayout" :jsx="true" />
6767

68+
<h2>TSX</h2>
69+
<VueLive :code="blobtsx" :layout="CustomLayout" lang="tsx" :jsx="true" />
70+
6871
<h2>Double Root</h2>
6972
<VueLive :code="doubleRoot" :layout="CustomLayout" />
7073

@@ -92,6 +95,7 @@ import codeSfc from "./assets/Button.vue?raw";
9295
import codeSfcSetup from "./assets/ButtonSetup.vue?raw";
9396
import codeJs from "./assets/input.js?raw";
9497
import realjsx from "./assets/real.jsx?raw";
98+
import blobtsx from "./assets/blob.tsx?raw";
9599
import codeTemplate from "./assets/PureTemplate.html?raw";
96100
import doubleRoot from "./assets/PureTemplateDoubleRoot.html?raw";
97101
import codeChicago from "./assets/Chicago.jsx?raw";
@@ -116,6 +120,7 @@ export default defineComponent({
116120
CustomLayout: markRaw(CustomLayout),
117121
chicagoRequires: { "./chicagoNeighbourhoods": all },
118122
realjsx,
123+
blobtsx,
119124
separateCode: codeSfc,
120125
doubleRoot,
121126
openExamples: false,

Diff for: demo/assets/ButtonSetup.vue

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
<script lang="ts" setup>
2-
import { ref } from 'vue'
2+
import { ref, h } from 'vue'
3+
4+
const MyButton = () => {
5+
return h('button',
6+
{
7+
style: { color: 'red' },
8+
"data-cy": "my-button"
9+
},
10+
'inline component'
11+
)
12+
}
313
414
const msg = ref("Push Me")
515
</script>
616

717
<template>
818
<div class="hello">
919
<h1>Colored Text</h1>
10-
<button>{{ msg }}</button>
20+
<input v-model="msg">
21+
<div>
22+
{{ msg }}
23+
</div>
24+
<div>
25+
<MyButton/>
26+
</div>
1127
</div>
12-
</template>
13-
14-
<style>
15-
.hello {
16-
text-align: center;
17-
color: #900;
18-
}
19-
</style>
28+
</template>

Diff for: demo/assets/blob.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const args = {
2+
type: "button",
3+
value: "update me",
4+
} as const;
5+
6+
type Key = keyof typeof args;
7+
8+
export default {
9+
render() {
10+
return <input {...args} />;
11+
},
12+
};

Diff for: package-lock.json

+24-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"debounce": "^1.2.1",
2424
"hash-sum": "^2.0.0",
2525
"prismjs": "^1.29.0",
26-
"vue-inbrowser-compiler-sucrase": "^4.60.0",
26+
"vue-inbrowser-compiler-sucrase": "^4.62.0",
27+
"vue-inbrowser-compiler-utils": "^4.62.1",
2728
"vue-prism-editor": "^2.0.0-alpha.2"
2829
},
2930
"devDependencies": {

Diff for: src/Editor.vue

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import debounce from "debounce";
99
1010
import "vue-prism-editor/dist/prismeditor.min.css";
1111
12-
import makeHighlight from "./utils/highlight";
12+
import makeHighlight, { CONFIGURED_LANGS, type CONFIGURED_LANGS_TYPE } from "./utils/highlight";
1313
1414
const UPDATE_DELAY = 300;
1515
@@ -35,9 +35,9 @@ export default defineComponent({
3535
default: () => ({}),
3636
},
3737
prismLang: {
38-
type: String,
38+
type: String as PropType<CONFIGURED_LANGS_TYPE>,
3939
default: "html",
40-
validator: (val: string) => ["html", "vsg"].includes(val),
40+
validator: (val: string) => CONFIGURED_LANGS.includes(val as CONFIGURED_LANGS_TYPE),
4141
},
4242
jsx: {
4343
type: Boolean,
@@ -58,7 +58,7 @@ export default defineComponent({
5858
*/
5959
stableCode: this.code,
6060
highlight: (() => (code: string) => code) as (
61-
lang: "vue" | "vsg",
61+
lang: CONFIGURED_LANGS_TYPE,
6262
jsxInExamples: boolean
6363
) => (code: string, errorLoc: any) => string,
6464
};
@@ -73,7 +73,7 @@ export default defineComponent({
7373
},
7474
methods: {
7575
highlighter(code: string) {
76-
return this.highlight(this.prismLang as "vue" | "vsg", this.jsx)(
76+
return this.highlight(this.prismLang, this.jsx)(
7777
code,
7878
this.squiggles && this.error && this.error.loc
7979
);

Diff for: src/Preview.vue

+4-18
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,7 @@ export default defineComponent({
143143
}
144144
},
145145
async renderComponent(code: string) {
146-
let options = defineComponent({
147-
render() {
148-
return h("div");
149-
},
150-
});
146+
let options = defineComponent({});
151147
let style;
152148
try {
153149
const renderedComponent = compileScript(
@@ -183,18 +179,6 @@ export default defineComponent({
183179
concatenate,
184180
h
185181
) || {}));
186-
if (options.render) {
187-
const preview = this;
188-
const originalRender = options.render;
189-
options.render = function (...args: any[]) {
190-
try {
191-
return originalRender.call(this, ...args);
192-
} catch (e) {
193-
preview.handleError(e);
194-
return;
195-
}
196-
};
197-
}
198182
options.name = "VueLiveCompiledExample";
199183
};
200184
await calcOptions();
@@ -218,7 +202,7 @@ export default defineComponent({
218202
options.data = () => mergeData;
219203
}
220204
}
221-
205+
222206
const template = renderedComponent.raw.template
223207
if (template) {
224208
checkTemplate({
@@ -266,6 +250,8 @@ export default defineComponent({
266250
return;
267251
}
268252
253+
console.log({render:options.render})
254+
269255
this.previewedComponent = markRaw(options);
270256
this.iteration = this.iteration + 1;
271257
this.error = false;

0 commit comments

Comments
 (0)