From eeaba5eb50b110ad44e4cc0c42322bb6f28773ee Mon Sep 17 00:00:00 2001 From: lichengyin Date: Fri, 6 Nov 2015 09:57:10 +0800 Subject: [PATCH] move auto compile code to file util/watch_compile.js --- src/index.js | 75 ++----------------------- src/util/watch_compile.js | 115 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 70 deletions(-) create mode 100644 src/util/watch_compile.js diff --git a/src/index.js b/src/index.js index dc33eb36..2eb8764a 100644 --- a/src/index.js +++ b/src/index.js @@ -4,9 +4,7 @@ import fs from 'fs'; import path from 'path'; import './core/think.js'; -//babel not export default property -//so can not use `import babel from 'babel-core'` -let babel = require('babel-core'); + export default class { /** @@ -514,73 +512,10 @@ export default class { * @return {} [] */ compile(srcPath, outPath, log){ - if(srcPath === true){ - log = true; - srcPath = ''; - } - srcPath = srcPath || `${think.ROOT_PATH}/src`; - outPath = outPath || `${think.ROOT_PATH}/app`; - - if(!think.isDir(srcPath)){ - return; - } - - think.autoCompile = true; - - //store compiled files last mtime - let compiledMtime = {}; - - //compile single file - let compileFile = (file, onlyCopy) => { - let filePath = `${srcPath}/${file}`; - let content = fs.readFileSync(filePath, 'utf8'); - if(!content){ - return; - } - if(onlyCopy){ - fs.writeFileSync(`${outPath}/${file}`, content); - return; - } - let startTime = Date.now(); - try{ - let data = babel.transform(content, { - retainLines: true, - stage: 0, - modules: 'common', - loose: true, - optional: 'runtime' - }); - if(log){ - think.log(`compile file ${file}`, 'BABEL', startTime); - } - think.mkdir(path.dirname(`${outPath}/${file}`)); - fs.writeFileSync(`${outPath}/${file}`, data.code); - }catch(e){ - think.log(colors => { - return colors.red(`compile file ${file} error`); - }, 'BABEL'); - think.log(e); - } - }; - - let fn = () => { - let files = think.getFiles(srcPath); - files.forEach(function(file){ - var extname = path.extname(file); - if(extname !== '.js'){ - compileFile(file, true); - return; - } - var mTime = fs.statSync(`${srcPath}/${file}`).mtime.getTime(); - if(!compiledMtime[file] || mTime > compiledMtime[file]){ - compileFile(file); - compiledMtime[file] = mTime; - return; - } - }); - }; - - setInterval(fn, 100); + let WatchCompile = require('./util/watch_compile.js'); + let instance = new WatchCompile(srcPath, outPath, log); + let flag = instance.run(); + think.autoCompile = flag; } /** * load, convenient for plugins diff --git a/src/util/watch_compile.js b/src/util/watch_compile.js new file mode 100644 index 00000000..7fc28052 --- /dev/null +++ b/src/util/watch_compile.js @@ -0,0 +1,115 @@ +'use strict'; + +import fs from 'fs'; +import path from 'path'; + +//babel not export default property +//so can not use `import babel from 'babel-core'` +let babel = require('babel-core'); + +/** + * watch compile + */ +export default class extends think.base { + /** + * store compiled files last mtime + * @type {Object} + */ + compiledMtime = {}; + /** + * init + * @param {String} srcPath [] + * @param {String} outPath [] + * @param {Boolean} log [] + * @return {} [] + */ + init(srcPath, outPath, log){ + if(srcPath === true){ + log = true; + srcPath = ''; + } + srcPath = srcPath || `${think.ROOT_PATH}/src`; + outPath = outPath || `${think.ROOT_PATH}/app`; + this.srcPath = srcPath; + this.outPath = outPath; + this.log = log; + } + /** + * compile single file + * @param {String} file [] + * @param {Boolean} onlyCopy [] + * @return {} [] + */ + compileFile(file, onlyCopy){ + let filePath = `${this.srcPath}/${file}`; + let content = fs.readFileSync(filePath, 'utf8'); + + if(!content){ + return; + } + if(onlyCopy){ + fs.writeFileSync(`${this.outPath}/${file}`, content); + return; + } + let startTime = Date.now(); + try{ + let data = babel.transform(content, { + retainLines: true, + stage: 0, + modules: 'common', + loose: true, + optional: 'runtime' + }); + if(this.log){ + think.log(`compile file ${file}`, 'BABEL', startTime); + } + think.mkdir(path.dirname(`${this.outPath}/${file}`)); + fs.writeFileSync(`${this.outPath}/${file}`, data.code); + }catch(e){ + think.log(colors => { + return colors.red(`compile file ${file} error`); + }, 'BABEL'); + think.log(e); + } + } + /** + * compile + * @return {} [] + */ + compile(){ + let files = think.getFiles(this.srcPath); + files.forEach(file => { + let extname = path.extname(file); + //if is not js file, only copy + if(extname !== '.js'){ + this.compileFile(file, true); + return; + } + let mTime = fs.statSync(`${this.srcPath}/${file}`).mtime.getTime(); + let outFile = `${this.outPath}/${file}`; + if(think.isFile(outFile)){ + let outmTime = fs.statSync(outFile).mtime.getTime(); + //if compiled file mtime is after than source file, return + if(outmTime > mTime){ + return; + } + } + if(!this.compiledMtime[file] || mTime > this.compiledMtime[file]){ + this.compileFile(file); + this.compiledMtime[file] = mTime; + return; + } + }); + } + /** + * run + * @return {} [] + */ + run(){ + if(!think.isDir(this.srcPath)){ + return; + } + this.timer = setInterval(this.compile.bind(this), 100); + return true; + } +} \ No newline at end of file