From 51618bef29bbd24129fe6673b0181eae30e59794 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 16 May 2017 02:17:58 +0430 Subject: [PATCH] Allow override anything using a file with same name in srcDir/app --- lib/build.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/build.js b/lib/build.js index 054b870470..01ef111755 100644 --- a/lib/build.js +++ b/lib/build.js @@ -241,13 +241,27 @@ async function generateRoutesAndFiles () { if (this.options.store) { templatesFiles.push('store.js') } - // Resolve all internal template files relative to app directory - templatesFiles = templatesFiles.map(file => { return {src: r(__dirname, 'app', file), dst: file} }) + // Resolve template files + templatesFiles = templatesFiles.map(file => { + // Allow override anything using a file with same name in srcDir/app + const customPath = r(this.srcDir, 'app', file) + const customFileExists = fs.existsSync(customPath) + return { + src: customFileExists ? customPath : r(__dirname, 'app', file), + dst: file, + custom: customFileExists + } + }) // Add external template files (used in modules) if (Array.isArray(this.options.build.templates)) { - templatesFiles = templatesFiles.concat(this.options.build.templates) + templatesFiles = templatesFiles.concat(this.options.build.templates.map(t => { + return Object.assign({custom: true}, t) + })) } - let moveTemplates = templatesFiles.map(({src, dst, options}) => { + let moveTemplates = templatesFiles.map(({src, dst, options, custom}) => { + // Add template to watchers + this.options.build.watch.push(src) + // Render template to dst return readFile(src, 'utf8') .then((fileContent) => { const template = _.template(fileContent, { @@ -257,7 +271,10 @@ async function generateRoutesAndFiles () { } }) const content = template(Object.assign({}, templateVars, { - options: options || {} + options: options || {}, + custom, + src, + dst })) const path = r(this.dir, '.nuxt', dst) return writeFile(path, content, 'utf8') @@ -518,7 +535,7 @@ function watchPages () { }) /* istanbul ignore next */ const refreshFiles = _.debounce(async () => { - await generateRoutesAndFiles.bind(this) + await generateRoutesAndFiles.call(this) }, 200) // Watch for internals this.pagesFilesWatcher = chokidar.watch(patterns, options)