Skip to content

Commit 9e1da21

Browse files
committed
fix: provide a way to resolve alias
1 parent d7786c1 commit 9e1da21

File tree

9 files changed

+46
-15
lines changed

9 files changed

+46
-15
lines changed

src/context.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { relative } from 'path'
12
import Debug from 'debug'
23
import { ComponentsInfo, ComponentsImportMap, Options } from './types'
3-
import { normalize, toArray, getNameFromFilePath } from './utils'
4+
import { normalize, toArray, getNameFromFilePath, resolveAlias } from './utils'
45
import { searchComponents } from './fs/glob'
56

67
const debug = {
@@ -89,11 +90,24 @@ export class Context {
8990
.filter(Boolean) as ComponentsInfo[]
9091
}
9192

93+
normalizePath(path: string) {
94+
return this.relative(this.resolveAlias(path))
95+
}
96+
97+
resolveAlias(path: string) {
98+
return resolveAlias(path, this.options.alias)
99+
}
100+
101+
relative(path: string) {
102+
return relative(this.root, path)
103+
}
104+
92105
setImports(key: string, names: string[]) {
93106
const casedNames = names.map(name => normalize(name))
94107
this._imports[key] = casedNames
95108
if (this._importsResolveTasks[key])
96109
this._importsResolveTasks[key][1]?.(casedNames)
110+
return key
97111
}
98112

99113
/**

src/generator/resolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function isResolverPath(reqPath: string) {
99
}
1010

1111
export async function generateResolver(ctx: Context, reqPath: string) {
12-
const sfcPath = reqPath.slice(0, -RESOLVER_EXT.length)
12+
const sfcPath = ctx.normalizePath(reqPath.slice(0, -RESOLVER_EXT.length))
1313
debug(sfcPath)
1414

1515
const names = await ctx.getImports(sfcPath) || []

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const defaultOptions: Options = {
1010
dirs: 'src/components',
1111
extensions: 'vue',
1212
deep: true,
13+
alias: {},
1314
}
1415

1516
function VitePluginComponents(options: Partial<Options> = {}): Plugin {

src/plugins/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ServerPlugin } from 'vite'
22
import Debug from 'debug'
33
import { isResolverPath, generateResolver } from '../generator/resolver'
44
import { Context } from '../context'
5-
import { matchGlobs, relative } from '../utils'
5+
import { matchGlobs } from '../utils'
66

77
const debug = {
88
add: Debug('vite-plugin-components:watcher:add'),
@@ -20,7 +20,7 @@ export function createServerPlugin(ctx: Context): ServerPlugin {
2020
}
2121

2222
watcher.on('add', (e) => {
23-
const path = relative(e)
23+
const path = ctx.normalizePath(e)
2424
if (matchGlobs(path, ctx.globs)) {
2525
debug.add(path)
2626
if (ctx.addComponents(path))
@@ -29,7 +29,7 @@ export function createServerPlugin(ctx: Context): ServerPlugin {
2929
})
3030

3131
watcher.on('unlink', (e) => {
32-
const path = relative(e)
32+
const path = ctx.normalizePath(e)
3333
if (matchGlobs(path, ctx.globs)) {
3434
debug.remove(path)
3535
if (ctx.removeComponents(path))

src/transforms/vueScript.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Transform } from 'vite'
22
import Debug from 'debug'
33
import { Context } from '../context'
44
import { RESOLVER_EXT } from '../constants'
5-
import { relative } from '../utils'
65

76
const debug = Debug('vite-plugin-components:transform:script')
87

@@ -18,7 +17,7 @@ export function VueScriptTransformer(ctx: Context): Transform {
1817
return path.endsWith('.vue') && !query.type
1918
},
2019
transform({ code, path, isBuild }) {
21-
const filepath = relative(path)
20+
const filepath = ctx.relative(path)
2221
debug(filepath)
2322
const lines = code.split('\n')
2423

src/transforms/vueTemplate.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Debug from 'debug'
22
import type { Transform } from 'vite'
33
import { Context } from '../context'
4-
import { relative } from '../utils'
54

65
const debug = Debug('vite-plugin-components:transform:template')
76

@@ -18,9 +17,9 @@ export function VueTemplateTransformer(ctx: Context): Transform {
1817
return path.endsWith('.vue') && query.type === 'template'
1918
},
2019
transform({ code, path }) {
21-
const filepath = relative(path)
20+
const filepath = ctx.normalizePath(path)
2221
const imports = Array.from(code.matchAll(/_resolveComponent\("(.*)"\)/g)).map(i => i[1])
23-
ctx.setImports(`/${filepath}`, imports)
22+
ctx.setImports(filepath, imports)
2423
debug(filepath, imports)
2524
return code
2625
},

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ export interface Options {
1717
* @default true
1818
*/
1919
deep: boolean
20+
/**
21+
* Path alias, same as what you passed to vite root config
22+
* @default {}
23+
*/
24+
alias: Record<string, string>
2025
}
2126

2227
export interface ComponentsInfo {

src/utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ export function getNameFromFilePath(filePath: string): string {
3939
return parsedFilePath.name
4040
}
4141

42-
const cwd = process.cwd()
43-
44-
export function relative(filepath: string) {
45-
return path.relative(cwd, filepath)
42+
export function resolveAlias(filepath: string, alias: Record<string, string>) {
43+
let result = filepath
44+
Object.entries(alias).forEach(([k, p]) => {
45+
if (k.startsWith('/') && k.endsWith('/') && result.startsWith(k))
46+
result = path.join(p, result.replace(k, ''))
47+
})
48+
return result
4649
}

test/fixture/vite.config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import path from 'path'
12
import { UserConfig } from 'vite'
23
import ViteComponents from 'vite-plugin-components'
34

5+
const alias = {
6+
'/~/': path.resolve(__dirname, 'src'),
7+
}
8+
49
const config: UserConfig = {
10+
alias: {
11+
'/~/': path.resolve(__dirname, 'src'),
12+
},
513
plugins: [
6-
ViteComponents(),
14+
ViteComponents({
15+
alias,
16+
}),
717
],
818
}
919

0 commit comments

Comments
 (0)