This commit is contained in:
Sébastien Chopin 2017-05-21 19:18:48 +02:00
parent c5b5913402
commit f050bb6330
3 changed files with 36 additions and 30 deletions

View File

@ -97,8 +97,9 @@ export function options () {
} }
export async function build () { export async function build () {
this._nuxtPages = typeof this.createRoutes !== 'function'
// Check if pages dir exists and warn if not // Check if pages dir exists and warn if not
if (typeof this.createRoutes !== 'function') { if (this._nuxtPages) {
if (!fs.existsSync(join(this.srcDir, 'pages'))) { if (!fs.existsSync(join(this.srcDir, 'pages'))) {
if (fs.existsSync(join(this.srcDir, '..', 'pages'))) { if (fs.existsSync(join(this.srcDir, '..', 'pages'))) {
console.error('> No `pages` directory found. Did you mean to run `nuxt` in the parent (`../`) directory?') // eslint-disable-line no-console console.error('> No `pages` directory found. Did you mean to run `nuxt` in the parent (`../`) directory?') // eslint-disable-line no-console
@ -128,7 +129,7 @@ async function buildFiles () {
debug('Adding webpack middleware...') debug('Adding webpack middleware...')
createWebpackMiddleware.call(this) createWebpackMiddleware.call(this)
webpackWatchAndUpdate.call(this) webpackWatchAndUpdate.call(this)
watchPages.call(this) watchFiles.call(this)
} else { } else {
debug('Building files...') debug('Building files...')
await webpackRunClient.call(this) await webpackRunClient.call(this)
@ -172,6 +173,7 @@ async function generateRoutesAndFiles () {
base: this.options.router.base, base: this.options.router.base,
middleware: this.options.router.middleware, middleware: this.options.router.middleware,
linkActiveClass: this.options.router.linkActiveClass, linkActiveClass: this.options.router.linkActiveClass,
linkExactActiveClass: this.options.router.linkExactActiveClass,
scrollBehavior: this.options.router.scrollBehavior scrollBehavior: this.options.router.scrollBehavior
}, },
env: this.options.env, env: this.options.env,
@ -215,20 +217,22 @@ async function generateRoutesAndFiles () {
// -- Routes -- // -- Routes --
debug('Generating routes...') debug('Generating routes...')
// Format routes for the lib/app/router.js template // If user defined a custom method to create routes
if (typeof this.createRoutes !== 'function') { if (this._nuxtPages) {
// Use internal createRoutes // Use nuxt.js createRoutes bases on pages/
const files = await glob('pages/**/*.vue', {cwd: this.srcDir}) const files = await glob('pages/**/*.vue', {cwd: this.srcDir})
templateVars.router.routes = createRoutes(files, this.srcDir) templateVars.router.routes = createRoutes(files, this.srcDir)
} else { } else {
templateVars.router.routes = this.createRoutes.call(this, this.srcDir) this.createRoutes = this.createRoutes.bind(this)
templateVars.router.routes = this.createRoutes(this.srcDir)
} }
// router.extendRoutes method
if (typeof this.options.router.extendRoutes === 'function') { if (typeof this.options.router.extendRoutes === 'function') {
// let the user extend the routes // let the user extend the routes
this.options.router.extendRoutes.call(this, templateVars.router.routes, r) this.options.router.extendRoutes.call(this, templateVars.router.routes || [], r)
} }
// Routes for generate command // Routes for generate command
this.routes = flatRoutes(templateVars.router.routes) this.routes = flatRoutes(templateVars.router.routes || [])
// -- Store -- // -- Store --
// Add store if needed // Add store if needed
@ -239,13 +243,13 @@ async function generateRoutesAndFiles () {
// Resolve template files // Resolve template files
const customTemplateFiles = this.options.build.templates.map(t => t.dst || basename(t.src || t)) const customTemplateFiles = this.options.build.templates.map(t => t.dst || basename(t.src || t))
templatesFiles = templatesFiles.map(file => { templatesFiles = templatesFiles.map(file => {
// Skip if custom file was already provided in build.templates[]
if (customTemplateFiles.indexOf(file) !== -1) {
return
}
// Allow override templates using a file with same name in ${srcDir}/app // Allow override templates using a file with same name in ${srcDir}/app
const customPath = r(this.srcDir, 'app', file) const customPath = r(this.srcDir, 'app', file)
const customFileExists = fs.existsSync(customPath) const customFileExists = fs.existsSync(customPath)
// Skip if custom file was already provided in build.templates[]
if (customTemplateFiles.indexOf(file) !== -1 && !customFileExists) {
return
}
return { return {
src: customFileExists ? customPath : r(__dirname, 'app', file), src: customFileExists ? customPath : r(__dirname, 'app', file),
dst: file, dst: file,
@ -264,7 +268,7 @@ async function generateRoutesAndFiles () {
})) }))
// Interpret and move template files to .nuxt/ // Interpret and move template files to .nuxt/
return Promise.all(templatesFiles.map(async ({src, dst, options, custom}) => { return Promise.all(templatesFiles.map(async ({ src, dst, options, custom }) => {
// Add template to watchers // Add template to watchers
this.options.build.watch.push(src) this.options.build.watch.push(src)
// Render template to dst // Render template to dst
@ -513,17 +517,19 @@ function createRenderer (bundle, manifest) {
this.renderToStream = this.renderer.renderToStream this.renderToStream = this.renderer.renderToStream
} }
function watchPages () { function watchFiles () {
const patterns = [ const patterns = [
r(this.srcDir, 'pages'),
r(this.srcDir, 'layouts'), r(this.srcDir, 'layouts'),
r(this.srcDir, 'store'), r(this.srcDir, 'store'),
r(this.srcDir, 'middleware'), r(this.srcDir, 'middleware'),
r(this.srcDir, 'pages/*.vue'),
r(this.srcDir, 'pages/**/*.vue'),
r(this.srcDir, 'layouts/*.vue'), r(this.srcDir, 'layouts/*.vue'),
r(this.srcDir, 'layouts/**/*.vue') r(this.srcDir, 'layouts/**/*.vue')
] ]
if (this._nuxtPages) {
patterns.push(r(this.srcDir, 'pages'))
patterns.push(r(this.srcDir, 'pages/*.vue'))
patterns.push(r(this.srcDir, 'pages/**/*.vue'))
}
const options = Object.assign({}, this.options.watchers.chokidar, { const options = Object.assign({}, this.options.watchers.chokidar, {
ignoreInitial: true ignoreInitial: true
}) })
@ -532,7 +538,7 @@ function watchPages () {
await generateRoutesAndFiles.call(this) await generateRoutesAndFiles.call(this)
}, 200) }, 200)
// Watch for internals // Watch for internals
this.pagesFilesWatcher = chokidar.watch(patterns, options) this.filesWatcher = chokidar.watch(patterns, options)
.on('add', refreshFiles) .on('add', refreshFiles)
.on('unlink', refreshFiles) .on('unlink', refreshFiles)
// Watch for custom provided files // Watch for custom provided files

View File

@ -145,8 +145,8 @@ class Nuxt {
promises.push(p) promises.push(p)
} }
/* istanbul ignore if */ /* istanbul ignore if */
if (this.pagesFilesWatcher) { if (this.filesWatcher) {
this.pagesFilesWatcher.close() this.filesWatcher.close()
} }
/* istanbul ignore if */ /* istanbul ignore if */
if (this.customFilesWatcher) { if (this.customFilesWatcher) {

View File

@ -23,9 +23,9 @@ class Server {
// Require if needed // Require if needed
if (typeof m === 'string') { if (typeof m === 'string') {
let src = m let src = m
// Using ~ shorthand to resolve from project srcDir // Using ~ or ./ shorthand to resolve from project srcDir
if (src.indexOf('~') === 0) { if (src.indexOf('~') === 0 || src.indexOf('./') === 0) {
src = path.resolve(this.nuxt.options.srcDir, src.substr(1)) src = path.join(this.nuxt.options.srcDir, src.substr(1))
} }
// eslint-disable-next-line no-eval // eslint-disable-next-line no-eval
m = eval('require')(src) m = eval('require')(src)