Skip to content

Commit 13695db

Browse files
committed
update <script setup> async context preservation compile sample
1 parent c22dda1 commit 13695db

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

active-rfcs/0040-script-setup.md

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default {
6363
<script setup>
6464
// expects props options
6565
const props = defineProps({
66-
foo: String,
66+
foo: String
6767
})
6868
// expects emits options
6969
const emit = defineEmits(['update', 'delete'])
@@ -150,10 +150,7 @@ import MyComponent from './MyComponent.vue'
150150
export default {
151151
setup() {
152152
return function render() {
153-
return [
154-
h(Foo),
155-
h(MyComponent)
156-
]
153+
return [h(Foo), h(MyComponent)]
157154
}
158155
}
159156
}
@@ -190,10 +187,7 @@ import Bar from './Bar.vue'
190187
export default {
191188
setup() {
192189
return function render() {
193-
return [
194-
h(Foo),
195-
h(someCondition ? Foo : Bar)
196-
]
190+
return [h(Foo), h(someCondition ? Foo : Bar)]
197191
}
198192
}
199193
}
@@ -224,9 +218,7 @@ import { directive as vClickOutside } from 'v-click-outside'
224218
export default {
225219
setup() {
226220
return function render() {
227-
return withDirectives(h('div'), [
228-
[vClickOutside]
229-
])
221+
return withDirectives(h('div'), [[vClickOutside]])
230222
}
231223
}
232224
}
@@ -243,7 +235,7 @@ To declare options like `props` and `emits` with full type inference support, we
243235
```html
244236
<script setup>
245237
const props = defineProps({
246-
foo: String,
238+
foo: String
247239
})
248240
249241
const emit = defineEmits(['change', 'delete'])
@@ -257,7 +249,7 @@ To declare options like `props` and `emits` with full type inference support, we
257249
```js
258250
export default {
259251
props: {
260-
foo: String,
252+
foo: String
261253
},
262254
emits: ['change', 'delete'],
263255
setup(props, { emit }) {
@@ -348,16 +340,22 @@ Top level `await` can be used inside `<script setup>`. The resulting `setup()` f
348340
</script>
349341
```
350342

351-
In addition, the awaited expression will be automatically wrapped with a `withAsyncContext` helper. The helper saves and restores the current instance context so that the current instance context is preserved even after the `await` statement:
343+
In addition, the awaited expression will be automatically compiled in a format that preserves the current component instance context after the `await`:
352344

353345
```js
354346
import { withAsyncContext } from 'vue'
355347

356348
export default {
357349
async setup() {
358-
const post = await withAsyncContext(
359-
fetch(`/api/post/1`).then((r) => r.json())
360-
)
350+
let __temp, __restore
351+
352+
const post =
353+
(([__temp, __restore] = withAsyncContext(() =>
354+
fetch(`/api/post/1`).then((r) => r.json())
355+
)),
356+
(__temp = await __temp),
357+
__restore(),
358+
__temp)
361359

362360
// current instance context preserved
363361
// e.g. onMounted() will still work.
@@ -379,13 +377,13 @@ To explicitly expose properties in a `<script setup>` component, use the `define
379377

380378
```html
381379
<script setup>
382-
const a = 1
383-
const b = ref(2)
380+
const a = 1
381+
const b = ref(2)
384382
385-
defineExpose({
386-
a,
387-
b
388-
})
383+
defineExpose({
384+
a,
385+
b
386+
})
389387
</script>
390388
```
391389

@@ -430,9 +428,9 @@ export default {
430428
setup() {
431429
const count = ref(0)
432430
return {
433-
count,
431+
count
434432
}
435-
},
433+
}
436434
}
437435
```
438436

@@ -469,7 +467,7 @@ If you need to declare these options, use a separate normal `<script>` block wit
469467
export default {
470468
name: 'CustomName',
471469
inheritAttrs: false,
472-
customOptions: {},
470+
customOptions: {}
473471
}
474472
</script>
475473

@@ -490,7 +488,7 @@ This new scoping model will require tooling adjustments in two aspects:
490488

491489
1. IDEs will need to provide dedicated handling for this new `<script setup>` model in order to provide template expression type checking / props validation, etc.
492490

493-
As of now, [Volar](https://github.com/johnsoncodehk/volar) already provides full support for this RFC in VSCode, including all TypeScript related features. Its internals are also implemented as a landuage server that can theoretically be used in other IDEs.
491+
As of now, [Volar](https://github.com/johnsoncodehk/volar) already provides full support for this RFC in VSCode, including all TypeScript related features. Its internals are also implemented as a landuage server that can theoretically be used in other IDEs.
494492

495493
2. ESLint rules like `no-unused-vars`. We will need a replacement rule in `eslint-plugin-vue` that takes both the `<script setup>` and `<template>` expressions into account.
496494

@@ -563,11 +561,11 @@ The `SFCScriptBlock` returned by `compiledScript` also exposes a `bindings` obje
563561

564562
```html
565563
<script setup="props">
566-
export const foo = 1
564+
export const foo = 1
567565
568-
export default {
569-
props: ['bar'],
570-
}
566+
export default {
567+
props: ['bar']
568+
}
571569
</script>
572570
```
573571

@@ -586,7 +584,7 @@ This object can then be passed to the template compiler:
586584
import { compile } from '@vue/compiler-dom'
587585

588586
compile(template, {
589-
bindingMetadata: bindings,
587+
bindingMetadata: bindings
590588
})
591589
```
592590

0 commit comments

Comments
 (0)