Skip to content

Commit

Permalink
feat: support tsx, jsx by default and add option `defaultExportByFile…
Browse files Browse the repository at this point in the history
…name` (#294)
  • Loading branch information
niku98 committed Nov 25, 2022
1 parent 8cbcde0 commit 632305e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
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

0 comments on commit 632305e

Please sign in to comment.