Skip to content

Commit 1323259

Browse files
VeekeeFrjulien_victor
authored and
julien_victor
committed
added namingRules
1 parent 172d8c3 commit 1323259

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

README.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,21 @@ export interface CommonjsOptions {
4848
onFiles?: (files: string[], id: string) => typeof files | undefined
4949
}
5050
advanced?: {
51-
/**
52-
* Custom import module interop behavior.
53-
*
54-
* If you want to fully customize the interop behavior,
55-
* you can pass a function and return the interop code snippet.
56-
*/
57-
importRules?: ImportInteropType | ((id: string) => ImportInteropType | string)
51+
/**
52+
* Custom import module interop behavior.
53+
*
54+
* If you want to fully customize the interop behavior,
55+
* you can pass a function and return the interop code snippet.
56+
*/
57+
importRules?: ImportInteropType | ((id: string) => ImportInteropType | string)
58+
59+
/**
60+
* Custom import ID handler.
61+
*
62+
* If you want to fully customize the generated import value,
63+
* you can pass a function and return the proper name.
64+
*/
65+
namingRules?: (id: string) => string
5866
}
5967
}
6068
```

src/generate-import.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ImportRecord {
88
}
99

1010
export function generateImport(analyzed: Analyzed, id: string, options: CommonjsOptions) {
11-
const { importRules } = options.advanced ?? {}
11+
const { namingRules, importRules } = options.advanced ?? {}
1212
const imports: ImportRecord[] = []
1313
let count = 0
1414

@@ -36,6 +36,10 @@ export function generateImport(analyzed: Analyzed, id: string, options: Commonjs
3636
${'^'.repeat(codeSnippets.length)}`)
3737
}
3838

39+
if (typeof namingRules === 'function') {
40+
requireId = namingRules(requireId)
41+
}
42+
3943
// This is probably less accurate, but is much cheaper than a full AST parse.
4044
let importInterop: ImportInteropType | string = 'defaultFirst'
4145
if (typeof importRules === 'string') {

src/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ export interface CommonjsOptions {
5151
* If you want to fully customize the interop behavior,
5252
* you can pass a function and return the interop code snippet.
5353
*/
54-
importRules?: ImportInteropType | ((id: string) => ImportInteropType | string)
54+
importRules?: ImportInteropType | ((id: string) => ImportInteropType | string),
55+
56+
/**
57+
* Custom import ID handler.
58+
*
59+
* If you want to fully customize the generated import value,
60+
* you can pass a function and return the proper name.
61+
*/
62+
namingRules?: (id: string) => string
5563
}
5664
}
5765

0 commit comments

Comments
 (0)