Skip to content

Commit 7cfe2ea

Browse files
committedJul 17, 2023
fix: ensure lib name is valid
fix #247
1 parent 0d84180 commit 7cfe2ea

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed
 

‎examples/ts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"scripts": {
88
"build": "DEBUG=\"vite-plugin-dts:bundle\" vite build"
99
},
10+
"types": "dist/ts-test.d.ts",
1011
"devDependencies": {
1112
"tslib": "^2.5.3",
1213
"typescript": "5.0.4",

‎examples/ts/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export { manualDts } from './manual-dts'
1515
export { ParametersTest, test, method } from './test'
1616

1717
export { data }
18+
export default data
1819

1920
export type { User } from './types'
2021
export type { AliasType } from '@alias/type'

‎examples/ts/vite.config.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default defineConfig({
1515
build: {
1616
lib: {
1717
entry: [resolve(__dirname, 'src/index.ts'), resolve(__dirname, 'src/main.ts')],
18-
name: 'Test',
18+
name: 'ts-test',
1919
formats: ['es']
2020
}
2121
},
@@ -26,8 +26,8 @@ export default defineConfig({
2626
exclude: ['src/ignore'],
2727
// aliasesExclude: [/^@components/],
2828
staticImport: true,
29-
rollupTypes: true,
30-
insertTypesEntry: true
29+
// insertTypesEntry: true,
30+
rollupTypes: true
3131
})
3232
]
3333
})

‎src/plugin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
removeDirIfEmpty,
2929
resolve,
3030
runParallel,
31+
toCapitalCase,
3132
tryGetPkgPath,
3233
wrapPromise
3334
} from './utils'
@@ -289,7 +290,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
289290
host = ts.createCompilerHost(compilerOptions, true)
290291
program = createProgram({ host, rootNames, options: compilerOptions })
291292

292-
libName = libName || '_default'
293+
libName = toCapitalCase(libName || '_default')
293294
indexName = indexName || defaultIndex
294295

295296
const maybeEmitted = (sourceFile: ts.SourceFile) => {

‎src/utils.ts

+16
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,19 @@ export function tryGetPkgPath(beginPath: string) {
306306

307307
return tryGetPkgPath(parentDir)
308308
}
309+
310+
type CapitalCase<T extends string> = T extends `${infer First} ${infer Rest}`
311+
? CapitalCase<`${First}-${Rest}`>
312+
: T extends `${infer First}-${infer Rest}`
313+
? `${Capitalize<First>}${CapitalCase<Rest>}`
314+
: Capitalize<T>
315+
316+
export function toCapitalCase<T extends string>(value: T) {
317+
value = value.trim().replace(/\s+/g, '-') as T
318+
value = value.replace(/-+(\w)/g, (_, char) => (char ? char.toUpperCase() : '')) as T
319+
320+
return (value.charAt(0).toLocaleUpperCase() + value.slice(1)).replace(
321+
/[^\w]/g,
322+
''
323+
) as CapitalCase<T>
324+
}

‎tests/utils.spec.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
isRegExp,
1414
mergeObjects,
1515
normalizePath,
16-
queryPublicPath
16+
queryPublicPath,
17+
toCapitalCase
1718
} from '../src/utils'
1819

1920
describe('utils tests', () => {
@@ -149,4 +150,18 @@ describe('utils tests', () => {
149150
expect(base64VLQEncode([i - 255])).toEqual(snapshots[i])
150151
}
151152
})
153+
154+
it('test: toCapitalCase', () => {
155+
expect(toCapitalCase('abc')).toEqual('Abc')
156+
expect(toCapitalCase('aa-bb-cc')).toEqual('AaBbCc')
157+
expect(toCapitalCase('aa_bb_cc')).toEqual('Aa_bb_cc')
158+
expect(toCapitalCase('_aa-bb-cc')).toEqual('_aaBbCc')
159+
expect(toCapitalCase('aa--bb')).toEqual('AaBb')
160+
expect(toCapitalCase('aa bb cc')).toEqual('AaBbCc')
161+
expect(toCapitalCase('aa -bb- cc')).toEqual('AaBbCc')
162+
expect(toCapitalCase(' aa bb cc')).toEqual('AaBbCc')
163+
expect(toCapitalCase(' aa bb cc ')).toEqual('AaBbCc')
164+
expect(toCapitalCase('-aa bb cc ')).toEqual('AaBbCc')
165+
expect(toCapitalCase(' -aa bb cc -')).toEqual('AaBbCc')
166+
})
152167
})

0 commit comments

Comments
 (0)
Please sign in to comment.