|
24 | 24 | import com.google.javascript.jscomp.CompilerOptions.ChunkOutputType;
|
25 | 25 | import com.google.javascript.jscomp.CompilerOptions.PropertyCollapseLevel;
|
26 | 26 | import com.google.javascript.jscomp.deps.ModuleLoader.ResolutionMode;
|
| 27 | +import com.google.javascript.jscomp.testing.JSChunkGraphBuilder; |
27 | 28 | import com.google.javascript.jscomp.testing.TestExternsBuilder;
|
28 | 29 | import com.google.javascript.rhino.Node;
|
29 | 30 | import com.google.javascript.rhino.testing.NodeSubject;
|
@@ -3298,4 +3299,39 @@ public void testHookGetProp2() {
|
3298 | 3299 | "var xid$internal_ = function() {};",
|
3299 | 3300 | "var xx = ((x ? xid : abc) - def).toExponential(5);"));
|
3300 | 3301 | }
|
| 3302 | + |
| 3303 | + @Test |
| 3304 | + public void conditionallyLoadedChunk() { |
| 3305 | + JSChunk[] chunks = |
| 3306 | + JSChunkGraphBuilder.forChain() |
| 3307 | + // m1, always loaded |
| 3308 | + .addChunk( |
| 3309 | + lines( |
| 3310 | + "const registry = {};", |
| 3311 | + "const factories = {};", |
| 3312 | + "factories.one = function() {};", |
| 3313 | + "", |
| 3314 | + "function build() {", |
| 3315 | + " const mod = registry.mod;", |
| 3316 | + " if (mod) mod();", |
| 3317 | + "}")) |
| 3318 | + // m2, conditionally loaded after m1 |
| 3319 | + .addChunk("registry.mod = factories.one;") |
| 3320 | + .build(); |
| 3321 | + |
| 3322 | + // TODO - b/385132629: don't inline the definition of registry.mod into build(). If the mod |
| 3323 | + // chunk is not loaded, then registry.mod will be undefined when build() is called, so it's |
| 3324 | + // wrong to always call `factories$one` in build(). |
| 3325 | + test( |
| 3326 | + srcs(chunks[0], chunks[1]), |
| 3327 | + expected( |
| 3328 | + lines( |
| 3329 | + "var factories$one = function() {};", |
| 3330 | + "", |
| 3331 | + "function build() {", |
| 3332 | + " const mod = null;", |
| 3333 | + " if (factories$one) factories$one();", |
| 3334 | + "}"), |
| 3335 | + "var registry$mod = null;")); |
| 3336 | + } |
3301 | 3337 | }
|
0 commit comments