Skip to content

Commit 6d4a207

Browse files
committed
fix(cli): checking if a modules is in main package should consider the npm file
1 parent 0f637ac commit 6d4a207

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

packages/wxa-cli/package-lock.json

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/wxa-cli/src/const/defaultWxaConfigs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default class DefaultWxaConfigs {
3636
],
3737
optimization: {
3838
splitDeps: {
39-
maxDeps: 1,
39+
maxDeps: -1,
4040
},
4141
},
4242
};

packages/wxa-cli/src/optimizer/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class Optimizer {
1717
this.cmdOptions = cmdOptions;
1818
this.cwd = cwd;
1919
this.progress = new ProgressBar(cwd, wxaConfigs);
20-
this.splitDeps = new SplitDeps(appConfigs, wxaConfigs);
20+
this.splitDeps = new SplitDeps({appConfigs, wxaConfigs, cwd, cmdOptions});
2121
}
2222

2323
async run(indexedMap, appConfigs) {

packages/wxa-cli/src/optimizer/splitDeps.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import path from 'path';
2+
import logger from '../helpers/logger';
23

34
export default class SplitDeps {
4-
constructor(appConfigs, wxaConfigs) {
5+
constructor({appConfigs, wxaConfigs, cwd, cmdOptions}) {
6+
this.cmdOptions = cmdOptions;
57
this.wxaConfigs = wxaConfigs;
68
this.maxSplitDeps = wxaConfigs.optimization.splitDeps.maxDeps;
7-
this.NMReg = new RegExp('node_modules');
9+
this.NMReg = new RegExp(path.join(cwd, 'node_modules'));
810

911
let pkg = appConfigs.subpackages || appConfigs.subPackages;
1012
if (pkg) {
@@ -59,14 +61,32 @@ export default class SplitDeps {
5961
this.getReferenceSize(child) > this.maxSplitDeps
6062
) return;
6163

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

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

73+
isInMainPackage(child) {
74+
let refs = Array.from(child.reference);
75+
let inMain = [];
76+
refs.forEach(([src, mdl])=>{
77+
// if a child module has neigth subpage nor node_modules reference, then it's in main package
78+
if (
79+
!this.subPages.some((sub)=>sub.reg.test(src)) &&
80+
!this.NMReg.test(src)
81+
) {
82+
inMain.push(true);
83+
} else {
84+
inMain.push(false);
85+
}
86+
});
87+
return inMain.some((item)=>item);
88+
}
89+
7090
trackChildNodes(dep, parent, subpage) {
7191
// depth-first
7292
let {path: pkg} = subpage;
@@ -97,7 +117,7 @@ export default class SplitDeps {
97117
});
98118

99119
// clean multi output
100-
if (dep.output.has(outputPath)) dep.output.delete(outputPath);
120+
if (dep.output.has(outputPath) && !this.isInMainPackage(dep)) dep.output.delete(outputPath);
101121

102122
// update output path
103123
dep.output.add(newOutputPath);

0 commit comments

Comments
 (0)