Skip to content

Commit

Permalink
feat: improve addons API
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 24, 2022
1 parent 2f02ebf commit bd92eac
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/addons/vue-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { toImports, toTypeDeclrationItems } from '../utils'
const contextRE = /\b_ctx\.([\w_]+)\b/g

const vueTemplateAddon: Addon = {
transform (s, _, ctx) {
transform (s) {
if (!s.original.includes('_ctx.')) {
return s
}
Expand All @@ -13,7 +13,7 @@ const vueTemplateAddon: Addon = {
return s
}

const imports = ctx.imports
const imports = this.imports
const targets: Import[] = []

for (const match of matches) {
Expand Down Expand Up @@ -42,8 +42,8 @@ const vueTemplateAddon: Addon = {

return s
},
decleration (dts, ctx, options) {
const items = toTypeDeclrationItems(ctx.imports, options)
decleration (dts, options) {
const items = toTypeDeclrationItems(this.imports, options)
.map(i => i.replace('const ', ''))
return dts +
`
Expand Down
10 changes: 7 additions & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function createUnimport (opts: Partial<UnimportOptions>) {
}
let dts = toTypeDeclrationFile(ctx.imports, opts)
for (const addon of ctx.addons) {
dts = addon.decleration?.(dts, ctx, opts) ?? dts
dts = addon.decleration?.call(ctx, dts, opts) ?? dts
}
return dts
}
Expand Down Expand Up @@ -104,10 +104,14 @@ async function detectImports (code: string | MagicString, ctx: UnimportContext)
.forEach(i => matched.delete(i))
}

const matchedImports = Array.from(matched)
let matchedImports = Array.from(matched)
.map(name => ctx.map.get(name))
.filter(i => i && !i.disabled) as Import[]

for (const addon of ctx.addons) {
matchedImports = await addon.matchImports?.call(ctx, matched, matchedImports) || matchedImports
}

return {
strippedCode,
isCJSContext,
Expand All @@ -119,7 +123,7 @@ async function injectImports (code: string | MagicString, id: string | undefined
const s = getMagicString(code)

for (const addon of ctx.addons) {
await addon.transform(s, id, ctx)
await addon.transform?.call(ctx, s, id)
}

const { isCJSContext, matchedImports } = await detectImports(s, ctx)
Expand Down
7 changes: 5 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ export interface InjectImportsOptions {
mergeExisting?: boolean
}

export type Thenable<T> = Promise<T> | T

export interface Addon {
transform: (code: MagicString, id: string | undefined, ctx: UnimportContext) => MagicString | Promise<MagicString>
decleration?: (dts: string, ctx: UnimportContext, options: TypeDeclrationOptions) => string
transform?: (this: UnimportContext, code: MagicString, id: string | undefined) => Thenable<MagicString>
decleration?: (this: UnimportContext, dts: string, options: TypeDeclrationOptions) => string
matchImports?: (this: UnimportContext, scannedNames: Set<string>, matched: Import[]) => Thenable<Import[] | void>
}

0 comments on commit bd92eac

Please sign in to comment.