diff --git a/packages/builder/src/builder.js b/packages/builder/src/builder.js index ec6956abdedd..09aa77a99cde 100644 --- a/packages/builder/src/builder.js +++ b/packages/builder/src/builder.js @@ -539,9 +539,10 @@ export default class Builder { } catch (err) { throw new Error(`Could not compile template ${src}: ${err.message}`) } - const _path = r(this.options.buildDir, dst) + // Ensure parent dir exits and write file - await fsExtra.outputFile(_path, content, 'utf8') + const relativePath = r(this.options.buildDir, dst) + await fsExtra.outputFile(relativePath, content, 'utf8') }) ) } @@ -662,12 +663,12 @@ export default class Builder { this.createFileWatcher( nuxtRestartWatch, ['all'], - (event, _path) => { + (event, fileName) => { if (['add', 'change', 'unlink'].includes(event) === false) { return } - this.nuxt.callHook('watch:fileChanged', this, _path) // Legacy - this.nuxt.callHook('watch:restart', { event, path: _path }) + this.nuxt.callHook('watch:fileChanged', this, fileName) // Legacy + this.nuxt.callHook('watch:restart', { event, path: fileName }) }, this.assignWatcher('restart') ) diff --git a/packages/generator/src/generator.js b/packages/generator/src/generator.js index d90c84dba7c8..1b0b0c4662e1 100644 --- a/packages/generator/src/generator.js +++ b/packages/generator/src/generator.js @@ -242,17 +242,17 @@ export default class Generator { } } - let _path + let fileName if (this.options.generate.subFolders) { - _path = path.join(route, path.sep, 'index.html') // /about -> /about/index.html - _path = _path === '/404/index.html' ? '/404.html' : _path // /404 -> /404.html + fileName = path.join(route, path.sep, 'index.html') // /about -> /about/index.html + fileName = fileName === '/404/index.html' ? '/404.html' : fileName // /404 -> /404.html } else { - _path = route.length > 1 ? path.join(path.sep, route + '.html') : path.join(path.sep, 'index.html') + fileName = route.length > 1 ? path.join(path.sep, route + '.html') : path.join(path.sep, 'index.html') } // Call hook to let user update the path & html - const page = { route, path: _path, html } + const page = { route, path: fileName, html } await this.nuxt.callHook('generate:page', page) page.path = path.join(this.distPath, page.path) diff --git a/packages/utils/src/resolve.js b/packages/utils/src/resolve.js index a1fe820870f8..ee53fb2b93df 100644 --- a/packages/utils/src/resolve.js +++ b/packages/utils/src/resolve.js @@ -49,15 +49,15 @@ export const relativeTo = function relativeTo(...args) { } // Resolve path - const _path = r(...args) + const resolvedPath = r(...args) // Check if path is an alias - if (startsWithSrcAlias(_path)) { - return _path + if (startsWithSrcAlias(resolvedPath)) { + return resolvedPath } // Make correct relative path - let rp = path.relative(dir, _path) + let rp = path.relative(dir, resolvedPath) if (rp[0] !== '.') { rp = '.' + path.sep + rp } diff --git a/packages/utils/src/route.js b/packages/utils/src/route.js index 5bda20e77953..cb71497e1c09 100644 --- a/packages/utils/src/route.js +++ b/packages/utils/src/route.js @@ -4,22 +4,22 @@ import consola from 'consola' import { r } from './resolve' -export const flatRoutes = function flatRoutes(router, _path = '', routes = []) { +export const flatRoutes = function flatRoutes(router, fileName = '', routes = []) { router.forEach((r) => { if ([':', '*'].some(c => r.path.includes(c))) { return } if (r.children) { - if (_path === '' && r.path === '/') { + if (fileName === '' && r.path === '/') { routes.push('/') } - return flatRoutes(r.children, _path + r.path + '/', routes) + return flatRoutes(r.children, fileName + r.path + '/', routes) } - _path = _path.replace(/^\/+$/, '/') + fileName = fileName.replace(/^\/+$/, '/') routes.push( - (r.path === '' && _path[_path.length - 1] === '/' - ? _path.slice(0, -1) - : _path) + r.path + (r.path === '' && fileName[fileName.length - 1] === '/' + ? fileName.slice(0, -1) + : fileName) + r.path ) }) return routes