refactor renderer init into it's component

This commit is contained in:
Pooya Parsa 2017-06-14 00:24:23 +04:30
parent 1d95e38f9c
commit 277aa7e6fa
2 changed files with 38 additions and 38 deletions

View File

@ -1,8 +1,5 @@
import _ from 'lodash'
import compression from 'compression'
import fs from 'fs-extra'
import pify from 'pify'
import serveStatic from 'serve-static'
import { resolve, join } from 'path'
import Tapable from 'tappable'
import * as Utils from './utils'
@ -34,13 +31,22 @@ export default class Nuxt extends Tapable {
// Apply defaults
this.options = _.defaultsDeep(options, Nuxt.Defaults)
// 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')
}
// 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
this.moduleContainer = new Nuxt.ModuleContainer(this)
this.builder = new Nuxt.Builder(this)
@ -64,38 +70,6 @@ export default class Nuxt extends Tapable {
this.initialized = false
}
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
})
// 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')
}
// For serving static/ files to /
this.serveStatic = pify(serveStatic(resolve(this.options.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.options.buildDir, 'dist'), {
maxAge: (this.options.dev ? 0 : '1y') // 1 year in production
}))
// gzip middleware for production
if (!this.options.dev && this.options.render.gzip) {
this.gzipMiddleware = pify(compression(this.options.render.gzip))
}
}
async init () {
if (this._init) {
return this._init
@ -108,7 +82,7 @@ export default class Nuxt extends Tapable {
return this
}
close (callback) {
async close (callback) {
let promises = []
/* istanbul ignore if */
if (this.webpackDevMiddleware) {

View File

@ -3,6 +3,12 @@ import serialize from 'serialize-javascript'
import generateETag from 'etag'
import fresh from 'fresh'
import Tapable from 'tappable'
import pify from 'pify'
import serveStatic from 'serve-static'
import compression from 'compression'
import _ from 'lodash'
import { resolve } from 'path'
import {readFileSync} from 'fs'
import { getContext, setAnsiColors, encodeHtml } from './utils'
const debug = require('debug')('nuxt:render')
@ -16,6 +22,26 @@ export default class Renderer extends Tapable {
super()
this.nuxt = nuxt
this.options = nuxt.options
this.nuxt.plugin('init', () => {
// For serving static/ files to /
this.serveStatic = pify(serveStatic(resolve(this.options.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.options.buildDir, 'dist'), {
maxAge: (this.options.dev ? 0 : '1y') // 1 year in production
}))
// gzip middleware for production
if (!this.options.dev && this.options.render.gzip) {
this.gzipMiddleware = pify(compression(this.options.render.gzip))
}
// Error template
this.errorTemplate = _.template(readFileSync(resolve(__dirname, 'views', 'error.html'), 'utf8'), {
interpolate: /{{([\s\S]+?)}}/g
})
})
}
async render (req, res) {