Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom resolvers #23

Merged
merged 3 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
},
"extends": ["@antfu"],
"plugins": ["jest"],
"rules": {}
"rules": {
"react/jsx-no-undef": "off"
}
}
3 changes: 3 additions & 0 deletions examples/vite-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
"react-dom": "^17.0.0"
},
"devDependencies": {
"@iconify/json": "^1.1.396",
"@svgr/core": "^5.5.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@vitejs/plugin-react-refresh": "^1.3.1",
"typescript": "^4.4.2",
"unplugin-auto-import": "workspace:*",
"unplugin-icons": "^0.7.5",
"vite": "^2.5.1"
}
}
1 change: 1 addition & 0 deletions examples/vite-react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function App() {

return (
<div className="App">
<IconLogosReact style={{ fontSize: '3em' }}/>
<header className="App-header">
<p>
<button type="button" onClick={() => setCount(count => count + 1)}>
Expand Down
5 changes: 4 additions & 1 deletion examples/vite-react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
"jsx": "react",
"types": [
"unplugin-icons/types/react"
]
},
"include": ["./src"]
}
15 changes: 13 additions & 2 deletions examples/vite-react/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'
import React from '@vitejs/plugin-react-refresh'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import AutoImport from '../../src/vite'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
reactRefresh(),
React(),
Icons({
compiler: 'jsx',
jsx: 'react',
}),
AutoImport({
imports: 'react',
dts: './src/auto-imports.d.ts',
resolvers: [
IconsResolver({
componentPrefix: 'Icon',
}),
],
}),
],
})
155 changes: 153 additions & 2 deletions pnpm-lock.yaml

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

8 changes: 5 additions & 3 deletions src/core/dts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ImportsFlatMap } from '../types'

export function generateDeclration(imports: ImportsFlatMap) {
const body = Object
.entries(imports)
export function generateDeclration(imports: ImportsFlatMap, resolvedImports: ImportsFlatMap = {}) {
const body = [
...Object.entries(imports),
...Object.entries(resolvedImports),
]
.map(([name, info]) => ` const ${name}: typeof import('${info.module}')['${info.from || name}']`)
.join('\n')
return `// Generated by 'unplugin-auto-import'\ndeclare global {\n${body}\n}\nexport {}\n`
Expand Down
4 changes: 3 additions & 1 deletion src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ export function resolveOptions(options: Options = {}): ResolvedOptions {
const { dts = hasPkg('typescript') } = options
const resolved: ResolvedOptions = {
sourceMap: false,
resolvedImports: {},
presetOverriding: false,
...options,
dts: dts === false
? false
: dts === true
? resolve('auto-imports.d.ts')
: resolve(dts),
imports,
matchRE: new RegExp(`\\b(${Object.keys(imports).join('|')})\\b`, 'g'),
resolvers: toArray(options.resolvers),
idFilter: createFilter(
options.include || [/\.[jt]sx?$/, /\.vue\??/, /\.svelte$/],
options.exclude || [/node_modules/, /\.git/],
Expand Down