-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
199 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// TODO: Copy from [unjs/pathe](https://github.com/unjs/pathe/blob/main/src/utils.ts) | ||
import { join, toNamespacedPath } from 'pathe' | ||
|
||
const pathSeparators = new Set(['/', '\\', undefined]) | ||
|
||
const normalizedAliasSymbol = Symbol.for('pathe:normalizedAlias') | ||
|
||
export function normalizeAliases(_aliases: Record<string, string>) { | ||
if ((_aliases as any)[normalizedAliasSymbol]) { | ||
return _aliases | ||
} | ||
|
||
// Sort aliases from specific to general (ie. fs/promises before fs) | ||
const aliases = Object.fromEntries( | ||
Object.entries(_aliases).sort(([a], [b]) => _compareAliases(a, b)), | ||
) | ||
|
||
// Resolve alias values in relation to each other | ||
for (const key in aliases) { | ||
for (const alias in aliases) { | ||
// don't resolve a more specific alias with regard to a less specific one | ||
if (alias === key || key.startsWith(alias)) { | ||
continue | ||
} | ||
|
||
if ( | ||
aliases[key].startsWith(alias) | ||
&& pathSeparators.has(aliases[key][alias.length]) | ||
) { | ||
aliases[key] = aliases[alias] + aliases[key].slice(alias.length) | ||
} | ||
} | ||
} | ||
|
||
Object.defineProperty(aliases, normalizedAliasSymbol, { | ||
value: true, | ||
enumerable: false, | ||
}) | ||
return aliases | ||
} | ||
|
||
export function resolveAlias(path: string, aliases: Record<string, string>) { | ||
const _path = toNamespacedPath(path) | ||
aliases = normalizeAliases(aliases) | ||
for (const [alias, to] of Object.entries(aliases)) { | ||
if (!_path.startsWith(alias)) { | ||
continue | ||
} | ||
|
||
// Strip trailing slash from alias for check | ||
const _alias = hasTrailingSlash(alias) ? alias.slice(0, -1) : alias | ||
|
||
if (hasTrailingSlash(_path[_alias.length])) { | ||
return join(to, _path.slice(alias.length)) | ||
} | ||
} | ||
return _path | ||
} | ||
|
||
const FILENAME_RE = /(^|[/\\])([^/\\]+?)(?=(\.[^.]+)?$)/ | ||
|
||
export function filename(path: string) { | ||
return path.match(FILENAME_RE)?.[2] | ||
} | ||
|
||
// --- internals --- | ||
|
||
function _compareAliases(a: string, b: string) { | ||
return b.split('/').length - a.split('/').length | ||
} | ||
|
||
// Returns true if path ends with a slash or **is empty** | ||
function hasTrailingSlash(path = '/') { | ||
const lastChar = path[path.length - 1] | ||
return lastChar === '/' || lastChar === '\\' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export const one = 1 | ||
export const two = 2 | ||
export const getThree = () => 3 | ||
export function funcIndex() {} | ||
export class ClassIndex {} | ||
|
||
export type NumOrStr = number | string | ||
|
||
export * from './core/func1' | ||
export * from '~test/parser-lab/core/func2' | ||
|
||
export * from '@s3xysteak/utils' | ||
|
||
// --- Auto-Generated By Unplugin-Export-Collector --- | ||
|
||
const __UnExportList = ["ClassIndex","custom","fRe","func1","func2","func3","funcIndex","getThree","two"] as const | ||
|
||
/** | ||
* @returns Call in `resolvers` option of `unplugin-auto-import`. | ||
*/ | ||
export function autoImport(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>) { | ||
return (name: string) => { | ||
if (!__UnExportList.includes(name as any)) | ||
return | ||
|
||
return map && (map as any)[name] | ||
? { | ||
name, | ||
as: (map as any)[name], | ||
from: 'unplugin-export-collector', | ||
} | ||
: { | ||
name, | ||
from: 'unplugin-export-collector', | ||
} | ||
} | ||
} | ||
|
||
// --- Auto-Generated By Unplugin-Export-Collector --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters