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

Commit d58623c

Browse files
committed
fix: get declarations from both <script setup> and <script>, close #104
1 parent 06ad728 commit d58623c

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

Diff for: src/core/transformScriptSetup.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ export function transformScriptSetup(
3939
|| n.type.startsWith('TS'),
4040
)
4141

42-
// get all identifiers in `<script setup>`
43-
const declarations = [
42+
// get all identifiers in `<script setup>` and `<script>`
43+
const declarationArray = uniq([
4444
...getIdentifierDeclarations(hoisted),
4545
...getIdentifierDeclarations(setupBody),
46-
]
47-
const declarationArray = uniq(declarations).filter(notNullish)
46+
...getIdentifierDeclarations(script.ast.body),
47+
]).filter(notNullish)
4848

4949
// filter out identifiers that are used in `<template>`
5050
const returns: ObjectExpression['properties'] = declarationArray

Diff for: test/__snapshots__/transform.test.ts.snap

+43
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,49 @@ export default __sfc_main;
262262
"
263263
`;
264264
265+
exports[`transform > fixtures > test/fixtures/ComponentsDirectivesLocal.vue 1`] = `
266+
"
267+
<script lang=\\"ts\\">
268+
import type { ObjectDirective } from '@vue/composition-api'; // enables v-focus in templates
269+
270+
const vFocus: ObjectDirective<HTMLInputElement, null> = {
271+
inserted: (el, binding, vnode, oldVnode) => {
272+
el.focus();
273+
}
274+
};
275+
const __sfc_main = {};
276+
__sfc_main.directives = Object.assign({
277+
focus: vFocus
278+
}, __sfc_main.directives);
279+
export default __sfc_main;
280+
</script>
281+
<template>
282+
<input v-focus=\\"null\\">
283+
</template>
284+
"
285+
`;
286+
287+
exports[`transform > fixtures > test/fixtures/ComponentsLocal.vue 1`] = `
288+
"
289+
<script lang=\\"tsx\\">
290+
const HelloComponent = ({
291+
name
292+
}: {
293+
name: string;
294+
}) => <span>Hello, {name}</span>;
295+
296+
const __sfc_main = {};
297+
__sfc_main.components = Object.assign({
298+
HelloComponent
299+
}, __sfc_main.components);
300+
export default __sfc_main;
301+
</script>
302+
<template>
303+
<HelloComponent name=\\"Evan You\\" />
304+
</template>
305+
"
306+
`;
307+
265308
exports[`transform > fixtures > test/fixtures/DynamicStyle.vue 1`] = `
266309
"<template>
267310
<div :style=\\"{ color, border: '1px' }\\" />

Diff for: test/fixtures/ComponentsDirectivesLocal.vue

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script lang="ts">
2+
import type { ObjectDirective } from '@vue/composition-api'
3+
// enables v-focus in templates
4+
const vFocus: ObjectDirective<HTMLInputElement, null> = {
5+
inserted: (el, binding, vnode, oldVnode) => {
6+
el.focus()
7+
},
8+
}
9+
</script>
10+
<script setup lang="ts"></script>
11+
<template>
12+
<input v-focus="null">
13+
</template>

Diff for: test/fixtures/ComponentsLocal.vue

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script lang="tsx">
2+
const HelloComponent = ({ name }: { name: string }) => <span>Hello, {name}</span>
3+
</script>
4+
<script setup lang="tsx"></script>
5+
<template>
6+
<HelloComponent name="Evan You" />
7+
</template>

Diff for: tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"esModuleInterop": true,
88
"allowSyntheticDefaultImports": true,
99
"strict": true,
10+
"jsx": "preserve",
1011
"strictNullChecks": true,
1112
"isolatedModules": true,
1213
"resolveJsonModule": true

0 commit comments

Comments
 (0)