fix(builder): apply overrides from app dir only (#6283)

[release]
This commit is contained in:
Pooya Parsa 2019-08-24 21:40:39 +04:30 committed by GitHub
parent 12ca1ce37d
commit 7f542e08e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 25 deletions

View File

@ -416,19 +416,16 @@ export default class Builder {
// Sanitize custom template files
this.options.build.templates = this.options.build.templates.map((t) => {
const src = t.src || t
return Object.assign(
{
return {
src: r(this.options.srcDir, src),
dst: t.dst || path.basename(src),
custom: true
},
typeof t === 'object' ? t : undefined
)
custom: true,
...(typeof t === 'object' ? t : undefined)
}
})
const customTemplateFiles = this.options.build.templates.map(
t => t.dst || path.basename(t.src || t)
)
const customTemplateFiles = this.options.build.templates.map(t => t.dst || path.basename(t.src || t))
const templatePaths = uniq([
// Modules & user provided templates
// first custom to keep their index
@ -437,20 +434,25 @@ export default class Builder {
...templateContext.templateFiles
])
const appDir = path.resolve(this.options.srcDir, this.options.dir.app)
templateContext.templateFiles = await Promise.all(templatePaths.map(async (file) => {
// Use custom file if provided in build.templates[]
const customTemplateIndex = customTemplateFiles.indexOf(file)
const customTemplate = customTemplateIndex !== -1 ? this.options.build.templates[customTemplateIndex] : null
let src = customTemplate ? (customTemplate.src || customTemplate) : r(this.template.dir, file)
// Allow override templates using a file with same name in ${srcDir}/app
const customPath = r(this.options.srcDir, this.options.dir.app, file)
const customFileExists = await fsExtra.exists(customPath)
src = customFileExists ? customPath : src
const customAppFile = path.resolve(this.options.srcDir, this.options.dir.app, file)
const customAppFileExists = customAppFile.startsWith(appDir) && await fsExtra.exists(customAppFile)
if (customAppFileExists) {
src = customAppFile
}
return {
src,
dst: file,
custom: Boolean(customFileExists || customTemplate),
custom: Boolean(customAppFileExists || customTemplate),
options: (customTemplate && customTemplate.options) || {}
}
}))

View File

@ -25,8 +25,9 @@ describe('builder: builder generate', () => {
r.mockImplementation((...args) => `r(${args.join(', ')})`)
fs.readFile.mockImplementation((...args) => `readFile(${args.join(', ')})`)
fs.outputFile.mockImplementation((...args) => `outputFile(${args.join(', ')})`)
jest.spyOn(path, 'join').mockImplementation((...args) => `join(${args.join(', ')})`)
jest.spyOn(path, 'resolve').mockImplementation((...args) => `resolve(${args.join(', ')})`)
const { join, resolve } = path.posix
jest.spyOn(path, 'join').mockImplementation((...args) => join(...args))
jest.spyOn(path, 'resolve').mockImplementation((...args) => resolve(...args))
})
afterAll(() => {
@ -211,7 +212,7 @@ describe('builder: builder generate', () => {
await builder.resolveCustomTemplates(templateContext)
expect(templateContext.templateFiles).toEqual([
{ custom: true, dst: 'foo.js', src: 'r(/var/nuxt/src, app, foo.js)', options: {} },
{ custom: true, dst: 'foo.js', src: '/var/nuxt/src/app/foo.js', options: {} },
{ custom: true, dst: 'bar.js', src: '/var/nuxt/templates/bar.js', options: {} },
{ custom: true, dst: 'baz.js', src: '/var/nuxt/templates/baz.js', options: {} },
{ custom: false, dst: 'router.js', src: 'r(/var/nuxt/templates, router.js)', options: {} },
@ -237,12 +238,12 @@ describe('builder: builder generate', () => {
expect(path.resolve).toBeCalledTimes(1)
expect(path.resolve).toBeCalledWith('/var/nuxt/templates', 'views/loading', 'test_loading_indicator.html')
expect(fs.exists).toBeCalledTimes(1)
expect(fs.exists).toBeCalledWith('resolve(/var/nuxt/templates, views/loading, test_loading_indicator.html)')
expect(fs.exists).toBeCalledWith('/var/nuxt/templates/views/loading/test_loading_indicator.html')
expect(templateFiles).toEqual([{
custom: false,
dst: 'loading.html',
options: { name: 'test_loading_indicator' },
src: 'resolve(/var/nuxt/templates, views/loading, test_loading_indicator.html)'
src: '/var/nuxt/templates/views/loading/test_loading_indicator.html'
}])
})
@ -265,7 +266,7 @@ describe('builder: builder generate', () => {
expect(path.resolve).toBeCalledTimes(1)
expect(path.resolve).toBeCalledWith('/var/nuxt/templates', 'views/loading', '@/app/template.vue.html')
expect(fs.exists).toBeCalledTimes(2)
expect(fs.exists).nthCalledWith(1, 'resolve(/var/nuxt/templates, views/loading, @/app/template.vue.html)')
expect(fs.exists).nthCalledWith(1, '/var/nuxt/templates/views/loading/@/app/template.vue.html')
expect(fs.exists).nthCalledWith(2, 'resolveAlias(@/app/template.vue)')
expect(templateFiles).toEqual([{
custom: true,
@ -294,7 +295,7 @@ describe('builder: builder generate', () => {
expect(path.resolve).toBeCalledTimes(1)
expect(path.resolve).toBeCalledWith('/var/nuxt/templates', 'views/loading', '@/app/empty.vue.html')
expect(fs.exists).toBeCalledTimes(2)
expect(fs.exists).nthCalledWith(1, 'resolve(/var/nuxt/templates, views/loading, @/app/empty.vue.html)')
expect(fs.exists).nthCalledWith(1, '/var/nuxt/templates/views/loading/@/app/empty.vue.html')
expect(fs.exists).nthCalledWith(2, 'resolveAlias(@/app/empty.vue)')
expect(consola.error).toBeCalledTimes(1)
expect(consola.error).toBeCalledWith('Could not fetch loading indicator: @/app/empty.vue')
@ -436,7 +437,7 @@ describe('builder: builder generate', () => {
expect(path.resolve).toBeCalledTimes(1)
expect(path.resolve).toBeCalledWith('/var/nuxt/src', '/var/nuxt/src/layouts')
expect(fs.exists).toBeCalledTimes(1)
expect(fs.exists).toBeCalledWith('resolve(/var/nuxt/src, /var/nuxt/src/layouts)')
expect(fs.exists).toBeCalledWith('/var/nuxt/src/layouts')
expect(builder.resolveFiles).toBeCalledTimes(1)
expect(builder.resolveFiles).toBeCalledWith('/var/nuxt/src/layouts')
expect(builder.relativeToBuild).toBeCalledTimes(2)
@ -503,7 +504,7 @@ describe('builder: builder generate', () => {
expect(path.resolve).toBeCalledTimes(1)
expect(path.resolve).toBeCalledWith('/var/nuxt/src', '/var/nuxt/src/layouts')
expect(fs.exists).toBeCalledTimes(1)
expect(fs.exists).toBeCalledWith('resolve(/var/nuxt/src, /var/nuxt/src/layouts)')
expect(fs.exists).toBeCalledWith('/var/nuxt/src/layouts')
expect(builder.resolveFiles).not.toBeCalled()
expect(fs.mkdirp).not.toBeCalled()
})