Skip to content

Commit

Permalink
Add DependencyMode.PRUNE_ALLOW_NO_ENTRY_POINTS
Browse files Browse the repository at this point in the history
We don't expect this to be broadly useful outside of some niche Google internal use cases, and generally recommend another DependencyMode.

PiperOrigin-RevId: 596618416
  • Loading branch information
lauraharker authored and copybara-github committed Jan 8, 2024
1 parent 2d396c3 commit 32dda7f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/com/google/javascript/jscomp/DependencyOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@ public enum DependencyMode {
*
* <p>All entry points must be explicitly defined.
*/
PRUNE;
PRUNE,

/**
* Identical to PRUNE unless there are no entry points specifified, in which case drops all
* other input files instead of crashing with an error about invalid flag usage.
*
* <p>PRUNE mode will report an error if used without a specified entry point because that
* usually indicates user error, where the user actually wants pruning starting from one/more
* entry points.
*
* <p>This mode is only recommended for use when using some macro to generate JSCompiler flags
* for a variety of targets, some of which have entry points and some of which do not.
*/
PRUNE_ALLOW_NO_ENTRY_POINTS;
}

/** Returns the dependency management mode. */
Expand Down Expand Up @@ -95,7 +108,9 @@ public boolean shouldSort() {
* dependency of an entry point. Otherwise, all input files should be included.
*/
public boolean shouldPrune() {
return getMode() == DependencyMode.PRUNE_LEGACY || getMode() == DependencyMode.PRUNE;
return getMode() == DependencyMode.PRUNE_LEGACY
|| getMode() == DependencyMode.PRUNE
|| getMode() == DependencyMode.PRUNE_ALLOW_NO_ENTRY_POINTS;
}

/**
Expand All @@ -107,7 +122,8 @@ public boolean shouldPrune() {
* <p>If true, moochers should not be considered implicit entry points.
*/
public boolean shouldDropMoochers() {
return getMode() == DependencyMode.PRUNE;
return getMode() == DependencyMode.PRUNE
|| getMode() == DependencyMode.PRUNE_ALLOW_NO_ENTRY_POINTS;
}

/** Returns a {@link DependencyOptions} using the {@link DependencyMode#NONE} mode. */
Expand All @@ -133,6 +149,15 @@ public static DependencyOptions pruneLegacyForEntryPoints(
DependencyMode.PRUNE_LEGACY, ImmutableList.copyOf(entryPoints));
}

/**
* Returns a {@link DependencyOptions} using the {@link DependencyMode#PRUNE} mode with the given
* entry points.
*/
public static DependencyOptions pruneAllowNoEntryPoints(Iterable<ModuleIdentifier> entryPoints) {
return new AutoValue_DependencyOptions(
DependencyMode.PRUNE_ALLOW_NO_ENTRY_POINTS, ImmutableList.copyOf(entryPoints));
}

/**
* Returns a {@link DependencyOptions} using the {@link DependencyMode#PRUNE} mode with the given
* entry points.
Expand Down Expand Up @@ -212,6 +237,8 @@ public static DependencyOptions pruneForEntryPoints(Iterable<ModuleIdentifier> e
DependencyMode dependencyMode;
if (dependencyModeFlag == DependencyMode.PRUNE || onlyClosureDependenciesFlag) {
dependencyMode = DependencyMode.PRUNE;
} else if (dependencyModeFlag == DependencyMode.PRUNE_ALLOW_NO_ENTRY_POINTS) {
dependencyMode = DependencyMode.PRUNE_ALLOW_NO_ENTRY_POINTS;
} else if (dependencyModeFlag == DependencyMode.PRUNE_LEGACY
|| manageClosureDependenciesFlag
|| hasEntryPoint) {
Expand Down Expand Up @@ -240,6 +267,8 @@ public static DependencyOptions pruneForEntryPoints(Iterable<ModuleIdentifier> e
return DependencyOptions.sortOnly();
case PRUNE_LEGACY:
return DependencyOptions.pruneLegacyForEntryPoints(entryPointsBuilder.build());
case PRUNE_ALLOW_NO_ENTRY_POINTS:
return DependencyOptions.pruneAllowNoEntryPoints(entryPointsBuilder.build());
case PRUNE:
return DependencyOptions.pruneForEntryPoints(entryPointsBuilder.build());
}
Expand Down
34 changes: 34 additions & 0 deletions test/com/google/javascript/jscomp/CommandLineRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,40 @@ public void testOnlyClosureDependenciesOneEntryPoint() {
});
}

@Test
public void testOnlyClosureDependenciesOneEntryPoint_pruneAllowNoEntryPoints() {
args.add("--dependency_mode=PRUNE_ALLOW_NO_ENTRY_POINTS");
args.add("--entry_point=goog:beer");
test(
new String[] {
"goog.require('beer'); var beerRequired = 1;",
"goog.provide('beer');\ngoog.require('hops');\nvar beerProvided = 1;",
"goog.provide('hops'); var hopsProvided = 1;",
"goog.provide('scotch'); var scotchProvided = 1;",
"goog.require('scotch');\nvar includeFileWithoutProvides = 1;",
"/** This is base.js @provideGoog */ var COMPILED = false;",
},
new String[] {
"var COMPILED = !1;",
"var hops = {}, hopsProvided = 1;",
"var beer = {}, beerProvided = 1;"
});
}

@Test
public void testPruneAllowNoEntryPoints_noEntryPointsPrunesEverything() {
args.add("--dependency_mode=PRUNE_ALLOW_NO_ENTRY_POINTS");
test(
new String[] {
"goog.require('beer'); var beerRequired = 1;",
"goog.provide('beer');\ngoog.require('hops');\nvar beerProvided = 1;",
"goog.provide('hops'); var hopsProvided = 1;",
"goog.provide('scotch'); var scotchProvided = 1;",
"goog.require('scotch');\nvar includeFileWithoutProvides = 1;",
},
new String[] {});
}

@Test
public void testSourceMapExpansion1() {
args.add("--js_output_file");
Expand Down

0 comments on commit 32dda7f

Please sign in to comment.