Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@ export interface CommonjsOptions {
onFiles?: (files: string[], id: string) => typeof files | undefined
}
advanced?: {
/**
* Custom import module interop behavior.
*
* If you want to fully customize the interop behavior,
* you can pass a function and return the interop code snippet.
*/
importRules?: ImportInteropType | ((id: string) => ImportInteropType | string)
/**
* Custom import module interop behavior.
*
* If you want to fully customize the interop behavior,
* you can pass a function and return the interop code snippet.
*/
importRules?: ImportInteropType | ((id: string) => ImportInteropType | string)

/**
* Custom import ID handler.
*
* If you want to fully customize the generated import value,
* you can pass a function and return the proper name.
*/
namingRules?: (id: string, node: AcornNode) => string
}
}
```
Expand Down
6 changes: 5 additions & 1 deletion src/generate-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface ImportRecord {
}

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

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

if (typeof namingRules === 'function') {
requireId = namingRules(requireId, node)
}

// This is probably less accurate, but is much cheaper than a full AST parse.
let importInterop: ImportInteropType | string = 'defaultFirst'
if (typeof importRules === 'string') {
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ export interface CommonjsOptions {
* If you want to fully customize the interop behavior,
* you can pass a function and return the interop code snippet.
*/
importRules?: ImportInteropType | ((id: string) => ImportInteropType | string)
importRules?: ImportInteropType | ((id: string) => ImportInteropType | string),

/**
* Custom import ID handler.
*
* If you want to fully customize the generated import value,
* you can pass a function and return the proper name.
*/
namingRules?: (id: string, node: AcornNode) => string
}
}

Expand Down