Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat!(nuxt3): allow modules to manage autoImports on generateApp #3480

Merged
merged 4 commits into from
Mar 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 35 additions & 29 deletions packages/nuxt3/src/auto-imports/composables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,45 @@ import { AutoImport } from '@nuxt/schema'
import { resolveFiles } from '@nuxt/kit'
import { filterInPlace } from './utils'

export async function scanForComposables (dir: string, autoImports: AutoImport[]) {
if (!existsSync(dir)) { return }
export async function scanForComposables (dir: string | string[], autoImports: AutoImport[]) {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
const performScan = async (entry: string) => {
const files = await resolveFiles(entry, [
'*.{ts,js,mjs,cjs,mts,cts}',
'*/index.{ts,js,mjs,cjs,mts,cts}'
])

const files = await resolveFiles(dir, [
'*.{ts,js,mjs,cjs,mts,cts}',
'*/index.{ts,js,mjs,cjs,mts,cts}'
])
await Promise.all(
files.map(async (path) => {
// Remove original entries from the same import (for build watcher)
filterInPlace(autoImports, i => i.from !== path)

await Promise.all(
files.map(async (path) => {
// Remove original entries from the same import (for build watcher)
filterInPlace(autoImports, i => i.from !== path)
const code = await fsp.readFile(path, 'utf-8')
const exports = findExports(code)
const defaultExport = exports.find(i => i.type === 'default')

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
if (defaultExport) {
let name = parsePath(path).name
if (name === 'index') {
name = parsePath(path.split('/').slice(0, -1).join('/')).name
}
autoImports.push({ name: 'default', as: camelCase(name), from: path })
}
autoImports.push({ name: 'default', as: camelCase(name), from: path })
}
for (const exp of exports) {
if (exp.type === 'named') {
for (const name of exp.names) {
autoImports.push({ name, as: name, from: path })
for (const exp of exports) {
if (exp.type === 'named') {
for (const name of exp.names) {
autoImports.push({ name, as: name, from: path })
}
} else if (exp.type === 'declaration') {
autoImports.push({ name: exp.name, as: exp.name, from: path })
}
} else if (exp.type === 'declaration') {
autoImports.push({ name: exp.name, as: exp.name, from: path })
}
}
})
)
})
)
}

for (const entry of Array.isArray(dir) ? dir : [dir]) {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
if (!existsSync(entry)) { continue }

await performScan(entry)
}
}
9 changes: 5 additions & 4 deletions packages/nuxt3/src/auto-imports/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ export default defineNuxtModule<AutoImportsOptions>({
: { name: importName.name, as: importName.as || importName.name, from: source.from }
))
// Scan composables/
for (const composablesDir of composablesDirs) {
await scanForComposables(composablesDir, ctx.autoImports)
}
await scanForComposables(composablesDirs, ctx.autoImports)
// Allow modules extending
await nuxt.callHook('autoImports:extend', ctx.autoImports)
// Update context
Expand All @@ -96,10 +94,13 @@ export default defineNuxtModule<AutoImportsOptions>({
nuxt.hook('builder:watch', async (_, path) => {
const _resolved = resolve(nuxt.options.srcDir, path)
if (composablesDirs.find(dir => _resolved.startsWith(dir))) {
await regenerateAutoImports()
await nuxt.callHook('builder:generateApp')
}
})

nuxt.hook('builder:generateApp', async () => {
await regenerateAutoImports()
})
}
})

Expand Down