Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: avoid using _path variable #5582

Merged
merged 4 commits into from Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/builder/src/builder.js
Expand Up @@ -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')
})
)
}
Expand Down Expand Up @@ -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')
)
Expand Down
10 changes: 5 additions & 5 deletions packages/generator/src/generator.js
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions packages/utils/src/resolve.js
Expand Up @@ -49,15 +49,15 @@ export const relativeTo = function relativeTo(...args) {
}

// Resolve path
const _path = r(...args)
const relativePath = r(...args)

// Check if path is an alias
if (startsWithSrcAlias(_path)) {
return _path
if (startsWithSrcAlias(relativePath)) {
return relativePath
}

// Make correct relative path
let rp = path.relative(dir, _path)
let rp = path.relative(dir, relativePath)
if (rp[0] !== '.') {
rp = '.' + path.sep + rp
}
Expand Down
14 changes: 7 additions & 7 deletions packages/utils/src/route.js
Expand Up @@ -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
Expand Down