mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-03 19:07:15 +00:00
parent
12ca1ce37d
commit
7f542e08e5
@ -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) || {}
|
||||
}
|
||||
}))
|
||||
|
@ -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()
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user