|
1 |
| -import { basename } from 'path' |
| 1 | +import { basename, parse as pathParse } from 'path' |
2 | 2 | import { compileScript, parse } from '@vue/compiler-sfc'
|
3 | 3 | import MagicString from 'magic-string'
|
| 4 | +import type { Options } from '../types' |
4 | 5 |
|
5 |
| -export function supportScriptName(code: string, id: string) { |
| 6 | +export function supportScriptName(code: string, id: string, options: Options) { |
| 7 | + const { mode } = options |
6 | 8 | let s: MagicString | undefined
|
7 | 9 | const str = () => s || (s = new MagicString(code))
|
8 | 10 | const { descriptor } = parse(code)
|
9 |
| - if (!descriptor.script && descriptor.scriptSetup) { |
| 11 | + if (!descriptor.script && descriptor.scriptSetup && !descriptor.scriptSetup.attrs?.extendIgnore) { |
10 | 12 | 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) |
12 | 14 | const lang = result.attrs.lang
|
13 | 15 | const inheritAttrs = result.attrs.inheritAttrs
|
14 | 16 | if (name || inheritAttrs) {
|
@@ -39,3 +41,26 @@ export default defineComponent({
|
39 | 41 | return null
|
40 | 42 | }
|
41 | 43 | }
|
| 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 | +} |
0 commit comments