diff --git a/lib/build.js b/lib/build.js index a5e5299e6b..5ad93f10ab 100644 --- a/lib/build.js +++ b/lib/build.js @@ -97,8 +97,9 @@ export function options () { } export async function build () { + this._nuxtPages = typeof this.createRoutes !== 'function' // 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'))) { 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...') createWebpackMiddleware.call(this) webpackWatchAndUpdate.call(this) - watchPages.call(this) + watchFiles.call(this) } else { debug('Building files...') await webpackRunClient.call(this) @@ -172,6 +173,7 @@ async function generateRoutesAndFiles () { base: this.options.router.base, middleware: this.options.router.middleware, linkActiveClass: this.options.router.linkActiveClass, + linkExactActiveClass: this.options.router.linkExactActiveClass, scrollBehavior: this.options.router.scrollBehavior }, env: this.options.env, @@ -215,20 +217,22 @@ async function generateRoutesAndFiles () { // -- Routes -- debug('Generating routes...') - // Format routes for the lib/app/router.js template - if (typeof this.createRoutes !== 'function') { - // Use internal createRoutes + // If user defined a custom method to create routes + if (this._nuxtPages) { + // Use nuxt.js createRoutes bases on pages/ const files = await glob('pages/**/*.vue', {cwd: this.srcDir}) templateVars.router.routes = createRoutes(files, this.srcDir) } 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') { // 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 - this.routes = flatRoutes(templateVars.router.routes) + this.routes = flatRoutes(templateVars.router.routes || []) // -- Store -- // Add store if needed @@ -239,13 +243,13 @@ async function generateRoutesAndFiles () { // Resolve template files const customTemplateFiles = this.options.build.templates.map(t => t.dst || basename(t.src || t)) 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 const customPath = r(this.srcDir, 'app', file) const customFileExists = fs.existsSync(customPath) - // Skip if custom file was already provided in build.templates[] - if (customTemplateFiles.indexOf(file) !== -1 && !customFileExists) { - return - } return { src: customFileExists ? customPath : r(__dirname, 'app', file), dst: file, @@ -264,7 +268,7 @@ async function generateRoutesAndFiles () { })) // 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 this.options.build.watch.push(src) // Render template to dst @@ -513,17 +517,19 @@ function createRenderer (bundle, manifest) { this.renderToStream = this.renderer.renderToStream } -function watchPages () { +function watchFiles () { const patterns = [ - r(this.srcDir, 'pages'), r(this.srcDir, 'layouts'), r(this.srcDir, 'store'), r(this.srcDir, 'middleware'), - r(this.srcDir, 'pages/*.vue'), - r(this.srcDir, 'pages/**/*.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, { ignoreInitial: true }) @@ -532,10 +538,10 @@ function watchPages () { await generateRoutesAndFiles.call(this) }, 200) // Watch for internals - this.pagesFilesWatcher = chokidar.watch(patterns, options) + this.filesWatcher = chokidar.watch(patterns, options) .on('add', refreshFiles) .on('unlink', refreshFiles) // Watch for custom provided files this.customFilesWatcher = chokidar.watch(_.uniq(this.options.build.watch), options) - .on('change', refreshFiles) + .on('change', refreshFiles) } diff --git a/lib/nuxt.js b/lib/nuxt.js index 8cb245dcc3..a2c8f3c316 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -120,12 +120,12 @@ class Nuxt { this.module = new Module(this) // Install all modules in sequence and then return `this` instance return utils.sequence(options.modules, this.module.addModule.bind(this.module)) - .then(() => this) - .catch(/* istanbul ignore next */ (err) => { - console.error('[nuxt] error while initializing modules') // eslint-disable-line no-console - console.error(err) // eslint-disable-line no-console - process.exit(1) - }) + .then(() => this) + .catch(/* istanbul ignore next */ (err) => { + console.error('[nuxt] error while initializing modules') // eslint-disable-line no-console + console.error(err) // eslint-disable-line no-console + process.exit(1) + }) } close (callback) { @@ -145,8 +145,8 @@ class Nuxt { promises.push(p) } /* istanbul ignore if */ - if (this.pagesFilesWatcher) { - this.pagesFilesWatcher.close() + if (this.filesWatcher) { + this.filesWatcher.close() } /* istanbul ignore if */ if (this.customFilesWatcher) { diff --git a/lib/server.js b/lib/server.js index 53cc60379f..5ad4439529 100644 --- a/lib/server.js +++ b/lib/server.js @@ -23,9 +23,9 @@ class Server { // Require if needed if (typeof m === 'string') { let src = m - // Using ~ shorthand to resolve from project srcDir - if (src.indexOf('~') === 0) { - src = path.resolve(this.nuxt.options.srcDir, src.substr(1)) + // Using ~ or ./ shorthand to resolve from project srcDir + if (src.indexOf('~') === 0 || src.indexOf('./') === 0) { + src = path.join(this.nuxt.options.srcDir, src.substr(1)) } // eslint-disable-next-line no-eval m = eval('require')(src)