|
1 | 1 | import path from 'path';
|
| 2 | +import logger from '../helpers/logger'; |
2 | 3 |
|
3 | 4 | export default class SplitDeps {
|
4 |
| - constructor(appConfigs, wxaConfigs) { |
| 5 | + constructor({appConfigs, wxaConfigs, cwd, cmdOptions}) { |
| 6 | + this.cmdOptions = cmdOptions; |
5 | 7 | this.wxaConfigs = wxaConfigs;
|
6 | 8 | this.maxSplitDeps = wxaConfigs.optimization.splitDeps.maxDeps;
|
7 |
| - this.NMReg = new RegExp('node_modules'); |
| 9 | + this.NMReg = new RegExp(path.join(cwd, 'node_modules')); |
8 | 10 |
|
9 | 11 | let pkg = appConfigs.subpackages || appConfigs.subPackages;
|
10 | 12 | if (pkg) {
|
@@ -59,14 +61,32 @@ export default class SplitDeps {
|
59 | 61 | this.getReferenceSize(child) > this.maxSplitDeps
|
60 | 62 | ) return;
|
61 | 63 |
|
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; |
64 | 65 |
|
| 66 | + if (this.isInMainPackage(child)) return; |
| 67 | + if (this.cmdOptions.verbose) logger.info('Find NPM need track to subpackages', child.src); |
65 | 68 | // fulfill all condition just track all the sub-nodes without any hesitate.
|
66 | 69 | this.trackChildNodes(child, {output: dep.meta.outputPath, originOutput: dep.meta.outputPath, instance: dep}, pkg);
|
67 | 70 | });
|
68 | 71 | }
|
69 | 72 |
|
| 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 | + |
70 | 90 | trackChildNodes(dep, parent, subpage) {
|
71 | 91 | // depth-first
|
72 | 92 | let {path: pkg} = subpage;
|
@@ -97,7 +117,7 @@ export default class SplitDeps {
|
97 | 117 | });
|
98 | 118 |
|
99 | 119 | // 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); |
101 | 121 |
|
102 | 122 | // update output path
|
103 | 123 | dep.output.add(newOutputPath);
|
|
0 commit comments