2017-01-11 19:14:59 +00:00
|
|
|
import _ from 'lodash'
|
|
|
|
import fs from 'fs-extra'
|
|
|
|
import { resolve, join } from 'path'
|
2017-06-13 17:58:04 +00:00
|
|
|
import Tapable from 'tappable'
|
2017-06-12 20:32:34 +00:00
|
|
|
import * as Utils from './utils'
|
2017-06-12 20:16:27 +00:00
|
|
|
import Builder from './builder'
|
|
|
|
import Renderer from './renderer'
|
|
|
|
import Generator from './generator'
|
|
|
|
import ModuleContainer from './module-container'
|
|
|
|
import Server from './server'
|
|
|
|
import Defaults from './defaults'
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
export default class Nuxt extends Tapable {
|
2016-12-09 18:40:59 +00:00
|
|
|
constructor (options = {}) {
|
2017-06-11 14:17:36 +00:00
|
|
|
super()
|
|
|
|
|
|
|
|
// Normalize options
|
|
|
|
if (options.loading === true) {
|
|
|
|
delete options.loading
|
|
|
|
}
|
|
|
|
if (options.router && typeof options.router.middleware === 'string') {
|
|
|
|
options.router.middleware = [options.router.middleware]
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
2017-05-17 09:27:05 +00:00
|
|
|
if (options.router && typeof options.router.base === 'string') {
|
|
|
|
this._routerBaseSpecified = true
|
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
if (typeof options.transition === 'string') {
|
|
|
|
options.transition = { name: options.transition }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply defaults
|
2017-06-12 20:16:27 +00:00
|
|
|
this.options = _.defaultsDeep(options, Nuxt.Defaults)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-06-13 19:59:26 +00:00
|
|
|
// Resolve dirs
|
|
|
|
this.options.rootDir = (typeof options.rootDir === 'string' && options.rootDir ? options.rootDir : process.cwd())
|
|
|
|
this.options.srcDir = (typeof options.srcDir === 'string' && options.srcDir ? resolve(options.rootDir, options.srcDir) : this.options.rootDir)
|
|
|
|
this.options.buildDir = join(this.options.rootDir, options.buildDir)
|
|
|
|
|
2017-06-13 19:54:23 +00:00
|
|
|
// If store defined, update store options to true
|
|
|
|
if (fs.existsSync(join(this.options.srcDir, 'store'))) {
|
|
|
|
this.options.store = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If app.html is defined, set the template path to the user template
|
|
|
|
this.options.appTemplatePath = resolve(__dirname, 'views/app.template.html')
|
|
|
|
if (fs.existsSync(join(this.options.srcDir, 'app.html'))) {
|
|
|
|
this.options.appTemplatePath = join(this.options.srcDir, 'app.html')
|
|
|
|
}
|
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
// Create instance of core components
|
2017-06-13 17:58:04 +00:00
|
|
|
this.moduleContainer = new Nuxt.ModuleContainer(this)
|
2017-06-12 20:16:27 +00:00
|
|
|
this.builder = new Nuxt.Builder(this)
|
|
|
|
this.renderer = new Nuxt.Renderer(this)
|
|
|
|
this.generator = new Nuxt.Generator(this)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
|
|
|
// Backward compatibility
|
|
|
|
this.render = this.renderer.render.bind(this.renderer)
|
|
|
|
this.renderRoute = this.renderer.renderRoute.bind(this.renderer)
|
|
|
|
this.renderAndGetWindow = this.renderer.renderAndGetWindow.bind(this.renderer)
|
2017-06-13 17:58:04 +00:00
|
|
|
this.build = this.builder.build.bind(this.builder)
|
2017-06-12 20:16:27 +00:00
|
|
|
this.generate = this.generator.generate.bind(this.generator)
|
2017-06-11 14:17:36 +00:00
|
|
|
this.dir = options.rootDir
|
|
|
|
this.srcDir = options.srcDir
|
|
|
|
this.buildDir = options.buildDir
|
2017-06-12 20:16:27 +00:00
|
|
|
this.Server = Nuxt.Server
|
|
|
|
this.Utils = Nuxt.Utils
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-06-11 14:57:39 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2017-06-13 17:58:04 +00:00
|
|
|
this._init = this.init().catch(console.error)
|
|
|
|
this.initialized = false
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 17:58:04 +00:00
|
|
|
async init () {
|
|
|
|
if (this._init) {
|
|
|
|
return this._init
|
2017-06-04 12:08:36 +00:00
|
|
|
}
|
2017-06-13 17:58:04 +00:00
|
|
|
// Wait for all components to be ready
|
|
|
|
await this.applyPluginsAsync('beforeInit')
|
|
|
|
await this.applyPluginsAsync('init')
|
2017-06-13 19:31:30 +00:00
|
|
|
this.applyPluginsAsync('afterInit')
|
2017-06-13 17:58:04 +00:00
|
|
|
this.initialized = true
|
2017-06-04 12:08:36 +00:00
|
|
|
return this
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 19:54:23 +00:00
|
|
|
async close (callback) {
|
2016-11-07 01:34:58 +00:00
|
|
|
let promises = []
|
2016-12-21 14:27:30 +00:00
|
|
|
/* istanbul ignore if */
|
2016-11-07 01:34:58 +00:00
|
|
|
if (this.webpackDevMiddleware) {
|
|
|
|
const p = new Promise((resolve, reject) => {
|
|
|
|
this.webpackDevMiddleware.close(() => resolve())
|
|
|
|
})
|
|
|
|
promises.push(p)
|
|
|
|
}
|
2016-12-21 14:27:30 +00:00
|
|
|
/* istanbul ignore if */
|
2016-11-07 01:34:58 +00:00
|
|
|
if (this.webpackServerWatcher) {
|
|
|
|
const p = new Promise((resolve, reject) => {
|
|
|
|
this.webpackServerWatcher.close(() => resolve())
|
|
|
|
})
|
|
|
|
promises.push(p)
|
|
|
|
}
|
2016-12-21 14:27:30 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-21 17:18:48 +00:00
|
|
|
if (this.filesWatcher) {
|
|
|
|
this.filesWatcher.close()
|
2016-11-10 02:38:11 +00:00
|
|
|
}
|
2017-05-12 08:27:59 +00:00
|
|
|
/* istanbul ignore if */
|
|
|
|
if (this.customFilesWatcher) {
|
|
|
|
this.customFilesWatcher.close()
|
|
|
|
}
|
2017-05-18 08:45:15 +00:00
|
|
|
return Promise.all(promises).then(() => {
|
2016-11-07 01:34:58 +00:00
|
|
|
if (typeof callback === 'function') callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-06-12 20:16:27 +00:00
|
|
|
|
|
|
|
// Add core components to Nuxt class
|
|
|
|
Nuxt.Defaults = Defaults
|
|
|
|
Nuxt.Utils = Utils
|
|
|
|
Nuxt.Renderer = Renderer
|
|
|
|
Nuxt.Builder = Builder
|
|
|
|
Nuxt.ModuleContainer = ModuleContainer
|
|
|
|
Nuxt.Server = Server
|
|
|
|
Nuxt.Generator = Generator
|