Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 10a2735

Browse files
Demivanantfu
andauthored
fix: async component registration hoisting (#44)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent e3c38f9 commit 10a2735

File tree

7 files changed

+69
-15
lines changed

7 files changed

+69
-15
lines changed

jest.js

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@
4949
"types": "index.d.ts",
5050
"files": [
5151
"dist",
52-
"jest.js",
52+
"jest-transform.js",
5353
"*.d.ts"
5454
],
5555
"scripts": {
56-
"build": "rimraf dist && tsup 'src/*.ts' --format cjs,esm --dts && esno scripts/postbuild.ts",
57-
"dev": "tsup 'src/*.ts' --watch src",
56+
"build": "rimraf dist && tsup \"src/*.ts\" --format cjs,esm --dts && esno scripts/postbuild.ts",
57+
"dev": "tsup \"src/*.ts\" --watch src",
5858
"lint": "eslint \"{src,test}/**/*.ts\"",
5959
"lint:fix": "nr lint -- --fix",
6060
"play": "npm -C playground run dev",

playground/App.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
<template>
22
<div>
33
<hello-world name="Vue 2" @update="onUpdate" />
4+
5+
<async-component />
46
</div>
57
</template>
68

79
<script setup lang="ts">
10+
import { defineAsyncComponent } from '@vue/composition-api'
11+
812
import HelloWorld from './HelloWorld.vue'
913
14+
const AsyncComponent = defineAsyncComponent(() => import('./Async.vue'))
15+
1016
function onUpdate(e: any) {
1117
// eslint-disable-next-line no-console
1218
console.log(e)

playground/Async.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>Async Component</div>
3+
</template>

src/core/transformScriptSetup.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,27 @@ import { ParsedSFC, ScriptSetupTransformOptions } from '../types'
77
import { applyMacros } from './macros'
88
import { getIdentifierDeclarations } from './identifiers'
99

10+
function isAsyncImport(node: any) {
11+
if (node.type === 'VariableDeclaration') {
12+
const declaration = node.declarations[0]
13+
14+
return declaration.init.callee != null
15+
&& declaration.init.callee.name === 'defineAsyncComponent'
16+
}
17+
18+
return false
19+
}
20+
1021
export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransformOptions) {
1122
const { scriptSetup, script, template } = sfc
1223

1324
const { nodes: body, props, expose } = applyMacros(scriptSetup.ast.body)
1425

1526
const [hoisted, setupBody] = partition(
1627
body,
17-
n => n.type === 'ImportDeclaration'
28+
n =>
29+
isAsyncImport(n)
30+
|| n.type === 'ImportDeclaration'
1831
|| n.type === 'ExportNamedDeclaration'
1932
|| n.type.startsWith('TS'),
2033
)

test/__snapshots__/transform.test.ts.snap

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ exports[`transform fixtures playground/App.vue 1`] = `
44
"<template>
55
<div>
66
<hello-world name=\\"Vue 2\\" @update=\\"onUpdate\\" />
7+
8+
<async-component />
79
</div>
810
</template>
911
1012
<script lang=\\"ts\\">
13+
import { defineAsyncComponent } from '@vue/composition-api';
1114
import HelloWorld from './HelloWorld.vue';
15+
const AsyncComponent = defineAsyncComponent(() => import('./Async.vue'));
1216
const __sfc_main = {};
1317
1418
__sfc_main.setup = (__props, __ctx) => {
@@ -23,13 +27,21 @@ __sfc_main.setup = (__props, __ctx) => {
2327
};
2428
2529
__sfc_main.components = Object.assign({
26-
HelloWorld
30+
HelloWorld,
31+
AsyncComponent
2732
}, __sfc_main.components);
2833
export default __sfc_main;
2934
</script>
3035
"
3136
`;
3237
38+
exports[`transform fixtures playground/Async.vue 1`] = `
39+
"<template>
40+
<div>Async Component</div>
41+
</template>
42+
"
43+
`;
44+
3345
exports[`transform fixtures playground/Bar.vue 1`] = `
3446
"<template>
3547
<div>Bar</div>
@@ -146,6 +158,28 @@ export default defineConfig({
146158
"
147159
`;
148160
161+
exports[`transform fixtures test/fixtures/AsyncImport.vue 1`] = `
162+
"<script lang=\\"ts\\">
163+
import { defineAsyncComponent } from '@vue/composition-api';
164+
const ScriptOnly = defineAsyncComponent(() => import('./ScriptOnly.vue'));
165+
const __sfc_main = {};
166+
167+
__sfc_main.setup = (__props, __ctx) => {
168+
return {};
169+
};
170+
171+
__sfc_main.components = Object.assign({
172+
ScriptOnly
173+
}, __sfc_main.components);
174+
export default __sfc_main;
175+
</script>
176+
177+
<template>
178+
<ScriptOnly />
179+
</template>
180+
"
181+
`;
182+
149183
exports[`transform fixtures test/fixtures/DynamicStyle.vue 1`] = `
150184
"<template>
151185
<div :style=\\"{ color, border: '1px' }\\" />

test/fixtures/AsyncImport.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script setup lang="ts">
2+
import { defineAsyncComponent } from '@vue/composition-api'
3+
const ScriptOnly = defineAsyncComponent(() => import('./ScriptOnly.vue'))
4+
</script>
5+
6+
<template>
7+
<ScriptOnly />
8+
</template>

0 commit comments

Comments
 (0)