Skip to content

Commit a2e9095

Browse files
authored
feat: add local package preset support (#451)
1 parent 04b9e9e commit a2e9095

9 files changed

Lines changed: 47 additions & 2 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,22 @@ Refer to the [type definitions](./src/types.ts) for more options.
312312

313313
See [src/presets](./src/presets).
314314

315+
## Package Presets
316+
317+
We only provide presets for the most popular packages, to use any package not included here you can install it as dev dependency and add it to the `packagePresets` array option:
318+
```ts
319+
AutoImport({
320+
/* other options */
321+
packagePresets: ['detect-browser-es'/* other local package names */]
322+
})
323+
```
324+
325+
You can check the [Svelte example](./examples/vite-svelte) for a working example registering `detect-browser-es` package preset and auto importing `detect` function in [App.svelte](./examples/vite-svelte/src/App.svelte).
326+
327+
Please refer to the [unimport PackagePresets jsdocs](https://github.com/unjs/unimport/blob/main/src/types.ts) for more information about options like `ignore` or `cache`.
328+
329+
**Note**: ensure local packages used have package exports configured properly, otherwise the corresponding modules exports will not be detected.
330+
315331
## TypeScript
316332

317333
In order to properly hint types for auto-imported APIs

examples/vite-svelte/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"devDependencies": {
1212
"@sveltejs/vite-plugin-svelte": "^3.0.0",
1313
"@tsconfig/svelte": "^5.0.2",
14+
"detect-browser-es": "^0.1.0",
1415
"svelte": "^4.2.5",
1516
"svelte-check": "^3.6.0",
1617
"svelte-preprocess": "^5.1.0",

examples/vite-svelte/src/App.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
onMount(function() {
77
console.log('onMount called')
88
})
9+
10+
$:browser = detect()
911
</script>
1012

1113
<main>
@@ -25,5 +27,7 @@
2527
<br />
2628

2729
<Counter />
30+
31+
<pre>Browser: { browser?.name }</pre>
2832
</main>
2933

examples/vite-svelte/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default defineConfig({
1414
'svelte/store',
1515
'svelte/transition',
1616
],
17+
packagePresets: ['detect-browser-es'],
1718
dts: './src/auto-imports.d.ts',
1819
}),
1920
],

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/ctx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function createContext(options: Options = {}, root = process.cwd()) {
5353

5454
const unimport = createUnimport({
5555
imports: [],
56-
presets: [],
56+
presets: options.packagePresets?.map(p => typeof p === 'string' ? { package: p } : p) ?? [],
5757
injectAtEnd,
5858
addons: [
5959
...(options.vueTemplate ? [vueTemplateAddon()] : []),

src/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Arrayable, Awaitable } from '@antfu/utils'
22
import type { FilterPattern } from '@rollup/pluginutils'
3-
import type { Import, InlinePreset } from 'unimport'
3+
import type { Import, InlinePreset, PackagePreset } from 'unimport'
44
import { PresetName } from './presets'
55

66
export interface ImportLegacy {
@@ -78,6 +78,16 @@ export interface Options {
7878
*/
7979
imports?: Arrayable<ImportsMap | PresetName | InlinePreset>
8080

81+
/**
82+
* Local package presets.
83+
*
84+
* Register local installed packages as a preset.
85+
*
86+
* @default []
87+
* @see https://github.com/unplugin/unplugin-auto-import#package-presets
88+
*/
89+
packagePresets?: (PackagePreset | string)[]
90+
8191
/**
8292
* Identifiers to be ignored
8393
*/

test/__snapshots__/dts.test.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ declare global {
1515
const $ref: typeof import('vue/macros')['$ref']
1616
const $shallowRef: typeof import('vue/macros')['$shallowRef']
1717
const $toRef: typeof import('vue/macros')['$toRef']
18+
const Bundle: typeof import('magic-string')['Bundle']
1819
const EffectScope: typeof import('vue-demi')['EffectScope']
20+
const SourceMap: typeof import('magic-string')['SourceMap']
1921
const afterUpdate: typeof import('svelte')['afterUpdate']
2022
const backIn: typeof import('svelte/easing')['backIn']
2123
const backInOut: typeof import('svelte/easing')['backInOut']
@@ -40,6 +42,7 @@ declare global {
4042
const customDefault: typeof import('custom')['default']
4143
const customNamed: typeof import('custom')['customNamed']
4244
const customRef: typeof import('vue-demi')['customRef']
45+
const default: typeof import('magic-string')['default']
4346
const defineAsyncComponent: typeof import('vue-demi')['defineAsyncComponent']
4447
const defineComponent: typeof import('vue-demi')['defineComponent']
4548
const derived: typeof import('svelte/store')['derived']

test/dts.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createContext } from '../src/core/ctx'
66
it('dts', async () => {
77
const cwd = process.cwd()
88
const ctx = createContext({
9+
packagePresets: ['magic-string'],
910
imports: [
1011
'vue-demi',
1112
'react',

0 commit comments

Comments
 (0)