forked from unplugin/unplugin-vue-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
177 lines (143 loc) · 4.94 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { join, parse, resolve } from 'path'
import minimatch from 'minimatch'
import { ResolvedConfig } from 'vite'
import { ComponentInfo, ResolvedOptions, Options, ImportInfo } from './types'
import { LibraryResolver } from './helpers/libraryResolver'
import { defaultOptions } from './constants'
import { Context } from './context'
export interface ResolveComponent {
filename: string
namespace?: string
}
export function slash(str: string) {
return str.replace(/\\/g, '/')
}
export function pascalCase(str: string) {
return capitalize(camelCase(str))
}
export function camelCase(str: string) {
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
}
export function kebabCase(key: string) {
const result = key.replace(/([A-Z])/g, ' $1').trim()
return result.split(' ').join('-').toLowerCase()
}
export function capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
export function toArray<T>(arr: T | T[]): T[] {
if (Array.isArray(arr))
return arr
return [arr]
}
export function parseId(id: string) {
const index = id.indexOf('?')
if (index < 0) {
return { path: id, query: {} }
}
else {
// @ts-ignore
const query = Object.fromEntries(new URLSearchParams(id.slice(index)))
return {
path: id.slice(0, index),
query,
}
}
}
export function isEmpty(value: any) {
if (!value || value === null || value === undefined || (Array.isArray(value) && Object.keys(value).length <= 0))
return true
else
return false
}
export function matchGlobs(filepath: string, globs: string[]) {
for (const glob of globs) {
if (minimatch(slash(filepath), glob))
return true
}
return false
}
export function stringifyImport(info: ImportInfo | string) {
if (typeof info === 'string')
return `import '${info}'`
if (!info.name)
return `import '${info.path}'`
else if (info.importName)
return `import { ${info.importName} as ${info.name} } from '${info.path}'`
else
return `import ${info.name} from '${info.path}'`
}
export function stringifyComponentImport({ name, path, importName, sideEffects }: ComponentInfo, ctx: Context) {
if (ctx.options.importPathTransform) {
const result = ctx.options.importPathTransform(path)
if (result != null)
path = result
}
const imports = [
stringifyImport({ name, path, importName }),
]
if (sideEffects)
toArray(sideEffects).forEach(i => imports.push(stringifyImport(i)))
return imports.join('\n')
}
export function resolveOptions(options: Options, viteConfig: ResolvedConfig): ResolvedOptions {
const resolved = Object.assign({}, defaultOptions, options) as ResolvedOptions
resolved.libraries = toArray(resolved.libraries).map(i => typeof i === 'string' ? { name: i } : i)
resolved.customComponentResolvers = toArray(resolved.customComponentResolvers)
resolved.customComponentResolvers.push(...resolved.libraries.map(lib => LibraryResolver(lib)))
resolved.extensions = toArray(resolved.extensions)
const extsGlob = resolved.extensions.length === 1
? resolved.extensions
: `{${resolved.extensions.join(',')}}`
resolved.dirs = toArray(resolved.dirs)
resolved.resolvedDirs = resolved.dirs.map(i => slash(resolve(viteConfig.root, i)))
resolved.globs = resolved.dirs.map(i =>
resolved.deep
? slash(join(i, `**/*.${extsGlob}`))
: slash(join(i, `*.${extsGlob}`)),
)
if (!resolved.extensions.length)
throw new Error('[vite-plugin-components] extensions are required to search for components')
return resolved
}
export function getNameFromFilePath(filePath: string, options: ResolvedOptions): string {
const { resolvedDirs, directoryAsNamespace, globalNamespaces } = options
const parsedFilePath = parse(slash(filePath))
let strippedPath = ''
// remove include directories from filepath
for (const dir of resolvedDirs) {
if (parsedFilePath.dir.startsWith(dir)) {
strippedPath = parsedFilePath.dir.slice(dir.length)
break
}
}
let folders = strippedPath.slice(1).split('/').filter(Boolean)
let filename = parsedFilePath.name
// set parent directory as filename if it is index
if (filename === 'index' && !directoryAsNamespace) {
filename = `${folders.slice(-1)[0]}`
return filename
}
if (directoryAsNamespace) {
// remove namesspaces from folder names
if (globalNamespaces.some((name: string) => folders.includes(name)))
folders = folders.filter(f => !globalNamespaces.includes(f))
if (filename.toLowerCase() === 'index')
filename = ''
if (!isEmpty(folders)) {
// add folders to filename
filename = [...folders, filename].filter(Boolean).join('-')
}
// console.log('!!!', filename)
return filename
}
return filename
}
export function resolveAlias(filepath: string, alias: ResolvedConfig['resolve']['alias'] = []) {
const result = filepath
if (Array.isArray(alias)) {
for (const { find, replacement } of alias)
result.replace(find, replacement)
}
return result
}