Skip to content

Commit

Permalink
feat: resolve priority from presets
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 11, 2022
1 parent 0c7adb6 commit 2353807
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import type { Preset, Import } from './types'

export function resolvePreset (preset: Preset): Import[] {
const imports: Import[] = []

const defaults: Omit<Import, 'name'> = {
from: preset.from
}
if (preset.priority != null) {
defaults.priority = preset.priority
}

for (const _import of preset.imports) {
if (typeof _import === 'string') {
imports.push({ name: _import, as: _import, from: preset.from })
imports.push({ ...defaults, name: _import, as: _import })
} else if (Array.isArray(_import)) {
imports.push({ name: _import[0], as: _import[1] || _import[0], from: _import[2] || preset.from })
imports.push({ ...defaults, name: _import[0], as: _import[1] || _import[0], from: _import[2] || preset.from })
} else if ((_import as Preset).imports) {
imports.push(...resolvePreset(_import as Preset))
} else {
imports.push({ from: preset.from, ..._import as Import })
imports.push({ ...defaults, ..._import as Import })
}
}
return imports
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type PresetImport = ImportName | [name: ImportName, as?: ImportName, from
export interface Preset {
from: ModuleId
imports: (PresetImport | Preset)[]
priority?: number
}

export interface UnimportOptions {
Expand Down

0 comments on commit 2353807

Please sign in to comment.