Add utils in next instance

This commit is contained in:
Sébastien Chopin 2017-01-11 22:51:52 +01:00
parent ac9412b908
commit 8f29fe6a54
3 changed files with 18 additions and 6 deletions

View File

@ -12,8 +12,8 @@ import * as render from './render'
import generate from './generate'
import serveStatic from 'serve-static'
import { resolve, join } from 'path'
import { encodeHtml, setAnsiColors } from './utils'
setAnsiColors(ansiHTML)
import * as utils from './utils'
utils.setAnsiColors(ansiHTML)
class Nuxt {
@ -61,7 +61,10 @@ class Nuxt {
imports: { serialize }
})
this.errorTemplate = _.template(fs.readFileSync(resolve(__dirname, 'views', 'error.html'), 'utf8'), {
imports: { ansiHTML, encodeHtml }
imports: {
ansiHTML,
encodeHtml: utils.encodeHtml
}
})
// renderer used by Vue.js (via createBundleRenderer)
this.renderer = null
@ -81,6 +84,8 @@ class Nuxt {
this.renderAndGetWindow = render.renderAndGetWindow.bind(this)
// Add this.generate
this.generate = generate.bind(this)
// Add this.utils (tests purpose)
this.utils = utils
return this
}

View File

@ -1,7 +1,7 @@
'use strict'
export function encodeHtml (str) {
str.replace(/</g, '&lt;').replace(/>/g, '&gt;')
return str.replace(/</g, '&lt;').replace(/>/g, '&gt;')
}
export function getContext (req, res) {

View File

@ -1,6 +1,13 @@
import test from 'ava'
const utils = require('../lib/utils')
const ansiHTML = require('ansi-html')
import ansiHTML from 'ansi-html'
let utils
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
let nuxt = new Nuxt({ dev: false })
utils = nuxt.utils
})
test('encodeHtml', t => {
const html = '<h1>Hello</h1>'