Debian Codegraph @

util/build/transforms/optimizer/closure.js

raw symbols SCIP
1 define([
2 "../../buildControl",
3 "dojo/has!host-node?./sendJob:",
4 "../../fs",
5 "../../fileUtils",
6 "./stripConsole",
7 "dojo/_base/lang",
8 "dojo/has"
9 ], function(bc, sendJob, fs, fileUtils, stripConsole, lang, has){
10 if(has("host-node")){
11 return function(resource, text, copyright, optimizeSwitch, callback){
12 copyright = copyright || "";
13 if(bc.stripConsole){
14 var tempFilename = resource.dest + ".consoleStripped.js";
15 text = stripConsole(text);
16 fs.writeFile(tempFilename, bc.newlineFilter(text, resource, "closureStripConsole"), resource.encoding, function(err){
17 if(!err){
18 sendJob(tempFilename, resource.dest, optimizeSwitch, copyright);
19 }
20 callback(resource, err);
21 });
22 return callback;
23 }else{
24 sendJob(resource.dest + ".uncompressed.js", resource.dest, optimizeSwitch, copyright);
25 return 0;
26 }
27 };
28 }
29
30 if(has("host-rhino")){
31 var JSSourceFilefromCode,
32 closurefromCode,
33 jscomp = 0,
34 built = "//>>built" + bc.newline;
35
36 var ccompile = function(text, dest, optimizeSwitch, copyright, useSourceMaps){
37 /*jshint rhino:true */
38 /*global com:false Packages:false */
39 if(!jscomp){
40 JSSourceFilefromCode = java.lang.Class.forName("com.google.javascript.jscomp.SourceFile").getMethod("fromCode", [ java.lang.String, java.lang.String ]);
41 closurefromCode = function(filename,content){
42 return JSSourceFilefromCode.invoke(null, [filename, content]);
43 };
44 jscomp = com.google.javascript.jscomp;
45 }
46 //Fake extern
47 var externSourceFile = closurefromCode("fakeextern.js", "function Symbol(description) {}");
48
49 //Set up source input
50 var destFilename = dest.split("/").pop(),
51 jsSourceFile = closurefromCode(destFilename + ".uncompressed.js", String(text));
52
53 //Set up options
54 var options = new jscomp.CompilerOptions();
55 var optimizeOptions = bc.optimizeOptions || {};
56
57 var FLAG_compilation_level = jscomp.CompilationLevel.SIMPLE_OPTIMIZATIONS;
58 if (optimizeOptions.compilationLevel) {
59 switch (optimizeOptions.compilationLevel.toUpperCase()) {
60 case "WHITESPACE_ONLY": FLAG_compilation_level = jscomp.CompilationLevel.WHITESPACE_ONLY;
61 break;
62 case "SIMPLE_OPTIMIZATIONS": FLAG_compilation_level = jscomp.CompilationLevel.SIMPLE_OPTIMIZATIONS;
63 break;
64 case "ADVANCED_OPTIMIZATIONS": FLAG_compilation_level = jscomp.CompilationLevel.ADVANCED_OPTIMIZATIONS;
65 break;
66 }
67 }
68 FLAG_compilation_level.setOptionsForCompilationLevel(options);
69 var FLAG_warning_level = jscomp.WarningLevel.DEFAULT;
70 FLAG_warning_level.setOptionsForWarningLevel(options);
71
72 // force this option to false to prevent overly aggressive code elimination (#18919)
73 options.setDeadAssignmentElimination(false);
74 options.setEmitUseStrict(false);
75 options.setStrictModeInput(false);
76 options.setWarningLevel(jscomp.DiagnosticGroups.ES5_STRICT, jscomp.CheckLevel.WARNING);
77
78 for(var k in optimizeOptions){
79 // Skip compilation level option
80 if (k === 'compilationLevel') {
81 continue;
82 }
83
84 // Set functions requiring Java Enum values
85 if (k === 'languageIn') {
86 options.setLanguageIn(jscomp.CompilerOptions.LanguageMode.fromString(optimizeOptions[k].toUpperCase()));
87 continue;
88 }
89
90 if (k === 'languageOut') {
91 options.setLanguageOut(jscomp.CompilerOptions.LanguageMode.fromString(optimizeOptions[k].toUpperCase()));
92 continue;
93 }
94
95 if (k === 'variableRenaming') {
96 switch (optimizeOptions[k].toUpperCase()) {
97 case 'OFF': options.setVariableRenaming(jscomp.VariableRenamingPolicy.OFF);
98 break;
99 case 'LOCAL': options.setVariableRenaming(jscomp.VariableRenamingPolicy.LOCAL);
100 break;
101 case 'ALL': options.setVariableRenaming(jscomp.VariableRenamingPolicy.ALL);
102 break;
103 }
104 continue;
105 }
106
107 if (k === 'propertyRenaming') {
108 switch (optimizeOptions[k].toUpperCase()) {
109 case 'OFF': options.setPropertyRenaming(jscomp.PropertyRenamingPolicy.OFF);
110 break;
111 case 'ALL_UNQUOTED': options.setPropertyRenaming(jscomp.PropertyRenamingPolicy.ALL_UNQUOTED);
112 break;
113 }
114 continue;
115 }
116
117 if (k === 'checkGlobalThisLevel') {
118 switch (optimizeOptions[k].toUpperCase()) {
119 case 'ERROR': options.setCheckGlobalThisLevel(jscomp.CheckLevel.ERROR);
120 break;
121 case 'WARNING': options.setCheckGlobalThisLevel(jscomp.CheckLevel.WARNING);
122 break;
123 case 'OFF': options.setCheckGlobalThisLevel(jscomp.CheckLevel.OFF);
124 break;
125 }
126 continue;
127 }
128
129 if (k === 'uselessCode') {
130 var uselessCode = jscomp.DiagnosticGroups.CHECK_USELESS_CODE;
131 switch (optimizeOptions[k].toUpperCase()) {
132 case 'ERROR': options.setWarningLevel(uselessCode, jscomp.CheckLevel.ERROR);
133 break;
134 case 'WARNING': options.setWarningLevel(uselessCode, jscomp.CheckLevel.WARNING);
135 break;
136 case 'OFF': options.setWarningLevel(uselessCode, jscomp.CheckLevel.OFF);
137 break;
138 }
139 continue;
140 }
141
142 if (k in {
143 'removeUnusedVariables': 1,
144 'inlineVariables': 1,
145 'inlineFunctions': 1
146 }) {
147 set = "set" + k.charAt(0).toUpperCase() + k.substr(1);
148 switch (optimizeOptions[k].toUpperCase()) {
149 case 'ALL': options[set](jscomp.CompilerOptions.Reach.ALL);
150 break;
151 case 'LOCAL_ONLY': options[set](jscomp.CompilerOptions.Reach.LOCAL_ONLY);
152 break;
153 case 'NONE': options[set](jscomp.CompilerOptions.Reach.NONE);
154 break;
155 }
156 continue;
157 }
158
159 // Set functions with boolean values
160 if (k === 'dependencySorting') {
161 options.dependencyOptions.setDependencySorting(optimizeOptions[k]);
162 continue;
163 }
164
165 if (k in {
166 'assumeClosuresOnlyCaptureReferences': 1,
167 'closurePass': 1,
168 'coalesceVariableNames': 1,
169 'collapseAnonymousFunctions': 1,
170 'collapseProperties': 1,
171 'collapseVariableDeclarations': 1,
172 'computeFunctionSideEffects': 1,
173 'crossModuleCodeMotion': 1,
174 'crossModuleMethodMotion': 1,
175 'deadAssignmentElimination': 1,
176 'devirtualizePrototypeMethods': 1,
177 'extractPrototypeMemberDeclarations': 1,
178 'flowSensitiveInlineVariables': 1,
179 'foldConstants': 1,
180 'inlineConstantVars': 1,
181 'optimizeArgumentsArray': 1,
182 'removeDeadCode': 1,
183 'removeUnusedClassProperties': 1,
184 'removeUnusedPrototypeProperties': 1,
185 'removeUnusedPrototypePropertiesInExterns': 1,
186 'rewriteFunctionExpressions': 1,
187 'smartNameRemoval': 1
188 }) {
189 set = 'set' + k.charAt(0).toUpperCase() + k.substr(1);
190 options[set](optimizeOptions[k]);
191 continue;
192 }
193
194 // Otherwise assign value to key
195 options[k] = optimizeOptions[k];
196 }
197 // Must have non-null path to trigger source map generation, also fix version
198 options.setSourceMapOutputPath("");
199 options.setSourceMapFormat(jscomp.SourceMap.Format.V3);
200 if(optimizeSwitch.indexOf(".keeplines") !== -1){
201 options.prettyPrint = true;
202 }
203
204
205 //Prevent too-verbose logging output
206 Packages.com.google.javascript.jscomp.Compiler.setLoggingLevel(java.util.logging.Level.SEVERE);
207
208 // Run the compiler
209 // File name and associated map name
210 var mapTag = useSourceMaps ? (bc.newline + "//# sourceMappingURL=" + destFilename + ".map") : "";
211 var compiler = new Packages.com.google.javascript.jscomp.Compiler(Packages.java.lang.System.err);
212 compiler.compile(externSourceFile, jsSourceFile, options);
213 var result = copyright + built + compiler.toSource() + mapTag;
214
215 if(useSourceMaps){
216 var sourceMap = compiler.getSourceMap();
217 sourceMap.setWrapperPrefix(copyright + built);
218 var sb = new java.lang.StringBuffer();
219 sourceMap.appendTo(sb, destFilename);
220 fs.writeFile(dest + ".map", sb.toString(), "utf-8");
221 }
222
223 return result;
224 };
225
226 return function(resource, text, copyright, optimizeSwitch, callback){
227 bc.log("optimize", ["module", resource.mid]);
228 copyright = copyright || "";
229 try{
230 var result = ccompile(stripConsole(text), resource.dest, optimizeSwitch, copyright, bc.useSourceMaps);
231 fs.writeFile(resource.dest, result, resource.encoding, function(err){
232 if(err){
233 bc.log("optimizeFailedWrite", ["filename", result.dest]);
234 }
235 callback(resource, err);
236 });
237 }catch(e){
238 bc.log("optimizeFailed", ["module identifier", resource.mid, "exception", e + ""]);
239 callback(resource, 0);
240 }
241 return callback;
242 };
243 }
244
245 throw new Error("Unknown host environment: only nodejs and rhino are supported by closure optimizer.");
246 });