From 40515ac91a9e4ae5fd7b4a37c75de2843f6d3fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Thu, 24 Nov 2016 01:47:11 +0100 Subject: [PATCH] Update dept and start next generate --- lib/app/index.js | 2 +- lib/app/router.js | 2 +- lib/build/index.js | 16 ++++++------ lib/generate.js | 63 +++++++++++++++++++++++++++++++++++++--------- lib/utils.js | 25 ++++++++++++++++++ package.json | 12 ++++----- 6 files changed, 93 insertions(+), 27 deletions(-) diff --git a/lib/app/index.js b/lib/app/index.js index 6ddd25963d..dfd79a443b 100644 --- a/lib/app/index.js +++ b/lib/app/index.js @@ -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' diff --git a/lib/app/router.js b/lib/app/router.js index 850a8a1a59..9ac3adceb2 100644 --- a/lib/app/router.js +++ b/lib/app/router.js @@ -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) => { diff --git a/lib/build/index.js b/lib/build/index.js index daa08631de..5d283743f9 100644 --- a/lib/build/index.js +++ b/lib/build/index.js @@ -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') diff --git a/lib/generate.js b/lib/generate.js index 5147b58f8b..f41837ed87 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -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,24 +60,45 @@ 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) => { - var promise = this.renderRoute(route.path) - .then(({ html }) => { - var path = join(route.path, sep, 'index.html') // /about -> /about/index.html - debug('Generate file: ' + path) - path = join(distPath, path) - // Make sure the sub folders are created - return co(function * () { - yield mkdirp(dirname(path)) - yield writeFile(path, html, 'utf8') + 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 + debug('Generate file: ' + path) + path = join(distPath, path) + // Make sure the sub folders are created + return co(function * () { + yield mkdirp(dirname(path)) + yield writeFile(path, html, 'utf8') + }) + }) + promises.push(promise) }) - promises.push(promise) }) return Promise.all(promises) }) @@ -84,3 +106,20 @@ module.exports = function () { 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) +} diff --git a/lib/utils.js b/lib/utils.js index 22fd0f4b19..6cde738d13 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 +} diff --git a/package.json b/package.json index 47c4af5aef..f02a4c8053 100644 --- a/package.json +++ b/package.json @@ -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",