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

feat(kit): add registerComponentDirectory helper #1379

Merged
merged 7 commits into from
Oct 22, 2021
Merged
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
15 changes: 14 additions & 1 deletion packages/kit/src/module/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { NuxtCompatibilityConstraints, NuxtCompatibilityIssues } from '../types/
import { Nuxt } from '../types/nuxt'
import { useNuxt } from '../nuxt'
import type { NuxtTemplate, NuxtPlugin, NuxtPluginTemplate } from '../types/nuxt'
import type { ComponentsDir } from '../types/components'

/**
* Renders given template using lodash template during build into the project buildDir
Expand Down Expand Up @@ -289,7 +290,19 @@ export async function compileTemplate (template: NuxtTemplate, ctx: any) {
throw new Error('Invalid template: ' + JSON.stringify(template))
}

const serialize = data => JSON.stringify(data, null, 2).replace(/"{(.+)}"/g, '$1')
/**
* Register a directory to be scanned for components and imported only when used.
*
* Requires Nuxt 2.13+
*/
export function addComponentsDir (dir: ComponentsDir) {
const nuxt = useNuxt()
ensureNuxtCompatibility({ nuxt: '>=2.13' }, nuxt)
nuxt.options.components = nuxt.options.components || []
nuxt.hook('components:dirs', (dirs) => { dirs.push(dir) })
}

const serialize = (data: any) => JSON.stringify(data, null, 2).replace(/"{(.+)}"/g, '$1')

const importName = (src: string) => `${camelCase(basename(src, extname(src))).replace(/[^a-zA-Z?\d\s:]/g, '')}_${hash(src)}`

Expand Down