Nuxt/lib/nuxt.js

136 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-01-11 19:14:59 +00:00
import _ from 'lodash'
import compression from 'compression'
2017-01-11 19:14:59 +00:00
import fs from 'fs-extra'
import pify from 'pify'
import Server from './server'
2017-06-11 14:17:36 +00:00
import ModuleContainer from './module-container'
import Builder from './builder'
import Renderer from './renderer'
import Generate from './generate'
2017-01-11 19:14:59 +00:00
import serveStatic from 'serve-static'
import { resolve, join } from 'path'
2017-06-11 14:17:36 +00:00
import defaults from './defaults'
import Tapable from 'tapable'
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
2016-11-07 01:34:58 +00:00
this.options = _.defaultsDeep(options, 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.Server = Server
this.componentTasks()
// Create instance of core components
this.builder = new Builder(this)
this.renderer = new Renderer(this)
this.generate = new Generate(this)
this.moduleContainer = new ModuleContainer(this)
// 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)
this.build = this.ready.bind(this)
this.dir = options.rootDir
this.srcDir = options.srcDir
this.buildDir = options.buildDir
// Wait for all core components be ready
2017-06-11 14:57:39 +00:00
// eslint-disable-next-line no-console
2017-06-11 14:17:36 +00:00
this._ready = this.ready().catch(console.error)
}
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-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))
}
}
async ready () {
if (this._ready) {
2017-06-11 14:17:36 +00:00
return this._ready
}
2017-06-11 14:17:36 +00:00
await this.moduleContainer.ready()
await this.builder.ready()
2017-06-11 14:57:39 +00:00
// eslint-disable-next-line no-console
console.log('[nuxt] ready')
return this
2016-11-07 01:34:58 +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()
}
/* 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()
})
}
}