Skip to content

Commit 7c2cfee

Browse files
fix: script blocks match lang type
1 parent 39a43b3 commit 7c2cfee

File tree

3 files changed

+15
-266
lines changed

3 files changed

+15
-266
lines changed

src/index-postprocess.ts

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

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Options } from './types'
44
import { resolve } from 'pathe'
55
import { existsSync } from 'fs'
66
import { parse } from '@vue/compiler-dom'
7-
import { RootNode, ElementNode } from '@vue/compiler-dom'
7+
import type { RootNode, ElementNode, AttributeNode } from '@vue/compiler-dom'
88

99
function getRootBlock(
1010
root: RootNode,
@@ -23,6 +23,17 @@ function isSetupScript(node: ElementNode) {
2323
return node.props.some((prop) => prop.type === 6 && prop.name === 'setup')
2424
}
2525

26+
function langAttr(node?: ElementNode): string {
27+
if (!node) return ''
28+
const langProp = node.props.find(
29+
(prop) => prop.type === 6 && prop.name === 'lang',
30+
) as AttributeNode | undefined
31+
if (langProp && langProp.value?.content) {
32+
return ` lang="${langProp.value.content}"`
33+
}
34+
return ''
35+
}
36+
2637
/**
2738
* Imports `FormKitLazyProvider` component into the script block of the SFC.
2839
* @param code - The SFC source code.
@@ -37,12 +48,13 @@ function injectProviderImport(code: string): string {
3748
console.error(err)
3849
return code
3950
}
51+
const script = getRootBlock(root, 'script')
4052
const importStatement = `import { FormKitLazyProvider } from '@formkit/vue'`
4153
const setupScript = root.children.find(
4254
(node) => node.type === 1 && node.tag === 'script' && isSetupScript(node),
4355
) as ElementNode | undefined
4456
if (!setupScript) {
45-
return `<script setup>${importStatement}</script>
57+
return `<script setup${langAttr(script)}>${importStatement}</script>
4658
${code}`
4759
}
4860
const startAt = setupScript.children[0].loc.start.offset

test/__snapshots__/index.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { FormKit } from '@formkit/vue'
1818
`;
1919

2020
exports[`index > injects setup block when using options api 1`] = `
21-
"<script setup>import { FormKitLazyProvider } from '@formkit/vue'</script>
21+
"<script setup lang=\\"ts\\">import { FormKitLazyProvider } from '@formkit/vue'</script>
2222
<script lang=\\"ts\\">
2323
import { FormKit } from '@formkit/vue'
2424

0 commit comments

Comments
 (0)