Skip to content

Commit

Permalink
watch pages/ dir to updates routes dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
Atinux committed Nov 10, 2016
1 parent d88948b commit c97c4ec
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 38 deletions.
2 changes: 1 addition & 1 deletion examples/with-ava/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ test('Route / exits and render HTML', async t => {
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
server.close()
nuxt.stop()
nuxt.close()
})
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*!
* nuxt.js
* MIT Licensed
*/
/*
** nuxt.js
*/

'use strict'

Expand Down
2 changes: 2 additions & 0 deletions lib/app/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

require('es6-promise').polyfill()
require('es6-object-assign').polyfill()
import Vue from 'vue'
Expand Down
2 changes: 2 additions & 0 deletions lib/app/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
Expand Down
2 changes: 2 additions & 0 deletions lib/app/router.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

import Vue from 'vue'
import Router from 'vue-router'
import Meta from 'vue-meta'
Expand Down
2 changes: 2 additions & 0 deletions lib/app/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const debug = require('debug')('nuxt:render')
import Vue from 'vue'
import { pick } from 'lodash'
Expand Down
93 changes: 62 additions & 31 deletions lib/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const debug = require('debug')('nuxt:build')
const _ = require('lodash')
const co = require('co')
const del = require('del')
const chokidar = require('chokidar')
const fs = require('fs')
const glob = require('glob-promise')
const hash = require('hash-sum')
Expand Down Expand Up @@ -90,6 +92,34 @@ module.exports = function * () {
if (!this.dev) {
yield mkdirp(r(this.dir, '.nuxt/dist'))
}
// Resolve custom routes component path
this.options.routes.forEach((route) => {
if (route.component.slice(-4) !== '.vue') {
route.component = route.component + '.vue'
}
route.component = r(this.dir, route.component)
})
// Generate routes and interpret the template files
yield generateRoutesAndFiles.call(this)
/*
** Generate .nuxt/dist/ files
*/
if (this.dev) {
debug('Adding webpack middlewares...')
createWebpackMiddlewares.call(this)
webpackWatchAndUpdate.call(this)
watchPages.call(this)
} else {
debug('Building files...')
yield [
webpackRunClient.call(this),
webpackRunServer.call(this)
]
}
}

function * generateRoutesAndFiles () {
debug('Generating routes...')
/*
** Generate routes based on files
*/
Expand All @@ -98,24 +128,14 @@ module.exports = function * () {
files.forEach((file) => {
let path = file.replace(/^pages/, '').replace(/index\.vue$/, '/').replace(/\.vue$/, '').replace(/\/{2,}/g, '/')
if (path[1] === '_') return
routes.push({ path: path, component: file })
})
this.options.routes.forEach((route) => {
if (route.component.slice(-4) !== '.vue') {
route.component = route.component + '.vue'
}
route.component = r(this.dir, route.component)
})
this.options.routes = routes.concat(this.options.routes)
// TODO: check .children
this.options.routes.forEach((route) => {
route._component = r(this.dir, route.component)
route._name = '_' + hash(route._component)
route.component = route._name
routes.push({ path: path, component: r(this.dir, file) })
})
// Concat pages routes and custom routes in this.routes
this.routes = routes.concat(this.options.routes)
/*
** Interpret and move template files to .nuxt/
*/
debug('Generating files...')
let templatesFiles = [
'App.vue',
'client.js',
Expand All @@ -135,8 +155,15 @@ module.exports = function * () {
Loading: r(__dirname, '..', 'app', 'components', 'Loading.vue'),
ErrorPage: r(__dirname, '..', '..', 'pages', (this.dev ? '_error-debug.vue' : '_error.vue'))
},
routes: this.options.routes
routes: this.routes
}
// Format routes for the lib/app/router.js template
// TODO: check .children
templateVars.routes.forEach((route) => {
route._component = route.component
route._name = '_' + hash(route._component)
route.component = route._name
})
if (this.options.store) {
templateVars.storePath = r(this.dir, 'store')
}
Expand All @@ -157,21 +184,6 @@ module.exports = function * () {
})
})
yield moveTemplates
debug('Files moved!')
/*
** Generate .nuxt/dist/ files
*/
if (this.dev) {
debug('Adding webpack middlewares...')
createWebpackMiddlewares.call(this)
webpackWatchAndUpdate.call(this)
} else {
debug('Building files...')
yield [
webpackRunClient.call(this),
webpackRunServer.call(this)
]
}
}

function getWebpackClientConfig () {
Expand All @@ -187,7 +199,7 @@ function getWebpackServerConfig () {
function createWebpackMiddlewares () {
const clientConfig = getWebpackClientConfig.call(this)
// setup on the fly compilation + hot-reload
clientConfig.entry.app = ['webpack-hot-middleware/client', clientConfig.entry.app]
clientConfig.entry.app = ['webpack-hot-middleware/client?reload=true', clientConfig.entry.app]
clientConfig.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
Expand Down Expand Up @@ -265,3 +277,22 @@ function createRenderer (bundle) {
this.renderToString = pify(this.renderer.renderToString)
this.renderToStream = this.renderer.renderToStream
}

function watchPages () {
const patterns = [ r(this.dir, 'pages/*.vue'), r(this.dir, 'pages/**/*.vue') ]
const options = {
ignored: '**/_*.vue',
ignoreInitial: true
}
const refreshFiles = _.debounce(() => {
console.log('Reload files', this.routes.length)
var d = Date.now()
co(generateRoutesAndFiles.bind(this))
.then(() => {
console.log('Time to gen:' + (Date.now() - d) + 'ms')
})
}, 200)
this.pagesFilesWatcher = chokidar.watch(patterns, options)
.on('add', refreshFiles)
.on('unlink', refreshFiles)
}
2 changes: 2 additions & 0 deletions lib/build/webpack/base.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const vueLoaderConfig = require('./vue-loader.config')
const { join } = require('path')

Expand Down
2 changes: 2 additions & 0 deletions lib/build/webpack/client.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const webpack = require('webpack')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const base = require('./base.config')
Expand Down
2 changes: 2 additions & 0 deletions lib/build/webpack/server.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const webpack = require('webpack')
const base = require('./base.config')
const { uniq } = require('lodash')
Expand Down
2 changes: 2 additions & 0 deletions lib/build/webpack/vue-loader.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

module.exports = function () {
let config = {
postcss: [
Expand Down
5 changes: 4 additions & 1 deletion lib/nuxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Nuxt {
})
}

stop (callback) {
close (callback) {
let promises = []
if (this.webpackDevMiddleware) {
const p = new Promise((resolve, reject) => {
Expand All @@ -147,6 +147,9 @@ class Nuxt {
})
promises.push(p)
}
if (this.pagesFilesWatcher) {
this.pagesFilesWatcher.close()
}
return co(function * () {
yield promises
})
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuxt",
"version": "0.3.0",
"version": "0.3.1",
"description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)",
"main": "index.js",
"license": "MIT",
Expand All @@ -22,6 +22,7 @@
"babel-loader": "^6.2.7",
"babel-preset-es2015": "^6.18.0",
"babel-preset-stage-2": "^6.18.0",
"chokidar": "^1.6.1",
"co": "^4.6.0",
"cross-spawn": "^5.0.1",
"css-loader": "^0.25.0",
Expand Down

0 comments on commit c97c4ec

Please sign in to comment.