2017-01-11 19:14:59 +00:00
|
|
|
import _ from 'lodash'
|
2017-02-21 17:10:19 +00:00
|
|
|
import compression from 'compression'
|
2017-01-11 19:14:59 +00:00
|
|
|
import fs from 'fs-extra'
|
|
|
|
import pify from 'pify'
|
|
|
|
import serveStatic from 'serve-static'
|
|
|
|
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
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
this.componentTasks()
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
componentTasks () {
|
|
|
|
// TODO: This task should move into their own components instead
|
|
|
|
|
|
|
|
// Error template
|
|
|
|
this.errorTemplate = _.template(fs.readFileSync(resolve(__dirname, 'views', 'error.html'), 'utf8'), {
|
|
|
|
interpolate: /{{([\s\S]+?)}}/g
|
|
|
|
})
|
|
|
|
|
2016-11-18 08:17:39 +00:00
|
|
|
// If store defined, update store options to true
|
2017-06-11 14:17:36 +00:00
|
|
|
if (fs.existsSync(join(this.options.srcDir, 'store'))) {
|
2016-11-18 08:17:39 +00:00
|
|
|
this.options.store = true
|
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-02-22 18:19:49 +00:00
|
|
|
// If app.html is defined, set the template path to the user template
|
|
|
|
this.options.appTemplatePath = resolve(__dirname, 'views/app.template.html')
|
2017-06-11 14:17:36 +00:00
|
|
|
if (fs.existsSync(join(this.options.srcDir, 'app.html'))) {
|
|
|
|
this.options.appTemplatePath = join(this.options.srcDir, 'app.html')
|
2017-02-22 18:19:49 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2016-11-10 01:19:47 +00:00
|
|
|
// For serving static/ files to /
|
2017-06-11 14:17:36 +00:00
|
|
|
this.serveStatic = pify(serveStatic(resolve(this.options.srcDir, 'static'), this.options.render.static))
|
2017-02-16 17:16:00 +00:00
|
|
|
// For serving .nuxt/dist/ files (only when build.publicPath is not an URL)
|
2017-06-11 14:17:36 +00:00
|
|
|
this.serveStaticNuxt = pify(serveStatic(resolve(this.options.buildDir, 'dist'), {
|
|
|
|
maxAge: (this.options.dev ? 0 : '1y') // 1 year in production
|
2017-02-21 17:10:19 +00:00
|
|
|
}))
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-05-21 12:06:01 +00:00
|
|
|
// gzip middleware for production
|
2017-06-11 14:17:36 +00:00
|
|
|
if (!this.options.dev && this.options.render.gzip) {
|
2017-05-21 12:06:01 +00:00
|
|
|
this.gzipMiddleware = pify(compression(this.options.render.gzip))
|
2017-02-21 17:10:19 +00:00
|
|
|
}
|
2017-06-02 15:58:53 +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
|
|
|
}
|
|
|
|
|
2016-11-10 02:38:11 +00:00
|
|
|
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
|