Nuxt/lib/nuxt.js
Pooya Parsa 349f6e6219 feat(http2): add render.http2.push option
This option disables http2 push headers by default as is currently inconsistent with different browser and webservers
2017-06-05 13:19:27 +04:30

182 lines
5.4 KiB
JavaScript

'use strict'
import _ from 'lodash'
import compression from 'compression'
import fs from 'fs-extra'
import pify from 'pify'
import Server from './server'
import Module from './module'
import * as build from './build'
import * as render from './render'
import generate from './generate'
import serveStatic from 'serve-static'
import { resolve, join } from 'path'
import * as utils from './utils'
class Nuxt {
constructor (options = {}) {
const defaults = {
dev: (process.env.NODE_ENV !== 'production'),
buildDir: '.nuxt',
env: {},
head: {
meta: [],
link: [],
style: [],
script: []
},
plugins: [],
css: [],
modules: [],
layouts: {},
serverMiddleware: [],
ErrorPage: null,
loading: {
color: 'black',
failedColor: 'red',
height: '2px',
duration: 5000
},
transition: {
name: 'page',
mode: 'out-in'
},
router: {
mode: 'history',
base: '/',
middleware: [],
linkActiveClass: 'nuxt-link-active',
linkExactActiveClass: 'nuxt-link-exact-active',
extendRoutes: null,
scrollBehavior: null
},
render: {
http2: {
push: false
},
static: {},
gzip: {
threshold: 0
},
etag: {
weak: true // Faster for responses > 5KB
}
},
watchers: {
webpack: {},
chokidar: {}
}
}
// Sanitization
if (options.loading === true) delete options.loading
if (options.router && typeof options.router.middleware === 'string') options.router.middleware = [options.router.middleware]
if (options.router && typeof options.router.base === 'string') {
this._routerBaseSpecified = true
}
if (typeof options.transition === 'string') options.transition = {name: options.transition}
this.options = _.defaultsDeep(options, defaults)
// Ready variable
this._ready = false
// Env variables
this.dev = this.options.dev
// Explicit srcDir, rootDir and buildDir
this.dir = (typeof options.rootDir === 'string' && options.rootDir ? options.rootDir : process.cwd())
this.srcDir = (typeof options.srcDir === 'string' && options.srcDir ? resolve(this.dir, options.srcDir) : this.dir)
this.buildDir = join(this.dir, options.buildDir)
options.rootDir = this.dir
options.srcDir = this.srcDir
options.buildDir = this.buildDir
// If store defined, update store options to true
if (fs.existsSync(join(this.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.srcDir, 'app.html'))) {
this.options.appTemplatePath = join(this.srcDir, 'app.html')
}
// renderer used by Vue.js (via createBundleRenderer)
this.renderer = null
// For serving static/ files to /
this.serveStatic = pify(serveStatic(resolve(this.srcDir, 'static'), this.options.render.static))
// For serving .nuxt/dist/ files (only when build.publicPath is not an URL)
this.serveStaticNuxt = pify(serveStatic(resolve(this.buildDir, 'dist'), {
maxAge: (this.dev ? 0 : '1y') // 1 year in production
}))
// gzip middleware for production
if (!this.dev && this.options.render.gzip) {
this.gzipMiddleware = pify(compression(this.options.render.gzip))
}
// Add this.Server Class
this.Server = Server
// Add this.build
build.options.call(this) // Add build options
this.build = build.build.bind(this)
// Error template
this.errorTemplate = _.template(fs.readFileSync(resolve(__dirname, 'views', 'error.html'), 'utf8'), {
interpolate: /{{([\s\S]+?)}}/g
})
// Add this.render and this.renderRoute
this.render = render.render.bind(this)
this.renderRoute = render.renderRoute.bind(this)
this.renderAndGetWindow = render.renderAndGetWindow.bind(this)
// Add this.generate
this.generate = generate.bind(this)
// Add this.utils (tests purpose)
this.utils = utils
// Add module integration
this.module = new Module(this)
// Init nuxt.js
this._ready = this.ready()
// Return nuxt.js instance
return this
}
async ready () {
if (this._ready) {
await this._ready
return this
}
// Init modules
await this.module.ready()
// Launch build in development but don't wait for it to be finished
if (this.dev) {
this.build()
} else {
build.production.call(this)
}
return this
}
close (callback) {
let promises = []
/* istanbul ignore if */
if (this.webpackDevMiddleware) {
const p = new Promise((resolve, reject) => {
this.webpackDevMiddleware.close(() => resolve())
})
promises.push(p)
}
/* istanbul ignore if */
if (this.webpackServerWatcher) {
const p = new Promise((resolve, reject) => {
this.webpackServerWatcher.close(() => resolve())
})
promises.push(p)
}
/* istanbul ignore if */
if (this.filesWatcher) {
this.filesWatcher.close()
}
/* istanbul ignore if */
if (this.customFilesWatcher) {
this.customFilesWatcher.close()
}
return Promise.all(promises).then(() => {
if (typeof callback === 'function') callback()
})
}
}
export default Nuxt