Skip to content

Commit

Permalink
fix: ensure lib name is valid
Browse files Browse the repository at this point in the history
fix #247
  • Loading branch information
qmhc committed Jul 17, 2023
1 parent 0d84180 commit 7cfe2ea
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/ts/package.json
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"build": "DEBUG=\"vite-plugin-dts:bundle\" vite build"
},
"types": "dist/ts-test.d.ts",
"devDependencies": {
"tslib": "^2.5.3",
"typescript": "5.0.4",
Expand Down
1 change: 1 addition & 0 deletions examples/ts/src/index.ts
Expand Up @@ -15,6 +15,7 @@ export { manualDts } from './manual-dts'
export { ParametersTest, test, method } from './test'

export { data }
export default data

export type { User } from './types'
export type { AliasType } from '@alias/type'
6 changes: 3 additions & 3 deletions examples/ts/vite.config.ts
Expand Up @@ -15,7 +15,7 @@ export default defineConfig({
build: {
lib: {
entry: [resolve(__dirname, 'src/index.ts'), resolve(__dirname, 'src/main.ts')],
name: 'Test',
name: 'ts-test',
formats: ['es']
}
},
Expand All @@ -26,8 +26,8 @@ export default defineConfig({
exclude: ['src/ignore'],
// aliasesExclude: [/^@components/],
staticImport: true,
rollupTypes: true,
insertTypesEntry: true
// insertTypesEntry: true,
rollupTypes: true
})
]
})
Expand Down
3 changes: 2 additions & 1 deletion src/plugin.ts
Expand Up @@ -28,6 +28,7 @@ import {
removeDirIfEmpty,
resolve,
runParallel,
toCapitalCase,
tryGetPkgPath,
wrapPromise
} from './utils'
Expand Down Expand Up @@ -289,7 +290,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
host = ts.createCompilerHost(compilerOptions, true)
program = createProgram({ host, rootNames, options: compilerOptions })

libName = libName || '_default'
libName = toCapitalCase(libName || '_default')
indexName = indexName || defaultIndex

const maybeEmitted = (sourceFile: ts.SourceFile) => {
Expand Down
16 changes: 16 additions & 0 deletions src/utils.ts
Expand Up @@ -306,3 +306,19 @@ export function tryGetPkgPath(beginPath: string) {

return tryGetPkgPath(parentDir)
}

type CapitalCase<T extends string> = T extends `${infer First} ${infer Rest}`
? CapitalCase<`${First}-${Rest}`>
: T extends `${infer First}-${infer Rest}`
? `${Capitalize<First>}${CapitalCase<Rest>}`
: Capitalize<T>

export function toCapitalCase<T extends string>(value: T) {
value = value.trim().replace(/\s+/g, '-') as T
value = value.replace(/-+(\w)/g, (_, char) => (char ? char.toUpperCase() : '')) as T

return (value.charAt(0).toLocaleUpperCase() + value.slice(1)).replace(
/[^\w]/g,
''
) as CapitalCase<T>
}
17 changes: 16 additions & 1 deletion tests/utils.spec.ts
Expand Up @@ -13,7 +13,8 @@ import {
isRegExp,
mergeObjects,
normalizePath,
queryPublicPath
queryPublicPath,
toCapitalCase
} from '../src/utils'

describe('utils tests', () => {
Expand Down Expand Up @@ -149,4 +150,18 @@ describe('utils tests', () => {
expect(base64VLQEncode([i - 255])).toEqual(snapshots[i])
}
})

it('test: toCapitalCase', () => {
expect(toCapitalCase('abc')).toEqual('Abc')
expect(toCapitalCase('aa-bb-cc')).toEqual('AaBbCc')
expect(toCapitalCase('aa_bb_cc')).toEqual('Aa_bb_cc')
expect(toCapitalCase('_aa-bb-cc')).toEqual('_aaBbCc')
expect(toCapitalCase('aa--bb')).toEqual('AaBb')
expect(toCapitalCase('aa bb cc')).toEqual('AaBbCc')
expect(toCapitalCase('aa -bb- cc')).toEqual('AaBbCc')
expect(toCapitalCase(' aa bb cc')).toEqual('AaBbCc')
expect(toCapitalCase(' aa bb cc ')).toEqual('AaBbCc')
expect(toCapitalCase('-aa bb cc ')).toEqual('AaBbCc')
expect(toCapitalCase(' -aa bb cc -')).toEqual('AaBbCc')
})
})

0 comments on commit 7cfe2ea

Please sign in to comment.