diff --git a/packages/cli/src/command.js b/packages/cli/src/command.js index cb24bb5b2c..03a50c8a54 100644 --- a/packages/cli/src/command.js +++ b/packages/cli/src/command.js @@ -104,7 +104,12 @@ export default class NuxtCommand extends Hookable { // Flag to indicate nuxt is running with CLI (not programmatic) extraOptions._cli = true - const config = await loadNuxtConfig(this.argv) + const context = { + command: this.cmd.name, + dev: !!extraOptions.dev + } + + const config = await loadNuxtConfig(this.argv, context) const options = Object.assign(config, extraOptions) for (const name of Object.keys(this.cmd.options)) { diff --git a/packages/cli/src/utils/config.js b/packages/cli/src/utils/config.js index af676ffa0d..227e366260 100644 --- a/packages/cli/src/utils/config.js +++ b/packages/cli/src/utils/config.js @@ -5,7 +5,7 @@ import { defaultNuxtConfigFile, getDefaultNuxtConfig } from '@nuxt/config' import { clearRequireCache, scanRequireTree } from '@nuxt/utils' import esm from 'esm' -export async function loadNuxtConfig (argv) { +export async function loadNuxtConfig (argv, context) { const rootDir = path.resolve(argv._[0] || '.') let nuxtConfigFile let options = {} @@ -32,7 +32,7 @@ export async function loadNuxtConfig (argv) { if (typeof options === 'function') { try { - options = await options() + options = await options(context) if (options.default) { options = options.default } diff --git a/packages/cli/test/fixtures/nuxt.fn-config.js b/packages/cli/test/fixtures/nuxt.fn-config.js new file mode 100644 index 0000000000..3f8e9810de --- /dev/null +++ b/packages/cli/test/fixtures/nuxt.fn-config.js @@ -0,0 +1,3 @@ +export default (context) => { + return { context } +} diff --git a/packages/cli/test/unit/command.test.js b/packages/cli/test/unit/command.test.js index b3bf84b17d..fc6051f1f1 100644 --- a/packages/cli/test/unit/command.test.js +++ b/packages/cli/test/unit/command.test.js @@ -1,6 +1,7 @@ import Command from '../../src/command' import { common, server } from '../../src/options' import * as utils from '../../src/utils/' +import * as config from '../../src/utils/config' import * as constants from '../../src/utils/constants' import { consola } from '../utils' @@ -60,13 +61,19 @@ describe('cli/command', () => { }) test('returns nuxt config', async () => { - const cmd = new Command({ options: allOptions }, ['-c', 'test-file', '-a', '-p', '3001', '-q', '-H']) + const loadConfigSpy = jest.spyOn(config, 'loadNuxtConfig') + + const cmd = new Command({ name: 'test', options: allOptions }, ['-c', 'test-file', '-a', '-p', '3001', '-q', '-H']) const options = await cmd.getNuxtConfig({ testOption: true }) expect(options.testOption).toBe(true) expect(options.server.port).toBe(3001) expect(consola.fatal).toHaveBeenCalledWith('Provided hostname argument has no value') // hostname check + expect(loadConfigSpy).toHaveBeenCalledTimes(1) + expect(loadConfigSpy).toHaveBeenCalledWith(expect.any(Object), { command: 'test', dev: false }) + + loadConfigSpy.mockRestore() }) test('returns Nuxt instance', async () => { diff --git a/packages/cli/test/unit/utils.test.js b/packages/cli/test/unit/utils.test.js index 2de7ce4925..b30ce9ccbe 100644 --- a/packages/cli/test/unit/utils.test.js +++ b/packages/cli/test/unit/utils.test.js @@ -76,6 +76,18 @@ describe('cli/utils', () => { expect(options.server.socket).toBe('/var/run/async.sock') }) + test('loadNuxtConfig: passes context to config fn', async () => { + const argv = { + _: [__dirname], + 'config-file': '../fixtures/nuxt.fn-config.js' + } + + const context = { command: 'test', dev: true } + const options = await loadNuxtConfig(argv, context) + expect(options.context.command).toBe('test') + expect(options.context.dev).toBe(true) + }) + test('loadNuxtConfig: async config-file with error', async () => { const argv = { _: [__dirname],