2021-10-20 09:47:18 +00:00
|
|
|
import { addVitePlugin, addWebpackPlugin, defineNuxtModule, addTemplate, resolveAlias, addPluginTemplate, useNuxt } from '@nuxt/kit'
|
2021-10-18 13:39:53 +00:00
|
|
|
import type { AutoImportsOptions } from '@nuxt/kit'
|
2021-10-20 09:47:18 +00:00
|
|
|
import { isAbsolute, join, relative, resolve, normalize } from 'pathe'
|
2021-08-24 07:49:03 +00:00
|
|
|
import { TransformPlugin } from './transform'
|
2021-10-18 13:39:53 +00:00
|
|
|
import { Nuxt3AutoImports } from './imports'
|
2021-10-20 09:47:18 +00:00
|
|
|
import { scanForComposables } from './composables'
|
|
|
|
import { toImports } from './utils'
|
|
|
|
import { AutoImportContext, createAutoImportContext, updateAutoImportContext } from './context'
|
2021-08-10 00:27:23 +00:00
|
|
|
|
2021-10-11 08:07:27 +00:00
|
|
|
export default defineNuxtModule<AutoImportsOptions>({
|
|
|
|
name: 'auto-imports',
|
|
|
|
configKey: 'autoImports',
|
2021-10-18 13:39:53 +00:00
|
|
|
defaults: {
|
|
|
|
sources: Nuxt3AutoImports,
|
2021-10-20 09:47:18 +00:00
|
|
|
global: false,
|
|
|
|
dirs: []
|
2021-10-18 13:39:53 +00:00
|
|
|
},
|
|
|
|
async setup (options, nuxt) {
|
|
|
|
// Allow modules extending sources
|
|
|
|
await nuxt.callHook('autoImports:sources', options.sources)
|
|
|
|
|
|
|
|
// Filter disabled sources
|
|
|
|
options.sources = options.sources.filter(source => source.disabled !== true)
|
|
|
|
|
2021-10-20 09:47:18 +00:00
|
|
|
// Create a context to share state between module internals
|
|
|
|
const ctx = createAutoImportContext()
|
|
|
|
|
2021-10-18 13:39:53 +00:00
|
|
|
// Resolve autoimports from sources
|
|
|
|
for (const source of options.sources) {
|
|
|
|
for (const importName of source.names) {
|
|
|
|
if (typeof importName === 'string') {
|
2021-10-20 09:47:18 +00:00
|
|
|
ctx.autoImports.push({ name: importName, as: importName, from: source.from })
|
2021-10-18 13:39:53 +00:00
|
|
|
} else {
|
2021-10-20 09:47:18 +00:00
|
|
|
ctx.autoImports.push({ name: importName.name, as: importName.as || importName.name, from: source.from })
|
2021-10-18 13:39:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 09:47:18 +00:00
|
|
|
// composables/ dirs
|
|
|
|
let composablesDirs = [
|
|
|
|
join(nuxt.options.srcDir, 'composables'),
|
|
|
|
...options.dirs
|
|
|
|
]
|
|
|
|
await nuxt.callHook('autoImports:dirs', composablesDirs)
|
|
|
|
composablesDirs = composablesDirs.map(dir => normalize(dir))
|
2021-10-12 12:24:43 +00:00
|
|
|
|
2021-10-20 09:47:18 +00:00
|
|
|
// Transpile and injection
|
|
|
|
// @ts-ignore temporary disabled due to #746
|
2021-10-18 13:39:53 +00:00
|
|
|
if (nuxt.options.dev && options.global) {
|
2021-08-10 00:27:23 +00:00
|
|
|
// Add all imports to globalThis in development mode
|
|
|
|
addPluginTemplate({
|
2021-10-11 08:07:27 +00:00
|
|
|
filename: 'auto-imports.mjs',
|
2021-08-10 00:27:23 +00:00
|
|
|
src: '',
|
|
|
|
getContents: () => {
|
2021-10-20 09:47:18 +00:00
|
|
|
const imports = toImports(ctx.autoImports)
|
|
|
|
const globalThisSet = ctx.autoImports.map(i => `globalThis.${i.as} = ${i.as};`).join('\n')
|
2021-08-10 00:27:23 +00:00
|
|
|
return `${imports}\n\n${globalThisSet}\n\nexport default () => {};`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Transform to inject imports in production mode
|
2021-10-20 09:47:18 +00:00
|
|
|
addVitePlugin(TransformPlugin.vite(ctx))
|
|
|
|
addWebpackPlugin(TransformPlugin.webpack(ctx))
|
2021-08-10 00:27:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 09:47:18 +00:00
|
|
|
const updateAutoImports = async () => {
|
|
|
|
// Scan composables/
|
|
|
|
for (const composablesDir of composablesDirs) {
|
|
|
|
await scanForComposables(composablesDir, ctx.autoImports)
|
2021-10-05 20:47:19 +00:00
|
|
|
}
|
2021-10-20 09:47:18 +00:00
|
|
|
// Allow modules extending
|
|
|
|
await nuxt.callHook('autoImports:extend', ctx.autoImports)
|
|
|
|
// Update context
|
|
|
|
updateAutoImportContext(ctx)
|
|
|
|
// Generate types
|
|
|
|
generateDts(ctx)
|
2021-10-05 20:47:19 +00:00
|
|
|
}
|
2021-10-20 09:47:18 +00:00
|
|
|
await updateAutoImports()
|
2021-08-11 20:28:38 +00:00
|
|
|
|
2021-10-20 12:53:13 +00:00
|
|
|
// Add generated types to `nuxt.d.ts`
|
|
|
|
nuxt.hook('prepare:types', ({ references }) => {
|
|
|
|
references.push({ path: resolve(nuxt.options.buildDir, 'auto-imports.d.ts') })
|
|
|
|
})
|
|
|
|
|
2021-10-20 09:47:18 +00:00
|
|
|
// Watch composables/ directory
|
|
|
|
nuxt.hook('builder:watch', async (_, path) => {
|
|
|
|
const _resolved = resolve(nuxt.options.srcDir, path)
|
|
|
|
if (composablesDirs.find(dir => _resolved.startsWith(dir))) {
|
|
|
|
await updateAutoImports()
|
|
|
|
}
|
2021-08-10 00:27:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-10-20 09:47:18 +00:00
|
|
|
function generateDts (ctx: AutoImportContext) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
|
|
|
|
const resolved = {}
|
|
|
|
const r = (id: string) => {
|
|
|
|
if (resolved[id]) { return resolved[id] }
|
|
|
|
let path = resolveAlias(id, nuxt.options.alias)
|
|
|
|
if (isAbsolute(path)) {
|
|
|
|
path = relative(nuxt.options.buildDir, path)
|
2021-08-10 00:27:23 +00:00
|
|
|
}
|
2021-10-20 09:47:18 +00:00
|
|
|
// Remove file extension for benefit of TypeScript
|
|
|
|
path = path.replace(/\.[a-z]+$/, '')
|
|
|
|
resolved[id] = path
|
|
|
|
return path
|
2021-10-18 13:39:53 +00:00
|
|
|
}
|
2021-10-20 09:47:18 +00:00
|
|
|
|
|
|
|
addTemplate({
|
|
|
|
filename: 'auto-imports.d.ts',
|
|
|
|
write: true,
|
|
|
|
getContents: () => `// Generated by auto imports
|
|
|
|
declare global {
|
|
|
|
${ctx.autoImports.map(i => ` const ${i.as}: typeof import('${r(i.from)}')['${i.name}']`).join('\n')}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {}
|
|
|
|
`
|
|
|
|
})
|
2021-08-10 00:27:23 +00:00
|
|
|
}
|