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: support tsx, jsx by default and add option defaultExportByFilename #294

Merged
merged 2 commits into from Nov 25, 2022
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
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -81,3 +81,5 @@ dist
.idea
auto-imports.d.ts
.eslintrc-auto-import.json

.vscode
3 changes: 0 additions & 3 deletions examples/vite-react/src/App.tsx
@@ -1,7 +1,4 @@
import React from 'react'
import MainLayout from './layouts/MainLayout'
import PageA from './views/PageA'
import PageB from './views/PageB'
import './i18n'

function App() {
Expand Down
5 changes: 5 additions & 0 deletions examples/vite-react/vite.config.ts
Expand Up @@ -15,6 +15,11 @@ export default defineConfig({
AutoImport({
imports: ['react', 'react-router-dom', 'react-i18next', 'ahooks'],
dts: './src/auto-imports.d.ts',
dirs: ['src/layouts', 'src/views'],
eslintrc: {
enabled: true,
},
defaultExportByFilename: true,
resolvers: [
IconsResolver({
componentPrefix: 'Icon',
Expand Down
21 changes: 17 additions & 4 deletions src/core/ctx.ts
Expand Up @@ -66,7 +66,7 @@ export function createContext(options: Options = {}, root = process.cwd()) {
return unimport.generateTypeDeclarations({
resolvePath: (i) => {
if (i.from.startsWith('.') || isAbsolute(i.from)) {
const related = slash(relative(dir, i.from).replace(/\.ts$/, ''))
const related = slash(relative(dir, i.from).replace(/\.ts(x)?$/, ''))
return !related.startsWith('.')
? `./${related}`
: related
Expand Down Expand Up @@ -112,12 +112,14 @@ export function createContext(options: Options = {}, root = process.cwd()) {
async function scanDirs() {
if (dirs?.length) {
await unimport.modifyDynamicImports(async (imports) => {
const exports = await scanDirExports(dirs) as ImportExtended[]
const exports = await scanDirExports(dirs, {
filePatterns: ['*.{tsx,jsx,ts,js,mjs,cjs,mts,cts}'],
}) as ImportExtended[]
exports.forEach(i => i.__source = 'dir')
return [
return modifyDefaultExportsAlias([
...imports.filter((i: ImportExtended) => i.__source !== 'dir'),
...exports,
] as Import[]
], options)
})
}
writeConfigFilesThrottled()
Expand Down Expand Up @@ -192,3 +194,14 @@ export function flattenImports(map: Options['imports'], overriding = false): Imp

return Object.values(flat)
}

function modifyDefaultExportsAlias(imports: ImportExtended[], options: Options): Import[] {
if (options.defaultExportByFilename) {
imports.forEach((i) => {
if (i.name === 'default')
i.as = i.from.split('/').pop()?.split('.')?.shift() ?? i.as
})
}

return imports as Import[]
}
7 changes: 7 additions & 0 deletions src/types.ts
Expand Up @@ -113,6 +113,13 @@ export interface Options {
*/
vueTemplate?: boolean

/**
* Set default export alias by file name
*
* @default false
*/
defaultExportByFilename?: boolean

/**
* Allow overriding imports sources from multiple presets.
*
Expand Down