Skip to content

Commit

Permalink
refactor: remove builder coupling from server (#5157)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored and clarkdo committed Mar 8, 2019
1 parent 9e1ef88 commit 13cb0f7
Show file tree
Hide file tree
Showing 43 changed files with 210 additions and 158 deletions.
7 changes: 2 additions & 5 deletions packages/builder/src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,9 @@ export default class Builder {
this.template = this.nuxt.resolver.requireModule(this.template).template
}

// if(!this.options.dev) {
// TODO: enable again when unsafe concern resolved.(common/options.js:42)
// this.nuxt.hook('build:done', () => this.generateConfig())
// }

// Create a new bundle builder
this.bundleBuilder = this.getBundleBuilder(bundleBuilder)

this.ignore = new Ignore({
rootDir: this.options.srcDir
})
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ export default class NuxtCommand {

async getNuxt(options) {
const { Nuxt } = await imports.core()

const nuxt = new Nuxt(options)
await nuxt.ready()

return nuxt
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default {
}
},
async run(cmd) {
const config = await cmd.getNuxtConfig({ dev: false })
const config = await cmd.getNuxtConfig({ dev: false, server: false })
const nuxt = await cmd.getNuxt(config)

if (cmd.argv.lock) {
Expand Down
9 changes: 6 additions & 3 deletions packages/cli/src/commands/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ export default {
nuxt.hook('watch:restart', payload => this.onWatchRestart(payload, { nuxt, builder, cmd, argv }))
nuxt.hook('bundler:change', changedFileName => this.onBundlerChange(changedFileName))

// Start listening
await nuxt.server.listen()

// Create builder instance
const builder = await cmd.getBuilder(nuxt)

// Wait for nuxt to be ready
await nuxt.ready()

// Start listening
await nuxt.server.listen()

// Start Build
await builder.build()

Expand Down
45 changes: 30 additions & 15 deletions packages/core/src/nuxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export default class Nuxt extends Hookable {
// Create instance of core components
this.resolver = new Resolver(this)
this.moduleContainer = new ModuleContainer(this)
this.server = new Server(this)

// Deprecated hooks
this._deprecatedHooks = {
Expand All @@ -32,27 +31,33 @@ export default class Nuxt extends Hookable {
}

// Add Legacy aliases
defineAlias(this, this.server, ['renderRoute', 'renderAndGetWindow', 'listen'])
defineAlias(this, this.resolver, ['resolveAlias', 'resolvePath'])
this.renderer = this.server
this.render = this.server.app
this.showReady = () => { this.callHook('webpack:done') }

// Wait for Nuxt to be ready
this.initialized = false
this._ready = this.ready().catch((err) => {
consola.fatal(err)
})
// Init server
if (this.options.server !== false) {
this._initServer()
}
}

static get version() {
return (global.__NUXT && global.__NUXT.version) || `v${version}`
}

async ready() {
if (this._ready) {
return this._ready
ready() {
if (!this._ready) {
this._ready = this._init().catch((err) => {
consola.fatal(err)
})
}
return this._ready
}

async _init() {
if (this._initCalled) {
return this
}
this._initCalled = true

// Add hooks
if (isPlainObject(this.options.hooks)) {
Expand All @@ -65,16 +70,26 @@ export default class Nuxt extends Hookable {
await this.moduleContainer.ready()

// Await for server to be ready
await this.server.ready()

this.initialized = true
if (this.server) {
await this.server.ready()
}

// Call ready hook
await this.callHook('ready', this)

return this
}

_initServer() {
if (this.server) {
return
}
this.server = new Server(this)
this.renderer = this.server
this.render = this.server.app
defineAlias(this, this.server, ['renderRoute', 'renderAndGetWindow', 'listen'])
}

async close(callback) {
await this.callHook('close', this)

Expand Down
43 changes: 12 additions & 31 deletions packages/core/test/nuxt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,22 @@ import Resolver from '../src/resolver'
import { version } from '../package.json'

jest.mock('@nuxt/utils')

jest.mock('@nuxt/server')

jest.mock('@nuxt/config', () => ({
getNuxtConfig: jest.fn(() => ({}))
}))
jest.mock('@nuxt/server')

describe('core: nuxt', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.spyOn(Nuxt.prototype, 'ready').mockImplementation(() => Promise.resolve())
})

afterEach(() => {
if (Nuxt.prototype.ready.mockRestore) {
Nuxt.prototype.ready.mockRestore()
}
})

test('should construct nuxt with options', () => {
test('should construct nuxt with options', async () => {
const options = {}
const nuxt = new Nuxt(options)
await nuxt.ready()

expect(nuxt).toBeInstanceOf(Hookable)
expect(getNuxtConfig).toBeCalledTimes(1)
Expand All @@ -46,15 +42,14 @@ describe('core: nuxt', () => {
})

expect(defineAlias).toBeCalledTimes(2)
expect(defineAlias).nthCalledWith(1, nuxt, nuxt.server, ['renderRoute', 'renderAndGetWindow', 'listen'])
expect(defineAlias).nthCalledWith(2, nuxt, nuxt.resolver, ['resolveAlias', 'resolvePath'])
expect(defineAlias).nthCalledWith(1, nuxt, nuxt.resolver, ['resolveAlias', 'resolvePath'])
expect(defineAlias).nthCalledWith(2, nuxt, nuxt.server, ['renderRoute', 'renderAndGetWindow', 'listen'])

expect(nuxt.renderer).toBe(nuxt.server)
expect(nuxt.render).toBe(nuxt.server.app)
expect(nuxt.showReady).toBeInstanceOf(Function)
expect(nuxt.initialized).toEqual(false)

expect(nuxt.ready).toBeCalledTimes(1)
expect(nuxt._ready).toBeInstanceOf(Promise)
})

// TODO: Remove in next major release
Expand All @@ -70,10 +65,9 @@ describe('core: nuxt', () => {

test('should display fatal message if ready failed', async () => {
const err = new Error('nuxt ready failed')
Nuxt.prototype.ready.mockImplementation(() => Promise.reject(err))
const nuxt = new Nuxt()

await nuxt._ready
nuxt._init = () => Promise.reject(err)
await nuxt.ready()

expect(consola.fatal).toBeCalledTimes(1)
expect(consola.fatal).toBeCalledWith(err)
Expand All @@ -95,8 +89,6 @@ describe('core: nuxt', () => {

test('should call module/server ready in nuxt.ready', async () => {
const nuxt = new Nuxt()
delete nuxt._ready
Nuxt.prototype.ready.mockRestore()

nuxt.callHook = jest.fn()
nuxt.server = { ready: jest.fn() }
Expand All @@ -107,29 +99,22 @@ describe('core: nuxt', () => {
expect(result).toBe(nuxt)
expect(nuxt.moduleContainer.ready).toBeCalledTimes(1)
expect(nuxt.server.ready).toBeCalledTimes(1)
expect(nuxt.initialized).toEqual(true)
expect(nuxt._initCalled).toEqual(true)
expect(nuxt.callHook).toBeCalledTimes(1)
expect(nuxt.callHook).toBeCalledWith('ready', nuxt)
})

test('should ignore ready when _ready exists', async () => {
const nuxt = new Nuxt()
Nuxt.prototype.ready.mockRestore()
const _ready = nuxt._ready = jest.fn()
nuxt.server = { ready: jest.fn() }

const result = await nuxt.ready()

expect(result).toBe(_ready)
expect(nuxt.server.ready).not.toBeCalled()
})

test('should add object hooks', async () => {
const hooks = {}
getNuxtConfig.mockReturnValueOnce({ hooks })
const nuxt = new Nuxt()
delete nuxt._ready
Nuxt.prototype.ready.mockRestore()

nuxt.addHooks = jest.fn()
nuxt.server = { ready: jest.fn() }
Expand All @@ -145,8 +130,6 @@ describe('core: nuxt', () => {
const hooks = jest.fn()
getNuxtConfig.mockReturnValueOnce({ hooks })
const nuxt = new Nuxt()
delete nuxt._ready
Nuxt.prototype.ready.mockRestore()

nuxt.addHooks = jest.fn()
nuxt.server = { ready: jest.fn() }
Expand Down Expand Up @@ -174,8 +157,6 @@ describe('core: nuxt', () => {

test('should ignore non-function callback in close', async () => {
const nuxt = new Nuxt()
delete nuxt._ready
Nuxt.prototype.ready.mockRestore()

nuxt.callHook = jest.fn()
nuxt.server = { ready: jest.fn() }
Expand All @@ -186,7 +167,7 @@ describe('core: nuxt', () => {
expect(result).toBe(nuxt)
expect(nuxt.moduleContainer.ready).toBeCalledTimes(1)
expect(nuxt.server.ready).toBeCalledTimes(1)
expect(nuxt.initialized).toEqual(true)
expect(nuxt._initCalled).toEqual(true)
expect(nuxt.callHook).toBeCalledTimes(1)
expect(nuxt.callHook).toBeCalledWith('ready', nuxt)
})
Expand Down

0 comments on commit 13cb0f7

Please sign in to comment.