mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
Merge remote-tracking branch 'origin/0.10.0' into 0.10.0
# Conflicts: # lib/generate.js # lib/utils.js
This commit is contained in:
commit
0a595bd1c3
15
lib/build.js
15
lib/build.js
@ -220,7 +220,7 @@ function * generateRoutesAndFiles () {
|
||||
this.options.router.extendRoutes(templateVars.router.routes, r)
|
||||
}
|
||||
// Routes for Generate command
|
||||
this.routes = flatRoutes(templateVars.router.routes, '')
|
||||
this.routes = flatRoutes(templateVars.router.routes)
|
||||
debug('Generating files...')
|
||||
if (layoutsFiles.includes('layouts/error.vue')) {
|
||||
templateVars.components.ErrorPage = r(this.srcDir, 'layouts/error.vue')
|
||||
@ -341,13 +341,14 @@ function cleanChildrenRoutes (routes, isChild = false) {
|
||||
return routes
|
||||
}
|
||||
|
||||
function flatRoutes (router, path) {
|
||||
let routes = []
|
||||
function flatRoutes (router, path = '', routes = []) {
|
||||
router.forEach((r) => {
|
||||
if (r.children) {
|
||||
routes.concat(flatRoutes(r.children, r.path))
|
||||
} else {
|
||||
routes.push(path + r.path)
|
||||
if (!r.path.includes(':') && !r.path.includes('*')) {
|
||||
if (r.children) {
|
||||
flatRoutes(r.children, path + r.path + '/', routes)
|
||||
} else {
|
||||
routes.push((r.path === '' && path[path.length - 1] === '/' ? path.slice(0, -1) : path) + r.path)
|
||||
}
|
||||
}
|
||||
})
|
||||
return routes
|
||||
|
@ -4,10 +4,9 @@ const debug = require('debug')('nuxt:generate')
|
||||
import fs from 'fs-extra'
|
||||
import co from 'co'
|
||||
import pify from 'pify'
|
||||
import pathToRegexp from 'path-to-regexp'
|
||||
import _ from 'lodash'
|
||||
import { resolve, join, dirname, sep } from 'path'
|
||||
import { isUrl, promisifyRouteParams } from './utils'
|
||||
import { isUrl, promisifyRoute } from './utils'
|
||||
import { minify } from 'html-minifier'
|
||||
const copy = pify(fs.copy)
|
||||
const remove = pify(fs.remove)
|
||||
@ -16,7 +15,7 @@ const mkdirp = pify(fs.mkdirp)
|
||||
|
||||
const defaults = {
|
||||
dir: 'dist',
|
||||
routeParams: {}
|
||||
routes: []
|
||||
}
|
||||
|
||||
export default function () {
|
||||
@ -52,29 +51,24 @@ export default function () {
|
||||
debug('Static & build files copied')
|
||||
})
|
||||
.then(() => {
|
||||
// Resolve config.generate.routesParams promises before generating the routes
|
||||
return resolveRouteParams(this.options.generate.routeParams)
|
||||
// Resolve config.generate.routes promises before generating the routes
|
||||
return promisifyRoute(this.options.generate.routes)
|
||||
.catch((e) => {
|
||||
console.error(`Could not resolve routes`) // eslint-disable-line no-console
|
||||
console.error(e) // eslint-disable-line no-console
|
||||
process.exit(1)
|
||||
})
|
||||
})
|
||||
.then(() => {
|
||||
.then((generateRoutes) => {
|
||||
/*
|
||||
** Generate html files from routes
|
||||
*/
|
||||
let routes = []
|
||||
this.routes.forEach((route) => {
|
||||
if (route.includes(':') || route.includes('*')) {
|
||||
const routeParams = this.options.generate.routeParams[route]
|
||||
if (!routeParams) {
|
||||
console.error(`Could not generate the dynamic route ${route}, please add the mapping params in nuxt.config.js (generate.routeParams).`) // eslint-disable-line no-console
|
||||
return process.exit(1)
|
||||
}
|
||||
const toPathRegexp = pathToRegexp.compile(route)
|
||||
routes = routes.concat(routeParams.map((params) => {
|
||||
return toPathRegexp(params)
|
||||
}))
|
||||
} else {
|
||||
routes.push(route)
|
||||
generateRoutes.forEach((route) => {
|
||||
if (this.routes.indexOf(route) < 0) {
|
||||
this.routes.push(route)
|
||||
}
|
||||
})
|
||||
let routes = this.routes
|
||||
return co(function * () {
|
||||
while (routes.length) {
|
||||
yield routes.splice(0, 500).map((route) => {
|
||||
@ -123,20 +117,3 @@ export default function () {
|
||||
return this
|
||||
})
|
||||
}
|
||||
|
||||
function resolveRouteParams (routeParams) {
|
||||
let promises = []
|
||||
Object.keys(routeParams).forEach(function (routePath) {
|
||||
let promise = promisifyRouteParams(routeParams[routePath])
|
||||
promise.then((routeParamsData) => {
|
||||
routeParams[routePath] = routeParamsData
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(`Could not resolve routeParams[${routePath}]`) // eslint-disable-line no-console
|
||||
console.error(e) // eslint-disable-line no-console
|
||||
process.exit(1)
|
||||
})
|
||||
promises.push(promise)
|
||||
})
|
||||
return Promise.all(promises)
|
||||
}
|
||||
|
@ -35,12 +35,12 @@ export function isUrl (url) {
|
||||
return (url.indexOf('http') === 0 || url.indexOf('//') === 0)
|
||||
}
|
||||
|
||||
export function promisifyRouteParams (fn) {
|
||||
// If routeParams[route] is an array
|
||||
export function promisifyRoute (fn) {
|
||||
// If routes is an array
|
||||
if (Array.isArray(fn)) {
|
||||
return Promise.resolve(fn)
|
||||
}
|
||||
// If routeParams[route] is a function expecting a callback
|
||||
// If routes is a function expecting a callback
|
||||
if (fn.length === 1) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fn(function (err, routeParams) {
|
||||
|
@ -72,7 +72,6 @@
|
||||
"lodash": "^4.17.4",
|
||||
"lru-cache": "^4.0.2",
|
||||
"memory-fs": "^0.4.1",
|
||||
"path-to-regexp": "^1.7.0",
|
||||
"pify": "^2.3.0",
|
||||
"post-compile-webpack-plugin": "^0.1.1",
|
||||
"preload-webpack-plugin": "^1.2.1",
|
||||
|
Loading…
Reference in New Issue
Block a user