Merge remote-tracking branch 'nuxt/master'

This commit is contained in:
yuchonghua 2017-01-20 14:40:15 +08:00
commit 634ff204b6
13 changed files with 53 additions and 13 deletions

View File

@ -1,5 +1,14 @@
module.exports = { module.exports = {
build: { build: {
vendor: ['socket.io-client'] vendor: ['socket.io-client']
},
head: {
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' }
]
},
env: {
HOST_URL: process.env.HOST_URL || 'http://localhost:3000'
} }
} }

View File

@ -3,7 +3,7 @@
<ul class="pages"> <ul class="pages">
<li class="chat page"> <li class="chat page">
<div class="chatArea"> <div class="chatArea">
<ul class="messages"> <ul class="messages" ref="messages">
<li class="message" v-for="message in messages"><i :title="message.date">{{ message.date.split('T')[1].slice(0, -2) }}</i>: {{ message.text }}</li> <li class="message" v-for="message in messages"><i :title="message.date">{{ message.date.split('T')[1].slice(0, -2) }}</i>: {{ message.text }}</li>
</ul> </ul>
</div> </div>
@ -25,11 +25,17 @@ export default {
}) })
}) })
}, },
watch: {
'messages': 'scrollToBottom'
},
beforeMount () { beforeMount () {
socket.on('new-message', (message) => { socket.on('new-message', (message) => {
this.messages.push(message) this.messages.push(message)
}) })
}, },
mounted () {
this.scrollToBottom()
},
methods: { methods: {
sendMessage () { sendMessage () {
if (!this.message.trim()) return if (!this.message.trim()) return
@ -40,6 +46,11 @@ export default {
this.messages.push(message) this.messages.push(message)
this.message = '' this.message = ''
socket.emit('send-message', message) socket.emit('send-message', message)
},
scrollToBottom () {
this.$nextTick(() => {
this.$refs.messages.scrollTop = this.$refs.messages.scrollHeight
})
} }
}, },
head: { head: {

View File

@ -1,4 +1,4 @@
import io from 'socket.io-client' import io from 'socket.io-client'
const socket = io('http://localhost:3000') const socket = io(process.env.HOST_URL)
export default socket export default socket

View File

@ -15,24 +15,22 @@ app.use(nuxt.render)
if (config.dev) { if (config.dev) {
nuxt.build() nuxt.build()
.catch((error) => { .catch((error) => {
console.error(error) // eslint-disable-line no-console console.error(error) // eslint-disable-line no-console
process.exit(1) process.exit(1)
}) })
} }
// Listen the server // Listen the server
server.listen(port, '0.0.0.0') server.listen(port, '0.0.0.0')
console.log('Server listening on localhost:' + port) console.log('Server listening on localhost:' + port) // eslint-disable-line no-console
// Socket.io // Socket.io
let messages = [] let messages = []
io.on('connection', (socket) => { io.on('connection', (socket) => {
console.log('New connection');
socket.on('last-messages', function (fn) { socket.on('last-messages', function (fn) {
fn(messages.slice(-50)) fn(messages.slice(-50))
}); });
socket.on('send-message', function (message) { socket.on('send-message', function (message) {
console.log('Message received', message);
messages.push(message) messages.push(message)
socket.broadcast.emit('new-message', message) socket.broadcast.emit('new-message', message)
}) })

View File

@ -9,6 +9,7 @@ Vue.use(Router)
function recursiveRoutes(routes, tab, components) { function recursiveRoutes(routes, tab, components) {
var res = '' var res = ''
routes.forEach((route, i) => { routes.forEach((route, i) => {
route._name = '_' + hash(route.component)
components.push({ _name: route._name, component: route.component }) components.push({ _name: route._name, component: route.component })
res += tab + '{\n' res += tab + '{\n'
res += tab + '\tpath: ' + JSON.stringify(route.path) + ',\n' res += tab + '\tpath: ' + JSON.stringify(route.path) + ',\n'

View File

@ -134,9 +134,12 @@ export default context => {
return _app return _app
}) })
.catch(function (error) { .catch(function (error) {
if (error && error instanceof Error) { if (error && (error instanceof Error || error.constructor.toString().indexOf('Error()') !== -1)) {
error = { statusCode: 500, message: error.message } error = { statusCode: 500, message: error.message }
} }
else if (typeof error === 'string') {
error = { statusCode: 500, message: error }
}
context.nuxt.error = context.error(error) context.nuxt.error = context.error(error)
<%= (store ? 'context.nuxt.state = store.state' : '') %> <%= (store ? 'context.nuxt.state = store.state' : '') %>
return _app return _app

View File

@ -205,7 +205,10 @@ function * generateRoutesAndFiles () {
return readFile(r(__dirname, 'app', file), 'utf8') return readFile(r(__dirname, 'app', file), 'utf8')
.then((fileContent) => { .then((fileContent) => {
const template = _.template(fileContent, { const template = _.template(fileContent, {
imports: { serialize } imports: {
serialize,
hash
}
}) })
const content = template(templateVars) const content = template(templateVars)
return writeFile(r(this.dir, '.nuxt', file), content, 'utf8') return writeFile(r(this.dir, '.nuxt', file), content, 'utf8')
@ -218,7 +221,7 @@ function createRoutes (files, srcDir) {
let routes = [] let routes = []
files.forEach((file) => { files.forEach((file) => {
let keys = file.replace(/^pages/, '').replace(/\.vue$/, '').replace(/\/{2,}/g, '/').split('/').slice(1) let keys = file.replace(/^pages/, '').replace(/\.vue$/, '').replace(/\/{2,}/g, '/').split('/').slice(1)
let route = { name: '', path: '', component: r(srcDir, file), _name: null } let route = { name: '', path: '', component: r(srcDir, file) }
let parent = routes let parent = routes
keys.forEach((key, i) => { keys.forEach((key, i) => {
route.name = route.name ? route.name + '-' + key.replace('_', '') : key.replace('_', '') route.name = route.name ? route.name + '-' + key.replace('_', '') : key.replace('_', '')
@ -240,7 +243,6 @@ function createRoutes (files, srcDir) {
} }
} }
}) })
route._name = '_' + hash(route.component)
// Order Routes path // Order Routes path
parent.push(route) parent.push(route)
parent.sort((a, b) => { parent.sort((a, b) => {

View File

@ -3,7 +3,7 @@
import vueLoaderConfig from './vue-loader.config' import vueLoaderConfig from './vue-loader.config'
import { defaults } from 'lodash' import { defaults } from 'lodash'
import { join } from 'path' import { join } from 'path'
import { urlJoin } from '../../utils' import { urlJoin } from '../utils'
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View File

@ -39,7 +39,7 @@ export default function () {
}) })
// Externals // Externals
const nuxtPackageJson = require('../../../package.json') const nuxtPackageJson = require('../../package.json')
const projectPackageJsonPath = resolve(this.dir, 'package.json') const projectPackageJsonPath = resolve(this.dir, 'package.json')
config.externals = Object.keys(nuxtPackageJson.dependencies || {}) config.externals = Object.keys(nuxtPackageJson.dependencies || {})
if (existsSync(projectPackageJsonPath)) { if (existsSync(projectPackageJsonPath)) {

View File

@ -1,6 +1,15 @@
const resolve = require('path').resolve
module.exports = { module.exports = {
router: { router: {
base: '/test/' base: '/test/',
extendRoutes (routes) {
routes.push({
name: 'about-bis',
path: '/about-bis',
component: resolve(__dirname, 'pages/about.vue')
})
}
}, },
cache: true, cache: true,
plugins: ['~plugins/test.js'], plugins: ['~plugins/test.js'],

View File

@ -47,6 +47,13 @@ test('/test/env', async t => {
t.true(html.includes('"string": "Nuxt.js"')) t.true(html.includes('"string": "Nuxt.js"'))
}) })
test('/test/about-bis (added with extendRoutes)', async t => {
const window = await nuxt.renderAndGetWindow(url('/test/about-bis'))
const html = window.document.body.innerHTML
t.true(html.includes('<h1>Custom layout</h1>'))
t.true(html.includes('<h1>About page</h1>'))
})
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after('Closing server and nuxt.js', t => {
server.close() server.close()