Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
| import path from 'path' | |
| import consola from 'consola' | |
| import defaultsDeep from 'lodash/defaultsDeep' | |
| import { defaultNuxtConfigFile, getDefaultNuxtConfig } from '@nuxt/config' | |
| import { clearRequireCache, scanRequireTree } from '@nuxt/utils' | |
| import esm from 'esm' | |
| export async function loadNuxtConfig(argv) { | |
| const rootDir = path.resolve(argv._[0] || '.') | |
| let nuxtConfigFile | |
| let options = {} | |
| try { | |
| nuxtConfigFile = require.resolve(path.resolve(rootDir, argv['config-file'])) | |
| } catch (e) { | |
| if (e.code !== 'MODULE_NOT_FOUND') { | |
| throw (e) | |
| } else if (argv['config-file'] !== defaultNuxtConfigFile) { | |
| consola.fatal('Could not load config file: ' + argv['config-file']) | |
| } | |
| } | |
| if (nuxtConfigFile) { | |
| if (nuxtConfigFile.endsWith('.ts')) { | |
| options = require(nuxtConfigFile) || {} | |
| } else { | |
| clearRequireCache(nuxtConfigFile) | |
| options = esm(module, { cache: false, cjs: { cache: false } })(nuxtConfigFile) || {} | |
| } | |
| if (options.default) { | |
| options = options.default | |
| } | |
| if (typeof options === 'function') { | |
| try { | |
| options = await options() | |
| if (options.default) { | |
| options = options.default | |
| } | |
| } catch (error) { | |
| consola.error(error) | |
| consola.fatal('Error while fetching async configuration') | |
| } | |
| } | |
| // Keep _nuxtConfigFile for watching | |
| options._nuxtConfigFile = nuxtConfigFile | |
| // Keep all related files for watching | |
| options._nuxtConfigFiles = Array.from(scanRequireTree(nuxtConfigFile)) | |
| } | |
| if (typeof options.rootDir !== 'string') { | |
| options.rootDir = rootDir | |
| } | |
| // Nuxt Mode | |
| options.mode = | |
| (argv.spa && 'spa') || (argv.universal && 'universal') || options.mode | |
| // Server options | |
| options.server = defaultsDeep({ | |
| port: argv.port || undefined, | |
| host: argv.hostname || undefined, | |
| socket: argv['unix-socket'] || undefined | |
| }, options.server || {}, getDefaultNuxtConfig().server) | |
| return options | |
| } |