Skip to content

Commit fd04c98

Browse files
committed
feat: 0.3.0
1 parent 48476ef commit fd04c98

File tree

4 files changed

+96
-22
lines changed

4 files changed

+96
-22
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
Make the vue script setup syntax support the name attribute
66

77
## CHANGELOG
8+
9+
[0.3.0]
10+
- Feature
11+
- Expanded the function of automatic name generation
12+
`For details, refer to Options and extendIgnore`
13+
814
[0.2.5]
915
- Fix cjs exports
1016

@@ -37,6 +43,48 @@ Make the vue script setup syntax support the name attribute
3743
npm i unplugin-vue-setup-extend-plus
3844
```
3945

46+
## Options
47+
48+
```ts
49+
vueSetupExtend({
50+
// Advanced mode for name, not necessary
51+
mode?: 'none' | 'fileName' | 'relativeName' | Function
52+
// none: Cancel the setting of name.
53+
// e.g.
54+
// <script setup name="CustomName"> 'CustomName' will be ignored
55+
// fileName: Automatically read file names
56+
// e.g.
57+
// /user/demo/src/user/login.vue => Login
58+
// relativeName: Automatically read relative path names
59+
// e.g.
60+
// /user/demo/src/user/login.vue => UserLogin
61+
// Function: support a custom function
62+
// (fileId: string) => {
63+
// let name = ''
64+
// ...
65+
// return name
66+
// }
67+
// fileId e.g. /user/demo/src/user/login.vue
68+
})
69+
70+
```
71+
72+
## extendIgnore
73+
74+
`Since the user may define the name attribute of the component in the script tag, this conflicts with the default name set by this plugin. So you need to add a switch attribute to the script setup.`
75+
76+
```html
77+
<template>
78+
<div>hello world {{ a }}</div>
79+
</template>
80+
81+
// name="App" will be invalid
82+
<script lang="ts" setup name="App" inheritAttrs="false" extendIgnore>
83+
const a = 1
84+
</script>
85+
```
86+
87+
4088
<details>
4189
<summary>Vite</summary><br>
4290

src/core/transform.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { basename } from 'path'
1+
import { basename, parse as pathParse } from 'path'
22
import { compileScript, parse } from '@vue/compiler-sfc'
33
import MagicString from 'magic-string'
4+
import type { Options } from '../types'
45

5-
export function supportScriptName(code: string, id: string) {
6+
export function supportScriptName(code: string, id: string, options: Options) {
7+
const { mode } = options
68
let s: MagicString | undefined
79
const str = () => s || (s = new MagicString(code))
810
const { descriptor } = parse(code)
9-
if (!descriptor.script && descriptor.scriptSetup) {
11+
if (!descriptor.script && descriptor.scriptSetup && !descriptor.scriptSetup.attrs?.extendIgnore) {
1012
const result = compileScript(descriptor, { id })
11-
const name = result.attrs.name
13+
const name = typeof result.attrs.name === 'string' ? result.attrs.name : nameProcess(id, mode)
1214
const lang = result.attrs.lang
1315
const inheritAttrs = result.attrs.inheritAttrs
1416
if (name || inheritAttrs) {
@@ -39,3 +41,26 @@ export default defineComponent({
3941
return null
4042
}
4143
}
44+
45+
function nameProcess(id: string, mode: Options['mode']) {
46+
const commonId = id.replaceAll('\\', '/').split('?')[0]
47+
if (typeof mode === 'string') {
48+
const parseUrl = pathParse(commonId)
49+
const fileName = parseUrl.name
50+
const relativeName = parseUrl.dir.split('/').at(-1)
51+
if (mode === 'fileName')
52+
return camelize(fileName)
53+
54+
if (mode === 'relativeName')
55+
return camelize(`${relativeName}-${fileName}`)
56+
}
57+
58+
if (typeof mode === 'function')
59+
return mode(commonId)
60+
61+
return ''
62+
}
63+
64+
function camelize(str: string) {
65+
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '').replace(/(\w)/, (_, c) => c.toUpperCase())
66+
}

src/core/unplugin.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { createUnplugin } from 'unplugin'
2+
import type { Options } from '../types'
23
import { supportScriptName } from './transform'
34

4-
export default createUnplugin(() => ({
5-
name: 'unplugin-vue-setup-extend-plus',
6-
enforce: 'pre',
5+
export default createUnplugin((options: Options = {}) => {
6+
return {
7+
name: 'unplugin-vue-setup-extend-plus',
8+
enforce: 'pre',
79

8-
transformInclude(id) {
9-
return id.endsWith('.vue')
10-
},
10+
transformInclude(id) {
11+
return id.endsWith('.vue')
12+
},
1113

12-
async transform(code, id) {
13-
if (/\.vue$/.test(id) || /\.vue\?.*type=script.*/.test(id))
14-
return supportScriptName.call(this, code, id)
15-
return null
16-
},
17-
}))
14+
async transform(code, id) {
15+
if (options.mode && options.mode === 'none')
16+
return null
17+
if (/\.vue$/.test(id) || /\.vue\?.*type=script.*/.test(id))
18+
return supportScriptName.call(this, code, id, options)
19+
return null
20+
},
21+
}
22+
})

src/types.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1+
export type ModeType = 'none' | 'fileName' | 'relativeName'
12
export interface Options {
2-
/**
3-
* Turn on name extension
4-
* @default true
5-
*/
6-
// name?: boolean
7-
// inheritAttrs?: boolean
3+
mode?: ModeType | Function
84
}

0 commit comments

Comments
 (0)