mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 00:52:01 +00:00
fix(nuxt): cleanup auto imports on regenerate (#4626)
This commit is contained in:
parent
64f437a754
commit
eb4b8c4c9d
@ -1,60 +0,0 @@
|
||||
import { promises as fsp, existsSync } from 'node:fs'
|
||||
import { parse as parsePath } from 'pathe'
|
||||
import { findExports } from 'mlly'
|
||||
import { camelCase } from 'scule'
|
||||
import { resolveFiles } from '@nuxt/kit'
|
||||
import { Unimport } from 'unimport'
|
||||
|
||||
export async function scanForComposables (dir: string | string[], ctx: Unimport) {
|
||||
const performScan = async (entry: string) => {
|
||||
const files = await resolveFiles(entry, [
|
||||
'*.{ts,js,mjs,cjs,mts,cts}',
|
||||
'*/index.{ts,js,mjs,cjs,mts,cts}'
|
||||
])
|
||||
|
||||
await ctx.modifyDynamicImports(async (dynamicImports) => {
|
||||
await Promise.all(
|
||||
files.map(async (path) => {
|
||||
// Remove original entries from the same import (for build watcher)
|
||||
filterInPlace(dynamicImports, i => i.from !== path)
|
||||
|
||||
const code = await fsp.readFile(path, 'utf-8')
|
||||
const exports = findExports(code)
|
||||
const defaultExport = exports.find(i => i.type === 'default')
|
||||
|
||||
if (defaultExport) {
|
||||
let name = parsePath(path).name
|
||||
if (name === 'index') {
|
||||
name = parsePath(path.split('/').slice(0, -1).join('/')).name
|
||||
}
|
||||
dynamicImports.push({ name: 'default', as: camelCase(name), from: path })
|
||||
}
|
||||
for (const exp of exports) {
|
||||
if (exp.type === 'named') {
|
||||
for (const name of exp.names) {
|
||||
dynamicImports.push({ name, as: name, from: path })
|
||||
}
|
||||
} else if (exp.type === 'declaration') {
|
||||
dynamicImports.push({ name: exp.name, as: exp.name, from: path })
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
for (const entry of Array.isArray(dir) ? dir : [dir]) {
|
||||
if (!existsSync(entry)) { continue }
|
||||
|
||||
await performScan(entry)
|
||||
}
|
||||
}
|
||||
|
||||
function filterInPlace<T> (arr: T[], predicate: (v: T) => any) {
|
||||
let i = arr.length
|
||||
while (i--) {
|
||||
if (!predicate(arr[i])) {
|
||||
arr.splice(i, 1)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,9 @@
|
||||
import { addVitePlugin, addWebpackPlugin, defineNuxtModule, addTemplate, resolveAlias, useNuxt, addPluginTemplate, logger } from '@nuxt/kit'
|
||||
import { isAbsolute, join, relative, resolve, normalize } from 'pathe'
|
||||
import { createUnimport, Import, toImports, Unimport } from 'unimport'
|
||||
import { createUnimport, Import, scanDirExports, toImports, Unimport } from 'unimport'
|
||||
import { AutoImportsOptions, ImportPresetWithDeprecation } from '@nuxt/schema'
|
||||
import { TransformPlugin } from './transform'
|
||||
import { defaultPresets } from './presets'
|
||||
import { scanForComposables } from './composables'
|
||||
|
||||
export default defineNuxtModule<Partial<AutoImportsOptions>>({
|
||||
meta: {
|
||||
@ -79,10 +78,11 @@ export default defineNuxtModule<Partial<AutoImportsOptions>>({
|
||||
}
|
||||
|
||||
const regenerateAutoImports = async () => {
|
||||
// Scan composables/
|
||||
await scanForComposables(composablesDirs, ctx)
|
||||
// Allow modules extending
|
||||
ctx.clearDynamicImports()
|
||||
await ctx.modifyDynamicImports(async (imports) => {
|
||||
// Scan composables/
|
||||
imports.push(...await scanDirExports(composablesDirs))
|
||||
// Modules extending
|
||||
await nuxt.callHook('autoImports:extend', imports)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user