From 8ffe4412679bafdfd6ef283afbc7b4e511df376d Mon Sep 17 00:00:00 2001 From: lichengyin Date: Thu, 12 Nov 2015 17:17:02 +0800 Subject: [PATCH] show error page when babel compile error --- src/core/app.js | 6 ++++++ src/util/watch_compile.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/core/app.js b/src/core/app.js index e412f5a4..c27f5e41 100644 --- a/src/core/app.js +++ b/src/core/app.js @@ -90,6 +90,12 @@ export default class extends think.http.base { //set module config, can not set config in request this.http._config = think.getModuleConfig(this.http.module); + //babel compile error + if(think.compileError){ + this.http.error = think.compileError; + return think.statusAction(500, this.http); + } + await this.hook('logic_before'); await this.execLogic().catch(err => { //ignore prevent reject promise diff --git a/src/util/watch_compile.js b/src/util/watch_compile.js index 1f2385a8..a0fce182 100644 --- a/src/util/watch_compile.js +++ b/src/util/watch_compile.js @@ -16,6 +16,11 @@ export default class extends think.base { * @type {Object} */ compiledMtime = {}; + /** + * compiled error files + * @type {Array} + */ + compiledErrorFiles = []; /** * init * @param {String} srcPath [] @@ -46,6 +51,7 @@ export default class extends think.base { fs.writeFileSync(`${this.outPath}/${file}`, content); return; } + let startTime = Date.now(); try{ let data = babel.transform(content, { @@ -61,11 +67,16 @@ export default class extends think.base { } think.mkdir(path.dirname(`${this.outPath}/${file}`)); fs.writeFileSync(`${this.outPath}/${file}`, data.code); + return true; }catch(e){ + think.log(colors => { return colors.red(`compile file ${file} error`); }, 'BABEL'); think.log(e); + + e.message = 'Babel Compile Error: ' + e.message; + think.compileError = e; } } /** @@ -75,6 +86,11 @@ export default class extends think.base { compile(){ let files = think.getFiles(this.srcPath); let changedFiles = []; + + if(!this.compiledErrorFiles.length){ + think.compileError = null; + } + files.forEach(file => { let extname = path.extname(file); //if is not js file, only copy @@ -92,10 +108,18 @@ export default class extends think.base { } } if(!this.compiledMtime[file] || mTime > this.compiledMtime[file]){ - this.compileFile(file); + let ret = this.compileFile(file); changedFiles.push(path.normalize(`${this.outPath}/${file}`)); this.compiledMtime[file] = mTime; - return; + + if(ret){ + let index = this.compiledErrorFiles.indexOf(file); + if(index > -1){ + this.compiledErrorFiles.splice(index, 1); + } + }else{ + this.compiledErrorFiles.push(file); + } } }); //notify auto reload service to clear file cache