vivo-hap-toolkit
Version:
A command line toolkit for developing Quick Apps.
3 lines (2 loc) • 2.26 kB
JavaScript
;const path=require("path"),webpack=require("webpack"),chokidar=require("chokidar"),{setCustomConfig:setCustomConfig,colorconsole:colorconsole,debounce:debounce}=require("@vivo-hap-toolkit/shared-utils"),globalConfig=require("@vivo-hap-toolkit/shared-utils/config"),genWebpackConf=require("../../gen-webpack-conf");let manifestPath="",watching=null,manifestWatcher=null;function showVersion(){const e=require("../../package.json").version,o=require("@babel/core/package.json").version,t=require("webpack/package.json").version;colorconsole.info(`hap-toolkit: ${e}; babel: ${o}; webpack: ${t};`)}showVersion(),module.exports.compile=function e(o,t,r,n={}){return new Promise((s,a)=>{function c(e,o){const t=n.onerror||function(){};let r;if(e&&(colorconsole.error(e),r=e),o&&o.hasErrors()){let e;o.compilation.errors.forEach(o=>{e=o.message;const t=/Can't resolve '(sass-loader|less-loader|stylus-loader)'/.exec(e);if("ModuleNotFoundError"===o.name&&t){let o=t[1];"less-loader"===o?o=`less ${o}`:"sass-loader"===o?o=`node-sass ${o}`:"stylus-loader"===o&&(o=`stylus ${o}`),e+=` 缺少依赖: ${o}, 请执行 \`npm install -D ${o}\` 安装相应依赖 `}colorconsole.error("构建错误",e)}),r=new Error(e)}r&&t(r)}colorconsole.init(n.log),setCustomConfig(n.cwd),manifestPath=path.join(globalConfig.projectPath,globalConfig.sourceRoot,"manifest.json"),process.env.NODE_PLATFORM=o,process.env.NODE_PHASE=t;const i="prod"===t?"production":"development";try{const l=genWebpackConf(n,i);if(r){const a=webpack(l);watching=a.watch({aggregateTimeout:300},(e,o)=>{c(e,o),s({compileError:e,stats:o,watching:watching,manifestWatcher:manifestWatcher})}),(manifestWatcher=chokidar.watch(manifestPath)).on("change",debounce(function(){colorconsole.info("### App Server ### manifest.json 文件修改,编译重启"),manifestWatcher.unwatch(manifestPath),manifestWatcher.close(),watching.close(()=>{e(o,t,r,n)})},300))}else webpack(l,(e,o)=>{c(e,o),s({compileError:e,stats:o})})}catch(e){a(e)}})},module.exports.stopWatch=function(){return new Promise(e=>{if(watching)return manifestWatcher.unwatch(manifestPath),manifestWatcher.close(),void watching.close(()=>{e({stopWatchError:null})});e({stopWatchError:"no watching"})})};
//# sourceMappingURL=compile.js.map