mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 23:32:38 +00:00
fix(components): don't ascend directories (#251)
This commit is contained in:
parent
15e65613b9
commit
01b2c2c972
@ -1,12 +1,11 @@
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { defineNuxtModule, resolveAlias } from '@nuxt/kit'
|
import { defineNuxtModule, resolveAlias } from '@nuxt/kit'
|
||||||
import { resolve, dirname } from 'upath'
|
import { resolve } from 'upath'
|
||||||
import { scanComponents } from './scan'
|
import { scanComponents } from './scan'
|
||||||
import type { ComponentsDir } from './types'
|
import type { ComponentsDir } from './types'
|
||||||
|
|
||||||
const isPureObjectOrString = (val: any) => (!Array.isArray(val) && typeof val === 'object') || typeof val === 'string'
|
const isPureObjectOrString = (val: any) => (!Array.isArray(val) && typeof val === 'object') || typeof val === 'string'
|
||||||
const isDirectory = (p: string) => { try { return fs.statSync(p).isDirectory() } catch (_e) { return false } }
|
const isDirectory = (p: string) => { try { return fs.statSync(p).isDirectory() } catch (_e) { return false } }
|
||||||
const getDir = (p: string) => isDirectory(p) ? p : dirname(p)
|
|
||||||
|
|
||||||
export default defineNuxtModule({
|
export default defineNuxtModule({
|
||||||
name: 'components',
|
name: 'components',
|
||||||
@ -22,21 +21,22 @@ export default defineNuxtModule({
|
|||||||
|
|
||||||
componentDirs = options.dirs.filter(isPureObjectOrString).map((dir) => {
|
componentDirs = options.dirs.filter(isPureObjectOrString).map((dir) => {
|
||||||
const dirOptions: ComponentsDir = typeof dir === 'object' ? dir : { path: dir }
|
const dirOptions: ComponentsDir = typeof dir === 'object' ? dir : { path: dir }
|
||||||
const dirPath = getDir(resolveAlias(dirOptions.path, nuxt.options.alias))
|
const dirPath = resolveAlias(dirOptions.path, nuxt.options.alias)
|
||||||
const transpile = typeof dirOptions.transpile === 'boolean' ? dirOptions.transpile : 'auto'
|
const transpile = typeof dirOptions.transpile === 'boolean' ? dirOptions.transpile : 'auto'
|
||||||
const extensions = dirOptions.extensions || ['vue'] // TODO: nuxt extensions and strip leading dot
|
const extensions = dirOptions.extensions || ['vue'] // TODO: nuxt extensions and strip leading dot
|
||||||
|
|
||||||
dirOptions.level = Number(dirOptions.level || 0)
|
dirOptions.level = Number(dirOptions.level || 0)
|
||||||
|
|
||||||
const enabled = fs.existsSync(dirPath)
|
const present = isDirectory(dirPath)
|
||||||
if (!enabled && dirOptions.path !== '~/components') {
|
if (!present && dirOptions.path !== '~/components') {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.warn('Components directory not found: `' + dirPath + '`')
|
console.warn('Components directory not found: `' + dirPath + '`')
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...dirOptions,
|
...dirOptions,
|
||||||
enabled,
|
// TODO: https://github.com/nuxt/framework/pull/251
|
||||||
|
enabled: true,
|
||||||
path: dirPath,
|
path: dirPath,
|
||||||
extensions,
|
extensions,
|
||||||
pattern: dirOptions.pattern || `**/*.{${extensions.join(',')},}`,
|
pattern: dirOptions.pattern || `**/*.{${extensions.join(',')},}`,
|
||||||
|
Loading…
Reference in New Issue
Block a user