mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
feat(cli): pass context when nuxt.config exports a function (#6855)
This commit is contained in:
parent
666f17e9ac
commit
75e00fe69f
@ -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)) {
|
||||
|
@ -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
|
||||
}
|
||||
|
3
packages/cli/test/fixtures/nuxt.fn-config.js
vendored
Normal file
3
packages/cli/test/fixtures/nuxt.fn-config.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export default (context) => {
|
||||
return { context }
|
||||
}
|
@ -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 () => {
|
||||
|
@ -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],
|
||||
|
Loading…
Reference in New Issue
Block a user