Skip to content

Commit

Permalink
feat!: support async resolver & compatible with unplugin-vue-componen…
Browse files Browse the repository at this point in the history
…ts, close unplugin#43 (unplugin#86)
  • Loading branch information
sxzz authored Nov 28, 2021
1 parent 6c0fcd2 commit 1082667
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 74 deletions.
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
"bugs": {
"url": "https://github.com/antfu/unplugin-auto-import/issues"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/antfu/unplugin-auto-import.git"
},
"funding": "https://github.com/sponsors/antfu",
"license": "MIT",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"sideEffects": false,
"files": [
"dist",
"*.d.ts"
],
"exports": {
".": {
"require": "./dist/index.js",
Expand Down Expand Up @@ -52,10 +56,6 @@
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist",
"*.d.ts"
],
"scripts": {
"build": "tsup src/*.ts --format cjs,esm --dts --splitting --clean && esno scripts/postbuild.ts",
"dev": "tsup 'src/*.ts' --watch src",
Expand All @@ -68,6 +68,14 @@
"test": "jest",
"test:update": "jest -u"
},
"peerDependencies": {
"@vueuse/core": "*"
},
"peerDependenciesMeta": {
"@vueuse/core": {
"optional": true
}
},
"dependencies": {
"@antfu/utils": "^0.3.0",
"@rollup/pluginutils": "^4.1.1",
Expand Down Expand Up @@ -95,14 +103,6 @@
"vite": "^2.6.14",
"webpack": "^5.64.4"
},
"peerDependencies": {
"@vueuse/core": "*"
},
"peerDependenciesMeta": {
"@vueuse/core": {
"optional": true
}
},
"engines": {
"node": ">=14"
}
Expand Down
23 changes: 15 additions & 8 deletions playground/App.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<template>
<div>
<hello-world :msg="msg" />
<div v-loading="true">
123
<ElButton>Hello</ElButton>
</div>
</template>

<script setup lang="ts">
import HelloWorld from './HelloWorld.vue'
<script lang="ts">
export default defineComponent({
components: {
ElButton,
},
directives: {
loading: vLoading,
},
})
</script>

const msg = ref('Global Imports')
const title = useTitle()
console.log(title.value) // print current title
title.value = 'Hello' // change current title
<script lang="ts" setup>
ElMessage.warning('Test')
</script>
2 changes: 2 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"devDependencies": {
"@vitejs/plugin-vue": "^1.10.1",
"@vue/compiler-sfc": "^3.2.23",
"element-plus": "^1.2.0-beta.4",
"unplugin-vue-components": "^0.17.2",
"vite": "^2.6.14",
"vite-plugin-inspect": "^0.3.11"
}
Expand Down
2 changes: 2 additions & 0 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import Inspect from 'vite-plugin-inspect'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import AutoImport from '../src/vite'

export default defineConfig({
Expand All @@ -9,6 +10,7 @@ export default defineConfig({
Inspect(),
AutoImport({
imports: ['vue', '@vueuse/core'],
resolvers: [...ElementPlusResolver()],
}),
],
})
86 changes: 83 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function generateDeclaration(imports: ImportsFlatMap, resolvedImports: Im
...Object.entries(resolvedImports),
]
.sort((a, b) => a[0].localeCompare(b[0]))
.map(([name, info]) => ` const ${name}: typeof import('${info.module}')['${info.from || name}']`)
.map(([name, info]) => ` const ${name}: typeof import('${info.path}')${info.importName !== '*' ? `['${info.importName || name}']` : ''}`)
.join('\n')
return `// Generated by 'unplugin-auto-import'\n// We suggest you to commit this file into source control\ndeclare global {\n${body}\n}\nexport {}\n`
}
20 changes: 11 additions & 9 deletions src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { toArray } from '@antfu/utils'
import { createFilter } from '@rollup/pluginutils'
import { isPackageExists } from 'local-pkg'
import { presets } from '../presets'
import { ImportInfo, ImportsFlatMap, Options, ResolvedOptions } from '../types'
import { ImportsFlatMap, Options, ResolvedOptions, ResolvedResult } from '../types'

export function resolveOptions(options: Options = {}): ResolvedOptions {
const imports = flattenImportsMap(options.imports, options.presetOverriding)
Expand Down Expand Up @@ -46,21 +46,23 @@ export function flattenImportsMap(map: Options['imports'], overriding = false):

for (const mod of Object.keys(definition)) {
for (const id of definition[mod]) {
const meta: ImportInfo = {
module: mod,
const meta: ResolvedResult = {
path: mod,
}
let name: string
if (Array.isArray(id)) {
meta.name = id[1]
meta.from = id[0]
name = id[1]
meta.importName = id[0]
}
else {
meta.name = id
name = id
meta.importName = id
}

if (flat[meta.name] && !overriding)
throw new Error(`[auto-import] identifier ${meta.name} already defined with ${flat[meta.name].module}`)
if (flat[name] && !overriding)
throw new Error(`[auto-import] identifier ${name} already defined with ${flat[name].path}`)

flat[meta.name] = meta
flat[name] = meta
}
}
})
Expand Down
Loading

0 comments on commit 1082667

Please sign in to comment.