Skip to content

Commit

Permalink
fix(cli): use color to specify which modules is changed
Browse files Browse the repository at this point in the history
fix watch mode bug, using color to specify which modules is changed
  • Loading branch information
Genuifx committed Jul 9, 2019
1 parent e3b6543 commit 167d615
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
16 changes: 14 additions & 2 deletions packages/wxa-cli/src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,14 @@ class Builder {
let changedDeps;
try {
this.schedule.$depPending.push(mdl);

if (mdl.childNodes && mdl.childNodes.size) this.walkChildNodesTreeAndMark(mdl);
await this.hooks.rebuildModule.promise(this.schedule, mdl);

changedDeps = await this.schedule.$doDPA();
await this.optimizeAndGenerate(changedDeps, cmd);

let map = new Map(changedDeps.map((mdl)=>[mdl.src, mdl]));

await this.optimizeAndGenerate(map, this.schedule.appConfigs, cmd);
} catch (e) {
logger.error('编译失败', e);
}
Expand Down Expand Up @@ -173,6 +176,15 @@ class Builder {
process.on('SIGHUP', h);
}

walkChildNodesTreeAndMark(mdl) {
// mark tree nodes as changed
mdl.childNodes.forEach((child)=>{
child.color = color.CHANGED;

if (child.childNodes && child.childNodes.size) this.walkChildNodesTreeAndMark(child);
});
}

async build(cmd) {
if (cmd.verbose) logger.info('WxaConfigs', this.wxaConfigs);

Expand Down
16 changes: 14 additions & 2 deletions packages/wxa-cli/src/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import globby from 'globby';
import debugPKG from 'debug';
import {SyncHook} from 'tapable';

import {readFile, isFile} from './utils';
import {readFile, isFile, getHash, getHashWithString} from './utils';
import bar from './helpers/progressBar';
import logger from './helpers/logger';
import COLOR from './const/color';
Expand Down Expand Up @@ -190,6 +190,7 @@ class Schedule {

let oldPages = new Map(this.$pageArray.entries());
let newPages = this.addPageEntryPoint();
newPages = new Map(newPages.map((page)=>[page.src, page]));
this.cleanUpPages(newPages, oldPages);
}

Expand Down Expand Up @@ -285,6 +286,7 @@ class Schedule {

// the amount of child output is decided by his parent module.
// normally one, emit multi while child module is npm package.
// debugger;
let child = {
...dep,
color: COLOR.INIT,
Expand All @@ -298,6 +300,16 @@ class Schedule {
if (this.$indexOfModule.has(dep.src)) {
let indexedModule = this.$indexOfModule.get(dep.src);

// check hash
child.hash = !child.isAbstract && child.content ? getHashWithString(child.content) : getHash(child.src);

if (child.hash !== indexedModule.hash) {
// module changed, clean up mdl.
indexedModule.content = child.content;
indexedModule.code = void(0);
indexedModule.hash = child.hash;
}

// merge reference, cause the module is parsed
if (indexedModule.reference instanceof Map) {
indexedModule.reference.set(mdl.src, mdl);
Expand All @@ -312,7 +324,7 @@ class Schedule {

child = indexedModule;
}

// debugger;
if (child.color !== COLOR.COMPILED) this.$depPending.push(child);
if (child.color === COLOR.INIT) this.$indexOfModule.set(child.src, child);

Expand Down
4 changes: 4 additions & 0 deletions packages/wxa-cli/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,7 @@ export function getHash(filepath) {

return content == null ? Date.now() : crypto.createHash('md5').update(content).digest('hex');
}

export function getHashWithString(content) {
return content == null ? Date.now() : crypto.createHash('md5').update(content).digest('hex');
}

0 comments on commit 167d615

Please sign in to comment.