Skip to content

Commit efdf8f3

Browse files
committed
feat: volar plugin
1 parent 381dc19 commit efdf8f3

File tree

8 files changed

+112
-8
lines changed

8 files changed

+112
-8
lines changed

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
"require": "./dist/esbuild.js",
4848
"import": "./dist/esbuild.mjs"
4949
},
50+
"./volar": {
51+
"require": "./dist/volar.js",
52+
"import": "./dist/volar.mjs"
53+
},
5054
"./*": "./*"
5155
},
5256
"typesVersions": {
@@ -79,6 +83,7 @@
7983
"@sxzz/prettier-config": "^1.0.4",
8084
"@types/node": "^20.8.4",
8185
"@vue-macros/test-utils": "^1.1.3",
86+
"@vue/language-core": "^1.8.19",
8287
"bumpp": "^9.2.0",
8388
"change-case": "^5.0.2",
8489
"eslint": "^8.51.0",

pnpm-lock.yaml

+67
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import path from 'node:path'
2+
import { camelCase } from 'change-case'
3+
4+
export function resolveName(id: string) {
5+
return camelCase(path.basename(id, 'vue'))
6+
}

src/index.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import path from 'node:path'
21
import { createUnplugin } from 'unplugin'
3-
import { camelCase } from 'change-case'
42
import {
53
MagicStringBase,
64
babelParse,
@@ -9,12 +7,9 @@ import {
97
getLang,
108
} from '@vue-macros/common'
119
import { type Options, resolveOption } from './core/options'
10+
import { resolveName } from './core/utils'
1211
import type { ExportDefaultDeclaration, Node } from '@babel/types'
1312

14-
function resolveName(id: string) {
15-
return camelCase(path.basename(id, 'vue'))
16-
}
17-
1813
function getNodeStart(node: Node) {
1914
if (node.leadingComments && node.leadingComments.length > 0) {
2015
return node.leadingComments[0].start!

src/volar.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { FileKind, type VueLanguagePlugin, replace } from '@vue/language-core'
2+
import { resolveName } from './core/utils'
3+
4+
const plugin: VueLanguagePlugin = () => {
5+
return {
6+
name: 'vue-named-export',
7+
version: 1,
8+
resolveEmbeddedFile(fileName, sfc, embeddedFile) {
9+
if (embeddedFile.kind !== FileKind.TypeScriptHostFile) return
10+
const exportedName = resolveName(fileName)
11+
replace(
12+
embeddedFile.content,
13+
'export default ',
14+
`export const ${exportedName} = `
15+
)
16+
},
17+
}
18+
}
19+
20+
// eslint-disable-next-line import/no-default-export
21+
export default plugin

tests/fixtures/UPPER-CASE.vue

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script setup lang="ts">
2+
console.log('UPPER-CASE')
3+
</script>

tests/fixtures/main.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { basic } from './basic.vue'
2+
export { elCard } from './ElCard.vue'
3+
export { elButton } from './el-button.vue'
4+
export { myButton } from './my_button.vue'
5+
export { upperCase } from './UPPER-CASE.vue'

tsconfig.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"esModuleInterop": true,
1313
"skipLibCheck": true
1414
},
15-
"include": ["src", "tests"],
16-
"exclude": ["tests/fixtures"]
15+
"vueCompilerOptions": {
16+
"plugins": ["./dist/volar.js"]
17+
},
18+
"include": ["src", "tests"]
1719
}

0 commit comments

Comments
 (0)