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

View File

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

View File

@ -1,6 +1,13 @@
import test from 'ava' import test from 'ava'
const utils = require('../lib/utils') import ansiHTML from 'ansi-html'
const ansiHTML = require('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 => { test('encodeHtml', t => {
const html = '<h1>Hello</h1>' const html = '<h1>Hello</h1>'