Update dept and start next generate

This commit is contained in:
Sébastien Chopin 2016-11-24 01:47:11 +01:00
parent e441b7eb12
commit 40515ac91a
6 changed files with 93 additions and 27 deletions

View File

@ -1,7 +1,7 @@
'use strict'
import Vue from 'vue'
import Meta from 'vue-meta/lib/vue-meta.js' // require the ES2015 lib
import Meta from 'vue-meta'
import router from './router.js'
<% if (store) { %>import store from '~store/index.js'<% } %>
import NuxtContainer from './components/nuxt-container.vue'

View File

@ -6,7 +6,7 @@ import Router from 'vue-router'
Vue.use(Router)
<% uniqBy(router.routes, '_name').forEach((route) => { %>
const <%= route._name %> = process.BROWSER ? () => System.import('<%= route._component %>') : require('<%= route._component %>')
const <%= route._name %> = process.BROWSER_BUILD ? () => System.import('<%= route._component %>') : require('<%= route._component %>')
<% }) %>
const scrollBehavior = (to, from, savedPosition) => {

View File

@ -5,7 +5,6 @@ const _ = require('lodash')
const co = require('co')
const chokidar = require('chokidar')
const fs = require('fs-extra')
const glob = require('glob-promise')
const hash = require('hash-sum')
const pify = require('pify')
const webpack = require('webpack')
@ -16,6 +15,7 @@ const remove = pify(fs.remove)
const readFile = pify(fs.readFile)
const writeFile = pify(fs.writeFile)
const mkdirp = pify(fs.mkdirp)
const glob = pify(require('glob'))
const reqSep = /\//g
const sysSep = _.escapeRegExp(sep)
const normalize = string => string.replace(reqSep, sysSep)
@ -171,8 +171,7 @@ function * generateRoutesAndFiles () {
isDev: this.dev,
router: {
base: this.options.router.base,
linkActiveClass: this.options.router.linkActiveClass,
routes: this.routes
linkActiveClass: this.options.router.linkActiveClass
},
head: this.options.head,
store: this.options.store,
@ -190,10 +189,13 @@ function * generateRoutesAndFiles () {
templateVars.loading = templateVars.loading + '.vue'
}
// Format routes for the lib/app/router.js template
templateVars.router.routes.forEach((route) => {
route._component = route.component
route._name = '_' + hash(route._component)
route.component = route._name
templateVars.router.routes = this.routes.map((route) => {
const r = Object.assign({}, route)
r._component = r.component
r._name = '_' + hash(r._component)
r.component = r._name
r.path = r.path.replace(/\\/g, '\\\\') // regex expression in route path escaping for lodash templating
return r
})
if (files.includes('pages/_app.vue')) {
templateVars.appPath = r(this.dir, 'pages/_app.vue')

View File

@ -1,12 +1,13 @@
'use strict'
const debug = require('debug')('nuxt:generate')
const fs = require('fs-extra')
const co = require('co')
const pify = require('pify')
const debug = require('debug')('nuxt:generate')
const pathToRegexp = require('path-to-regexp')
const _ = require('lodash')
const { resolve, join, dirname, sep } = require('path')
const { urlJoin } = require('./utils')
const { urlJoin, promisifyRouteParams } = require('./utils')
const copy = pify(fs.copy)
const remove = pify(fs.remove)
const writeFile = pify(fs.writeFile)
@ -59,12 +60,32 @@ module.exports = function () {
yield copy(srcBuiltPath, distNuxtPath)
debug('Static & build files copied')
})
.then(() => {
// Resolve config.generate.routesParams promises before generating the routes
return resolveRouteParams(this.options.generate.routeParams)
})
.then(() => {
/*
** Generate html files from routes
*/
var promises = []
this.routes.forEach((route) => {
let subRoutes = []
if (route.path.includes(':') || route.path.includes('*')) {
const routeParams = this.options.generate.routeParams[route.path]
if (!routeParams) {
console.error(`Could not generate the dynamic route ${route.path}, please add the mapping params in nuxt.config.js (generate.routeParams).`)
return process.exit(1)
}
const toPath = pathToRegexp.compile(route.path)
routeParams.forEach((params) => {
const newRoute = Object.assign({}, route, { path: toPath(params) })
subRoutes.push(newRoute)
})
} else {
subRoutes.push(route)
}
subRoutes.forEach((route) => {
var promise = this.renderRoute(route.path)
.then(({ html }) => {
var path = join(route.path, sep, 'index.html') // /about -> /about/index.html
@ -78,9 +99,27 @@ module.exports = function () {
})
promises.push(promise)
})
})
return Promise.all(promises)
})
.then((pages) => {
debug('HTML Files generated')
})
}
function resolveRouteParams (routeParams) {
let promises = []
Object.keys(routeParams).forEach(function (routePath) {
let promise = promisifyRouteParams(routeParams[routePath])
.then((routeParamsData) => {
routeParams[routePath] = routeParamsData
})
.catch((e) => {
console.error(`Could not resolve routeParams[${routePath}]`)
console.error(e)
process.exit(1)
})
promises.push(promise)
})
return Promise.all(promises)
}

View File

@ -31,3 +31,28 @@ exports.waitFor = function * (ms) {
exports.urlJoin = function () {
return [].slice.call(arguments).join('/').replace(/\/+/g, '/')
}
exports.promisifyRouteParams = function (fn) {
// If routeParams[route] is an array
if (Array.isArray(fn)) {
return Promise.resolve(fn)
}
let promise
// If routeParams[route] is a function expecting a callback
if (fn.length === 1) {
promise = new Promise((resolve, reject) => {
fn(function (err, routeParams) {
if (err) {
reject(err)
}
resolve(routeParams)
})
})
} else {
promise = fn()
}
if (!(promise instanceof Promise)) {
promise = Promise.resolve(promise)
}
return promise
}

View File

@ -1,6 +1,6 @@
{
"name": "nuxt",
"version": "0.6.9",
"version": "0.7.0",
"description": "A minimalistic framework for server-rendered Vue.js applications (inspired by Next.js)",
"main": "index.js",
"license": "MIT",
@ -30,7 +30,7 @@
"extract-text-webpack-plugin": "2.0.0-beta.4",
"file-loader": "^0.9.0",
"fs-extra": "^1.0.0",
"glob-promise": "^2.0.0",
"glob": "^7.1.1",
"hash-sum": "^1.0.2",
"lodash": "^4.17.2",
"lru-cache": "^4.0.1",
@ -39,12 +39,12 @@
"serialize-javascript": "^1.3.0",
"serve-static": "^1.11.1",
"url-loader": "^0.5.7",
"vue": "^2.1.0",
"vue": "^2.1.3",
"vue-loader": "^10.0.0",
"vue-meta": "^0.4.4",
"vue-meta": "^0.5.2",
"vue-router": "^2.0.3",
"vue-server-renderer": "^2.1.0",
"vue-template-compiler": "^2.1.0",
"vue-server-renderer": "^2.1.3",
"vue-template-compiler": "^2.1.3",
"vuex": "^2.0.0",
"webpack": "2.1.0-beta.27",
"webpack-dev-middleware": "^1.8.4",