From 5ed6fa76127ba8a2c9f65372d85f7e1bda82b907 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sun, 4 Apr 2021 14:28:31 +0200 Subject: [PATCH] fix(kit): properly resolve _nuxtConfigFiles --- packages/kit/src/config/load.ts | 25 +++++---------------- packages/kit/src/config/schema/_common.ts | 10 +-------- packages/kit/src/config/schema/_internal.ts | 2 ++ 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/packages/kit/src/config/load.ts b/packages/kit/src/config/load.ts index 21de2f3a1e..3aec30d2a4 100644 --- a/packages/kit/src/config/load.ts +++ b/packages/kit/src/config/load.ts @@ -1,8 +1,8 @@ import { resolve } from 'path' import defu from 'defu' -import jiti from 'jiti' import { applyDefaults } from 'untyped' import * as rc from 'rc9' +import { tryResolveModule, requireModule, scanRequireTree } from '../utils/cjs' import { NuxtOptions } from '../types/config' import nuxtConfigSchema from './schema' @@ -15,28 +15,15 @@ export interface LoadNuxtConfigOptions { export function loadNuxtConfig (opts: LoadNuxtConfigOptions): NuxtOptions { const rootDir = resolve(process.cwd(), opts.rootDir || '.') - const _require = jiti(rootDir) - - let configFile - try { - configFile = _require.resolve(resolve(rootDir, opts.configFile || 'nuxt.config')) - } catch (e) { - if (e.code !== 'MODULE_NOT_FOUND') { - throw (e) - } - // Skip if cannot resolve - configFile = undefined - } + const nuxtConfigFile = tryResolveModule(resolve(rootDir, opts.configFile || 'nuxt.config')) let nuxtConfig: any = {} - if (configFile) { - // clearRequireCache(configFile) TODO - nuxtConfig = _require(configFile) || {} - if (nuxtConfig.default) { - nuxtConfig = nuxtConfig.default - } + if (nuxtConfigFile) { + nuxtConfig = requireModule(nuxtConfigFile, { clearCache: true }) nuxtConfig = { ...nuxtConfig } + nuxtConfig._nuxtConfigFile = nuxtConfigFile + nuxtConfig._nuxtConfigFiles = Array.from(scanRequireTree(nuxtConfigFile)) } // Combine configs diff --git a/packages/kit/src/config/schema/_common.ts b/packages/kit/src/config/schema/_common.ts index 3be87049de..d31bd26bdc 100644 --- a/packages/kit/src/config/schema/_common.ts +++ b/packages/kit/src/config/schema/_common.ts @@ -88,14 +88,6 @@ export default { } }, - _nuxtConfigFile: { - $resolve: (val, get) => resolve(get('rootDir'), val || 'nuxt.config.js') - }, - - _nuxtConfigFiles: { - $resolve: (val, get) => [].concat(get('_nuxtConfigFile'), val).filter(Boolean) - }, - modulesDir: { $default: ['node_modules'], $resolve: (val, get) => [].concat( @@ -143,7 +135,7 @@ export default { }, watch: { - $resolve: (_val, get) => [].concat(get._nuxtConfigFiles).filter(Boolean) + $resolve: (val, get) => [].concat(val, get('_nuxtConfigFiles')).filter(Boolean) }, watchers: { diff --git a/packages/kit/src/config/schema/_internal.ts b/packages/kit/src/config/schema/_internal.ts index 22420b9a2c..ffe161a3a9 100644 --- a/packages/kit/src/config/schema/_internal.ts +++ b/packages/kit/src/config/schema/_internal.ts @@ -6,6 +6,8 @@ export default { _generate: false, _cli: false, _requiredModules: {}, + _nuxtConfigFile: undefined, + _nuxtConfigFiles: [], appDir: '', vite: false }