Closure编译器选项

编程入门 行业动态 更新时间:2024-10-27 07:31:14
本文介绍了Closure编译器选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用Closure Compiler来缩小/压缩JS代码。

I want to use Closure Compiler to minify/compress JS code.

问题是它不会像我预期的那样缩小。考虑下面的代码。当我传递字符串

the problem is that it doesn't minify as well as I expect it to. consider the code below. when I pass the string

var func = function ( someArgument ) { alert ( someArgument ); return someArgument; }

我希望缩小的代码将someArgument重命名为更短, a。

I expect the minified code to rename "someArgument" to something much shorter, like "a".

是它的方式或我做错了什么? TIA

is that the way it is or am I doing something wrong? TIA

public static void Compress( String src ) { ByteArrayOutputStream err = new ByteArrayOutputStream(); CompilerOptions opt = new CompilerOptions(); CompilationLevel.ADVANCED_OPTIMIZATIONS.setDebugOptionsForCompilationLevel( opt ); Compiler.setLoggingLevel( Level.OFF ); Compiler compiler = new Compiler( new PrintStream( err ) ); compiler.disableThreads(); List<SourceFile> externs = Collections.emptyList(); List<SourceFile> inputs = Arrays.asList( SourceFile.fromCode( "javascript-code.js", src) ); Result result = compilerpile( externs, inputs, opt ); System.out.println( "source: " + compiler.toSource() ); }

推荐答案

$ c> setDebugOptionsForCompilationLevel(),您需要 setOptionsForCompilationLevel()。从来源,这是 setDebugOptionsForCompilationLevel 正在执行:

You are using setDebugOptionsForCompilationLevel(), you want setOptionsForCompilationLevel(). From the Source this is what setDebugOptionsForCompilationLevel is doing:

public void setDebugOptionsForCompilationLevel(CompilerOptions options) { options.anonymousFunctionNaming = AnonymousFunctionNamingPolicy.UNMAPPED; options.generatePseudoNames = true; options.removeClosureAsserts = false; // Don't shadow variables as it is too confusing. options.shadowVariables = false; }

虽然这是 setOptionsForCompilationLevel c $ c>正在执行:

While this is what setOptionsForCompilationLevel() is doing:

// All the safe optimizations. options.dependencyOptions.setDependencySorting(true); options.closurePass = true; options.foldConstants = true; options.coalesceVariableNames = true; options.deadAssignmentElimination = true; options.extractPrototypeMemberDeclarations = true; options.collapseVariableDeclarations = true; options.convertToDottedProperties = true; options.rewriteFunctionExpressions = true; options.labelRenaming = true; options.removeDeadCode = true; options.optimizeArgumentsArray = true; options.collapseObjectLiterals = true; options.protectHiddenSideEffects = true; // All the advance optimizations. options.removeClosureAsserts = true; options.aliasKeywords = true; options.reserveRawExports = true; options.setRenamingPolicy( VariableRenamingPolicy.ALL, PropertyRenamingPolicy.ALL_UNQUOTED); options.shadowVariables = true; options.removeUnusedPrototypeProperties = true; options.removeUnusedPrototypePropertiesInExterns = true; options.collapseAnonymousFunctions = true; options.collapseProperties = true; options.checkGlobalThisLevel = CheckLevel.WARNING; options.rewriteFunctionExpressions = true; options.smartNameRemoval = true; options.inlineConstantVars = true; options.setInlineFunctions(Reach.ALL); options.inlineGetters = true; options.setInlineVariables(Reach.ALL); options.flowSensitiveInlineVariables = true; optionsputeFunctionSideEffects = true; // Remove unused vars also removes unused functions. options.setRemoveUnusedVariables(Reach.ALL); // Move code around based on the defined modules. options.crossModuleCodeMotion = true; options.crossModuleMethodMotion = true; // Call optimizations options.devirtualizePrototypeMethods = true; options.optimizeParameters = true; options.optimizeReturns = true; options.optimizeCalls = true;

从技术上讲,SIMPLE_OPTIMIZATIONS会给你参数重命名。如果高级开始导致你的代码出现问题从源):

Technically, SIMPLE_OPTIMIZATIONS would give you argument renaming., in case advanced start causing problems with your code (again from the source):

/** * SIMPLE_OPTIMIZATIONS performs transformations to the input JS that do not * require any changes to JS that depend on the input JS. For example, * function arguments are renamed (which should not matter to code that * depends on the input JS), but functions themselves are not renamed (which * would otherwise require external code to change to use the renamed function * names). */

更多推荐

Closure编译器选项

本文发布于:2023-07-27 14:16:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222809.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编译器   选项   Closure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!