( bundle: BundleImport, newInverseProbability: number, seen?: Set<BundleImport> )
| 151 | * We also limit "organic" probability to 98% so they don't get unlimited preloads. |
| 152 | */ |
| 153 | export const adjustProbabilities = ( |
| 154 | bundle: BundleImport, |
| 155 | newInverseProbability: number, |
| 156 | seen?: Set<BundleImport> |
| 157 | ) => { |
| 158 | if (seen?.has(bundle)) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | const previousInverseProbability = bundle.$inverseProbability$; |
| 163 | bundle.$inverseProbability$ = newInverseProbability; |
| 164 | // Don't propagate tiny changes |
| 165 | if (previousInverseProbability - bundle.$inverseProbability$ < 0.01) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | if ( |
| 170 | // don't queue until we have initialized the preloader |
| 171 | base != null && |
| 172 | bundle.$state$ < BundleImportState_Preload |
| 173 | ) { |
| 174 | if (bundle.$state$ === BundleImportState_None) { |
| 175 | bundle.$state$ = BundleImportState_Queued; |
| 176 | queue.push(bundle); |
| 177 | config.$DEBUG$ && |
| 178 | log(`queued ${Math.round((1 - bundle.$inverseProbability$) * 100)}%`, bundle.$name$); |
| 179 | } |
| 180 | |
| 181 | // It's in the queue, so we need to re-sort it |
| 182 | queueDirty = true; |
| 183 | } |
| 184 | |
| 185 | if (bundle.$deps$) { |
| 186 | seen ||= new Set(); |
| 187 | seen.add(bundle); |
| 188 | const probability = 1 - bundle.$inverseProbability$; |
| 189 | for (const dep of bundle.$deps$) { |
| 190 | const depBundle = getBundle(dep.$name$)!; |
| 191 | if (depBundle.$inverseProbability$ === 0) { |
| 192 | // it's already at max probability |
| 193 | continue; |
| 194 | } |
| 195 | /** |
| 196 | * The chance that a dep won't be loaded is 1-(the chance that the dep will be loaded)*(the |
| 197 | * chance that the current bundle will be loaded). |
| 198 | * |
| 199 | * We can multiply this chance together with all other bundle adjustments to get the chance |
| 200 | * that a dep will be loaded given all the chances of the other bundles. |
| 201 | * |
| 202 | * But when we're very likely to load the current bundle, make the dynamic imports very likely |
| 203 | * too. |
| 204 | */ |
| 205 | let newInverseProbability: number; |
| 206 | if (probability === 1 || (probability >= 0.99 && depsCount < 100)) { |
| 207 | depsCount++; |
| 208 | // we're loaded at max probability, so elevate dynamic imports to 99% sure |
| 209 | newInverseProbability = Math.min(0.01, 1 - dep.$importProbability$); |
| 210 | } else { |
no test coverage detected
searching dependent graphs…