-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(addon): add auto-import vue directives (#374)
Co-authored-by: Bobbie Goede <[email protected]> Co-authored-by: Anthony Fu <[email protected]>
- Loading branch information
1 parent
1d346ff
commit 225eb69
Showing
22 changed files
with
1,154 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
shamefully-hoist=true | ||
ignore-workspace-root-check=true | ||
shell-emulator=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const dummy = 'from manual composable preset' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import type { InlinePreset } from '../src' | ||
import type { UnimportPluginOptions } from '../src/unplugin' | ||
import * as process from 'node:process' | ||
import { resolve } from 'pathe' | ||
|
||
export function resolvePresets(presets: InlinePreset[]) { | ||
return presets.map((preset) => { | ||
preset.from = resolve(process.cwd(), preset.from) | ||
return preset | ||
}) | ||
} | ||
|
||
const usePresets = process.env.USE_PRESETS === 'true' | ||
|
||
const unimportViteOptions: Partial<UnimportPluginOptions> = { | ||
dts: true, | ||
// eslint-disable-next-line no-console | ||
debugLog: console.log, | ||
presets: [ | ||
'vue', | ||
{ | ||
from: resolve(process.cwd(), 'composables-preset/dummy.ts'), | ||
imports: [{ | ||
name: 'dummy', | ||
}], | ||
}, | ||
], | ||
dirs: ['./composables/**'], | ||
addons: { | ||
vueTemplate: true, | ||
vueDirectives: { | ||
isDirective(normalizeImportFrom, _importEntry) { | ||
return normalizeImportFrom.includes('/directives/') | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
if (!usePresets) { | ||
unimportViteOptions.dirsScanOptions = { | ||
cwd: process.cwd().replace(/\\/g, '/'), | ||
} | ||
unimportViteOptions.dirs!.push('./directives/**') | ||
} | ||
else { | ||
unimportViteOptions.presets!.push(...resolvePresets([{ | ||
from: 'directives/awesome-directive.ts', | ||
imports: [{ | ||
name: 'default', | ||
as: 'AwesomeDirective', | ||
meta: { | ||
vueDirective: true, | ||
}, | ||
}], | ||
}, { | ||
from: 'directives/named-directive.ts', | ||
imports: [{ | ||
name: 'NamedDirective', | ||
meta: { | ||
vueDirective: true, | ||
}, | ||
}], | ||
}, { | ||
from: 'directives/mixed-directive.ts', | ||
imports: [{ | ||
name: 'NamedMixedDirective', | ||
meta: { | ||
vueDirective: true, | ||
}, | ||
}, { | ||
name: 'default', | ||
as: 'MixedDirective', | ||
meta: { | ||
vueDirective: true, | ||
}, | ||
}], | ||
}, { | ||
from: 'directives/custom-directive.ts', | ||
imports: [{ | ||
name: 'CustomDirective', | ||
meta: { | ||
vueDirective: true, | ||
}, | ||
}], | ||
}, { | ||
from: 'directives/ripple-directive.ts', | ||
imports: [{ | ||
name: 'vRippleDirective', | ||
meta: { | ||
vueDirective: true, | ||
}, | ||
}], | ||
}, { | ||
from: 'directives/v-focus-directive.ts', | ||
imports: [{ | ||
name: 'default', | ||
as: 'FocusDirective', | ||
meta: { | ||
vueDirective: true, | ||
}, | ||
}], | ||
}])) | ||
} | ||
|
||
export { unimportViteOptions } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { DirectiveBinding } from 'vue' | ||
|
||
export function AwesomeDirective(el: HTMLElement, binding: DirectiveBinding) { | ||
// eslint-disable-next-line no-console | ||
console.log('AwesomeDirective', el, binding) | ||
} | ||
|
||
export default AwesomeDirective |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { DirectiveBinding } from 'vue' | ||
|
||
function mounted(el: HTMLElement, binding: DirectiveBinding) { | ||
// eslint-disable-next-line no-console | ||
console.log('mounted', el, binding) | ||
} | ||
|
||
function unmounted(el: HTMLElement, binding: DirectiveBinding) { | ||
// eslint-disable-next-line no-console | ||
console.log('unmounted', el, binding) | ||
} | ||
|
||
function updated(el: HTMLElement, binding: DirectiveBinding) { | ||
// eslint-disable-next-line no-console | ||
console.log('updated', el, binding) | ||
} | ||
|
||
export const CustomDirective = { | ||
mounted, | ||
unmounted, | ||
updated, | ||
} | ||
|
||
export default CustomDirective |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { DirectiveBinding } from 'vue' | ||
|
||
export function NamedMixedDirective(el: HTMLElement, binding: DirectiveBinding) { | ||
// eslint-disable-next-line no-console | ||
console.log('NamedMixedDirective', el, binding) | ||
} | ||
|
||
export default function DefaultMixedDirective(el: HTMLElement, binding: DirectiveBinding) { | ||
// eslint-disable-next-line no-console | ||
console.log('DefaultMixedDirective', el, binding) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { DirectiveBinding } from 'vue' | ||
|
||
export function NamedDirective(el: HTMLElement, binding: DirectiveBinding) { | ||
// eslint-disable-next-line no-console | ||
console.log('NamedDirective', el, binding) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { DirectiveBinding } from 'vue' | ||
|
||
function vRippleDirective(el: HTMLElement, binding: DirectiveBinding) { | ||
// eslint-disable-next-line no-console | ||
console.log('FocusDirective', el, binding) | ||
} | ||
|
||
export { vRippleDirective } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { DirectiveBinding } from 'vue' | ||
|
||
function VFocusDirective(el: HTMLElement, binding: DirectiveBinding) { | ||
// eslint-disable-next-line no-console | ||
console.log('FocusDirective', el, binding) | ||
} | ||
|
||
export default VFocusDirective |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { AwesomeDirective } from '../directives/awesome-directive' | ||
export { CustomDirective } from '../directives/custom-directive' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './addons/vue-template' | ||
export { vueDirectivesAddon } from './addons/vue-directives' | ||
export { vueTemplateAddon } from './addons/vue-template' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { Addon, UnimportOptions } from '../types' | ||
import { VUE_DIRECTIVES_NAME, vueDirectivesAddon } from './vue-directives' | ||
import { VUE_TEMPLATE_NAME, vueTemplateAddon } from './vue-template' | ||
|
||
export function configureAddons(opts: Partial<UnimportOptions>) { | ||
const addons: Addon[] = [] | ||
|
||
if (Array.isArray(opts.addons)) { | ||
addons.push(...opts.addons) | ||
} | ||
else { | ||
const addonsMap = new Map<string, Addon>() | ||
if (opts.addons?.addons?.length) { | ||
let i = 0 | ||
for (const addon of opts.addons.addons) { | ||
addonsMap.set(addon.name || `external:custom-${i++}`, addon) | ||
} | ||
} | ||
|
||
if (opts.addons?.vueTemplate) { | ||
if (!addonsMap.has(VUE_TEMPLATE_NAME)) { | ||
addonsMap.set(VUE_TEMPLATE_NAME, vueTemplateAddon()) | ||
} | ||
} | ||
|
||
if (opts.addons?.vueDirectives) { | ||
if (!addonsMap.has(VUE_DIRECTIVES_NAME)) { | ||
addonsMap.set(VUE_DIRECTIVES_NAME, vueDirectivesAddon( | ||
typeof opts.addons.vueDirectives === 'object' | ||
? opts.addons.vueDirectives | ||
: undefined, | ||
)) | ||
} | ||
} | ||
|
||
addons.push(...addonsMap.values()) | ||
} | ||
|
||
return addons | ||
} |
Oops, something went wrong.