Skip to content

Commit

Permalink
fix(cli): checking if a modules is in main package should consider th…
Browse files Browse the repository at this point in the history
…e npm file
  • Loading branch information
Genuifx committed Jul 10, 2019
1 parent 0f637ac commit 6d4a207
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
22 changes: 11 additions & 11 deletions packages/wxa-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/wxa-cli/src/const/defaultWxaConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class DefaultWxaConfigs {
],
optimization: {
splitDeps: {
maxDeps: 1,
maxDeps: -1,
},
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/wxa-cli/src/optimizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class Optimizer {
this.cmdOptions = cmdOptions;
this.cwd = cwd;
this.progress = new ProgressBar(cwd, wxaConfigs);
this.splitDeps = new SplitDeps(appConfigs, wxaConfigs);
this.splitDeps = new SplitDeps({appConfigs, wxaConfigs, cwd, cmdOptions});
}

async run(indexedMap, appConfigs) {
Expand Down
30 changes: 25 additions & 5 deletions packages/wxa-cli/src/optimizer/splitDeps.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import path from 'path';
import logger from '../helpers/logger';

export default class SplitDeps {
constructor(appConfigs, wxaConfigs) {
constructor({appConfigs, wxaConfigs, cwd, cmdOptions}) {
this.cmdOptions = cmdOptions;
this.wxaConfigs = wxaConfigs;
this.maxSplitDeps = wxaConfigs.optimization.splitDeps.maxDeps;
this.NMReg = new RegExp('node_modules');
this.NMReg = new RegExp(path.join(cwd, 'node_modules'));

let pkg = appConfigs.subpackages || appConfigs.subPackages;
if (pkg) {
Expand Down Expand Up @@ -59,14 +61,32 @@ export default class SplitDeps {
this.getReferenceSize(child) > this.maxSplitDeps
) return;

let isInMainPackage = Array.from(child.reference).some(([src, mdl])=>!this.subPages.some((sub)=>sub.reg.test(mdl.src)));
if (isInMainPackage) return;
if ( child.pret.isWXALib ) return;

if (this.isInMainPackage(child)) return;
if (this.cmdOptions.verbose) logger.info('Find NPM need track to subpackages', child.src);
// fulfill all condition just track all the sub-nodes without any hesitate.
this.trackChildNodes(child, {output: dep.meta.outputPath, originOutput: dep.meta.outputPath, instance: dep}, pkg);
});
}

isInMainPackage(child) {
let refs = Array.from(child.reference);
let inMain = [];
refs.forEach(([src, mdl])=>{
// if a child module has neigth subpage nor node_modules reference, then it's in main package
if (
!this.subPages.some((sub)=>sub.reg.test(src)) &&
!this.NMReg.test(src)
) {
inMain.push(true);
} else {
inMain.push(false);
}
});
return inMain.some((item)=>item);
}

trackChildNodes(dep, parent, subpage) {
// depth-first
let {path: pkg} = subpage;
Expand Down Expand Up @@ -97,7 +117,7 @@ export default class SplitDeps {
});

// clean multi output
if (dep.output.has(outputPath)) dep.output.delete(outputPath);
if (dep.output.has(outputPath) && !this.isInMainPackage(dep)) dep.output.delete(outputPath);

// update output path
dep.output.add(newOutputPath);
Expand Down

0 comments on commit 6d4a207

Please sign in to comment.