Skip to content

Commit

Permalink
feat: improve aliasesExclude to support more flexible definition
Browse files Browse the repository at this point in the history
fix #93
  • Loading branch information
qmhc committed Sep 11, 2022
1 parent cc5514b commit f652523
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ export interface PluginOptions {
// Default: 'tsconfig.json'
tsConfigFilePath?: string

// Set which paths should exclude when transform aliases
// If it's regexp, it will test the original import path directly
// Default: []
aliasesExclude?: (string | RegExp)[]

// Whether transform file name '.vue.d.ts' to '.d.ts'
// Default: false
cleanVueFileName?: boolean
Expand Down
5 changes: 5 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export interface PluginOptions {
// 默认值: 'tsconfig.json'
tsConfigFilePath?: string

// 设置在转换别名时哪些路径需要排除
// 如果为正则,会直接使用 test 和原始路径进行比较
// 默认值: []
aliasesExclude?: (string | RegExp)[]

// 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
// 默认值: false
cleanVueFileName?: boolean
Expand Down
4 changes: 2 additions & 2 deletions example/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export default defineConfig({
outputDir: ['dist', 'types'],
// include: ['src/index.ts'],
exclude: ['src/ignore'],
aliasesExclude: ['@components'],
aliasesExclude: [/^@components/],
staticImport: true,
// skipDiagnostics: false,
// logDiagnostics: true,
// rollupTypes: true,
insertTypesEntry: true
// rollupTypes: true
}),
vue()
]
Expand Down
4 changes: 2 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface PluginOptions {
entryRoot?: string,
compilerOptions?: ts.CompilerOptions | null,
tsConfigFilePath?: string,
aliasesExclude?: Alias['find'][],
aliasesExclude?: (string | RegExp)[],
cleanVueFileName?: boolean,
staticImport?: boolean,
clearPureImport?: boolean,
Expand Down Expand Up @@ -395,7 +395,7 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin {

if (!isMapFile && content && content !== noneExport) {
content = clearPureImport ? removePureImport(content) : content
content = transformAliasImport(filePath, content, aliases)
content = transformAliasImport(filePath, content, aliases, aliasesExclude)
content = staticImport || rollupTypes ? transformDynamicImport(content) : content
}

Expand Down
13 changes: 12 additions & 1 deletion src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ const dynamicImportRE = /import\(['"]([^;\n]+?)['"]\)/
const simpleStaticImportRE = /((?:import|export).+from\s?)['"](.+)['"]/
const simpleDynamicImportRE = /(import\()['"](.+)['"]\)/

export function transformAliasImport(filePath: string, content: string, aliases: Alias[]) {
export function transformAliasImport(
filePath: string,
content: string,
aliases: Alias[],
exclude: (string | RegExp)[] = []
) {
if (!aliases.length) return content

return content.replace(globalImportRE, str => {
Expand All @@ -93,6 +98,12 @@ export function transformAliasImport(filePath: string, content: string, aliases:
const matchedAlias = aliases.find(alias => isAliasMatch(alias, matchResult![1]))

if (matchedAlias) {
if (
exclude.some(e => (isRegExp(e) ? e.test(matchResult![1]) : String(e) === matchResult![1]))
) {
return str
}

const truthPath = isAbsolute(matchedAlias.replacement)
? normalizePath(relative(dirname(filePath), matchedAlias.replacement))
: normalizePath(matchedAlias.replacement)
Expand Down

0 comments on commit f652523

Please sign in to comment.