| 1 | /* |
| 2 | * Copyright 2011 The Closure Compiler Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.javascript.jscomp; |
| 18 | |
| 19 | import com.google.common.collect.Sets; |
| 20 | |
| 21 | import java.io.Serializable; |
| 22 | import java.util.Collection; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | /** |
| 26 | * Options for how to manage dependencies between input files. |
| 27 | * |
| 28 | * Dependency information is usually pulled out from the JS code by |
| 29 | * looking for primitive dependency functions (like Closure Library's |
| 30 | * goog.provide/goog.require). Analysis of this dependency information is |
| 31 | * controlled by {@code CodingConvention}, which lets you define those |
| 32 | * dependency primitives. |
| 33 | * |
| 34 | * This options class determines how we use that dependency information |
| 35 | * to change how code is built. |
| 36 | * |
| 37 | * @author [email protected] (Nick Santos) |
| 38 | */ |
| 39 | public class DependencyOptions implements Serializable { |
| 40 | private static final long serialVersionUID = 1L; |
| 41 | |
| 42 | private boolean sortDependencies = false; |
| 43 | private boolean pruneDependencies = false; |
| 44 | private boolean dropMoochers = false; |
| 45 | private final Set<String> entryPoints = Sets.newHashSet(); |
| 46 | |
| 47 | /** |
| 48 | * Enables or disables dependency sorting mode. |
| 49 | * |
| 50 | * If true, we will sort the input files based on dependency information |
| 51 | * in them. Otherwise, we will use the order of files specified |
| 52 | * on the command-line. |
| 53 | * @return this for easy building. |
| 54 | */ |
| 55 | public DependencyOptions setDependencySorting(boolean enabled) { |
| 56 | this.sortDependencies = enabled; |
| 57 | return this; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Enables or disables dependency pruning mode. |
| 62 | * |
| 63 | * In dependency pruning mode, we will look for all files that provide a |
| 64 | * symbol. Unless that file is a transitive dependency of a file that |
| 65 | * we're using, we will remove it from the compilation job. |
| 66 | * |
| 67 | * This does not affect how we handle files that do not provide symbols. |
| 68 | * See setMoocherDropping for information on how these are handled. |
| 69 | * |
| 70 | * @return this for easy chaining. |
| 71 | */ |
| 72 | public DependencyOptions setDependencyPruning(boolean enabled) { |
| 73 | this.pruneDependencies = enabled; |
| 74 | return this; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Enables or disables moocher dropping mode. |
| 79 | * |
| 80 | * A 'moocher' is a file that does not provide any symbols (though they |
| 81 | * may require symbols). This is usually because they don't want to |
| 82 | * tie themselves to a particular dependency system (e.g., Closure's |
| 83 | * goog.provide, CommonJS modules). So they rely on other people to |
| 84 | * manage dependencies on them. |
| 85 | * |
| 86 | * If true, we drop these files when we prune dependencies. |
| 87 | * If false, we always keep these files an anything they depend on. |
| 88 | * The default is false. |
| 89 | * |
| 90 | * Notice that this option only makes sense if dependency pruning is on, |
| 91 | * and a set of entry points is specified. |
| 92 | * |
| 93 | * @return this for easy chaining. |
| 94 | */ |
| 95 | public DependencyOptions setMoocherDropping(boolean enabled) { |
| 96 | this.dropMoochers = enabled; |
| 97 | return this; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Adds a collection of symbols to always keep. |
| 102 | * |
| 103 | * In dependency pruning mode, we will automatically keep all the |
| 104 | * transitive dependencies of these symbols. |
| 105 | * |
| 106 | * The syntactic form of a symbol depends on the type of dependency |
| 107 | * primitives we're using. For example, goog.provide('foo.bar') |
| 108 | * provides the symbol 'foo.bar'. |
| 109 | * |
| 110 | * Entry points can be scoped to a module by specifying 'mod2:foo.bar'. |
| 111 | * |
| 112 | * @return this for easy chaining. |
| 113 | */ |
| 114 | public DependencyOptions setEntryPoints(Collection<String> symbols) { |
| 115 | entryPoints.clear(); |
| 116 | entryPoints.addAll(symbols); |
| 117 | return this; |
| 118 | } |
| 119 | |
| 120 | /** Returns whether re-ordering of files is needed. */ |
| 121 | boolean needsManagement() { |
| 122 | return sortDependencies || pruneDependencies; |
| 123 | } |
| 124 | |
| 125 | boolean shouldSortDependencies() { |
| 126 | return sortDependencies; |
| 127 | } |
| 128 | |
| 129 | boolean shouldPruneDependencies() { |
| 130 | return pruneDependencies; |
| 131 | } |
| 132 | |
| 133 | boolean shouldDropMoochers() { |
| 134 | return pruneDependencies && dropMoochers; |
| 135 | } |
| 136 | |
| 137 | Collection<String> getEntryPoints() { |
| 138 | return entryPoints; |
| 139 | } |
| 140 | } |