Skip to content

Commit

Permalink
fix(compiler): add withAsyncContext helper import from vue when trans…
Browse files Browse the repository at this point in the history
…form
  • Loading branch information
ShenQingchuan committed Nov 27, 2024
1 parent dc09ca6 commit 7814986
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/compiler/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const USE_SLOT_HELPER = 'useSlots'
export const UN_REF_HELPER = 'unref'
export const DEFAULT_MODEL_NAME = 'modelValue'
export const DEFAULT_MODEL_MODIFIERS_NAME = 'modelModifiers'
export const WITH_ASYNC_CONTEXT_HELPER = 'withAsyncContext'

/**
* These macros can't be inside other expressions but just called directly.
*/
Expand Down
23 changes: 18 additions & 5 deletions packages/compiler/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
USE_DEFAULTS_HELPER,
USE_MODEL_HELPER,
USE_SLOT_HELPER,
WITH_ASYNC_CONTEXT_HELPER,
} from './constants'
import { sortStyleImport } from './style/order'
import { compileCSSVars } from './style/transform-css-var'
Expand All @@ -44,11 +45,11 @@ function wrapWithAsyncContext(
);`
}

function mayContainAwaitExpr(targetNode: Node) {
function mayContainAwaitExpr(vineFnBodyStmt: Node) {
let awaitExpr: AwaitExpression | undefined
const isVarDecl = isVariableDeclaration(targetNode)
const isAssignExpr = isAssignmentExpression(targetNode)
const isExprStmt = isExpressionStatement(targetNode)
const isVarDecl = isVariableDeclaration(vineFnBodyStmt)
const isAssignExpr = isAssignmentExpression(vineFnBodyStmt)
const isExprStmt = isExpressionStatement(vineFnBodyStmt)

if (!(
isVarDecl
Expand All @@ -58,10 +59,17 @@ function mayContainAwaitExpr(targetNode: Node) {
return null
}

if (
isExprStmt
&& vineFnBodyStmt.expression.type !== 'AwaitExpression'
) {
return null
}

const isNeedResult = isVarDecl || isAssignExpr

try {
traverse(targetNode, (descendant) => {
traverse(vineFnBodyStmt, (descendant) => {
if (isAwaitExpression(descendant)) {
awaitExpr = descendant
throw new Error(EXPECTED_ERROR)
Expand Down Expand Up @@ -258,6 +266,11 @@ export function transformFile(
if (!mayContain || !mayContain.awaitExpr) {
continue
}

// has await expression in function body root level statements,
// we need to add 'withAsyncContext' helper, imported from 'vue'
vueImportsSpecs.set(WITH_ASYNC_CONTEXT_HELPER, `_${WITH_ASYNC_CONTEXT_HELPER}`)

const { awaitExpr, isNeedResult } = mayContain
hasAwait = true
ms.update(
Expand Down
32 changes: 32 additions & 0 deletions packages/compiler/tests/__snapshots__/transform.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useModel as _useModel,
toRefs as _toRefs,
createVNode as _createVNode,
withAsyncContext as _withAsyncContext,
} from "vue";
import "testTransformInlineResult?type=vine-style&scopeId=3921d7bd&comp=MyProfile&lang=css&index=0&virtual.css";
Expand Down Expand Up @@ -83,6 +84,13 @@ export const MyProfile = (() => {
// ...
};
onMounted(async () => {
const data = await fetch(
"https://api.sampleapis.com/futurama/characters",
);
console.log(data);
});
__expose({
age,
bio,
Expand Down Expand Up @@ -216,6 +224,7 @@ import {
useModel as _useModel,
toRefs as _toRefs,
createVNode as _createVNode,
withAsyncContext as _withAsyncContext,
} from "vue";
import "testNoHMRContentOnProduction?type=vine-style&scopeId=3b48c990&comp=MyProfile&lang=css&index=0&virtual.css";
Expand Down Expand Up @@ -282,6 +291,13 @@ export const MyProfile = (() => {
// ...
};
onMounted(async () => {
const data = await fetch(
"https://api.sampleapis.com/futurama/characters",
);
console.log(data);
});
__expose({
age,
bio,
Expand Down Expand Up @@ -389,6 +405,7 @@ import {
useModel as _useModel,
toRefs as _toRefs,
createVNode as _createVNode,
withAsyncContext as _withAsyncContext,
} from "vue";
import "testTransformSeparatedResult?type=vine-style&scopeId=25e4e706&comp=MyProfile&lang=css&index=0&virtual.css";
Expand Down Expand Up @@ -455,6 +472,13 @@ export const MyProfile = (() => {
// ...
};
onMounted(async () => {
const data = await fetch(
"https://api.sampleapis.com/futurama/characters",
);
console.log(data);
});
__expose({
age,
bio,
Expand Down Expand Up @@ -602,6 +626,7 @@ import {
useSlots as _useSlots,
useModel as _useModel,
toRefs as _toRefs,
withAsyncContext as _withAsyncContext,
} from "vue";
import "testTransformSeparatedResult?type=vine-style&scopeId=25e4e706&comp=MyProfile&lang=css&index=0&virtual.css";
Expand Down Expand Up @@ -667,6 +692,13 @@ export const MyProfile = (() => {
// ...
};
onMounted(async () => {
const data = await fetch(
"https://api.sampleapis.com/futurama/characters",
);
console.log(data);
});
__expose({
age,
bio,
Expand Down
5 changes: 5 additions & 0 deletions packages/compiler/tests/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ function MyProfile() {
vineStyle.import('../styles/test1.less').scoped()
vineStyle.import(\`../styles/test2.scss\`)
onMounted(async () => {
const data = await fetch("https://api.sampleapis.com/futurama/characters")
console.log(data)
})
return vine\`
<div class="my-profile">
<div>{{ name }}<span> - {{ age }}</span></div>
Expand Down

0 comments on commit 7814986

Please sign in to comment.