Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript "compilers" [closed]

Recently I found such cool thing as Google Closure Compiler. Are there alternatives which provide opportunities to get benefits of compiled statically-typed languages?

Update

It is not about real compilers, it is about JavaScript-JavaScript translators, which provide types verification etc, optimization, and maybe compression.

like image 356
Danil Speransky Avatar asked Sep 03 '12 00:09

Danil Speransky


2 Answers

From your comment:

I am interested in type checking, interface checking, private fields checking etc, all things allows you to write safety code, not only minimizer.

JavaScript is a dynamically-typed language that does not have built-in support for classes, interfaces, or access modifiers. Closure Compiler supports various extralinguistic features such as interfaces and access modifiers by using information embedded in JSDoc comments. Several IDEs also provide static analysis using JSDoc tag information, for example, see WebStorm & PhpStorm Blog: New in 5.0: Google Closure Compiler JSDoc annotations.

In the area of JavaScript minifiers there are numerous options such as YUI Compressor, UglifyJS, dojo shrinksafe, Microsoft Ajax Minifier, and JSMIN. However, none of these tools provide the same level of support for JSDoc-based analysis as Closure Compiler.

Related stack overflow questions:

  1. Which Javascript minifier (cruncher) does the same things that the one Google uses for its JS APIs?
  2. What are some good css and js minimizers for production code?
  3. Type checker for JavaScript?
  4. Is there a good JavaScript minimizer?
  5. JavaScript and CSS minimizer
  6. JavaScript compression
  7. What is the current state of JavaScript static type checking?
like image 80
Christopher Peisert Avatar answered Oct 07 '22 20:10

Christopher Peisert


Closure compiler (Google) is a true compiler for javascript. Alternatives include typescript (Microsoft) and Flow (facebook). Closure compiler uses jsdoc comments to annotate types. Typescript uses a different syntax than es3/es5 to provide type annotations that compiles to plain javascript. Flow piggybacks onto the OCaml language (which excels in type inference) to infer as much type information as possible, but can consume annotations in a comment syntax as well. Closure compiler is also working on better type inference, but it is not ready for production.

In response to @EASI: The closure compiler is a true compiler, not just a minifier. It works by:

  1. Parsing a set of input .js files and a set of extern files (that defined interfaces for ecma 3,5,6 and common browser objects like Window, etc) into an abstract syntax tree (AST).

  2. Running a series of compiler passes over the AST to rewrite, transform, eliminate dead code etc.

  3. Emit the AST back into js source code. It will either concatenate the files back together with comments stripped out (WHITESPACE_ONLY), rename and minify symbols within function definitions (SIMPLE), or rename and rewrite all symbols into a minified and obfuscated form (ADVANCED).

Here's a list of compiler passes, for those that are interested. As you can see, there is a lot going on:

AliasExternals.java AliasStrings.java AmbiguateProperties.java AnalyzeNameReferences.java AnalyzePrototypeProperties.java AstValidator.java CallGraph.java ChainCalls.java CheckConformance.java CheckDebuggerStatement.java CheckEventfulObjectDisposal.java CheckGlobalNames.java CheckMissingGetCssName.java CheckRegExp.java CheckSideEffects.java ClosureCodeRemoval.java ClosureOptimizePrimitives.java CollapseAnonymousFunctions.java CollapseProperties.java CollapseVariableDeclarations.java ConstCheck.java ConstParamCheck.java ConvertDeclaredTypesToJSDoc.java ConvertToDottedProperties.java ConvertToTypedES6.java CoverageInstrumentationPass.java CreateSyntheticBlocks.java CrossModuleCodeMotion.java CrossModuleMethodMotion.java DeclaredGlobalExternsOnWindow.java DefaultPassConfig.java Denormalize.java DisambiguateProperties.java ErrorPass.java Es6ToEs3ClassSideInheritance.java ExpandJqueryAliases.java ExportTestFunctions.java ExternExportsPass.java ExtractPrototypeMemberDeclarations.java FlowSensitiveInlineVariables.java FunctionNames.java FunctionRewriter.java GatherExternProperties.java GatherRawExports.java GenerateExports.java GlobalNamespace.java GlobalTypeInfo.java GroupVariableDeclarations.java ImplicitNullabilityCheck.java InferConsts.java InjectEs6RuntimeLibrary.java InlineFunctions.java InlineObjectLiterals.java InlineProperties.java InlineVariables.java InstrumentFunctions.java JsMessageVisitor.java MarkNoSideEffectCalls.java MethodCompilerPass.java MinimizeExitPoints.java NameAnalyzer.java NameAnonymousFunctions.java NameAnonymousFunctionsMapped.java NameReferenceGraphConstruction.java NewTypeInference.java Normalize.java ObjectPropertyStringPostprocess.java ObjectPropertyStringPreprocess.java OptimizeArgumentsArray.java OptimizeCalls.java OptimizeParameters.java PeepholeOptimizationsPass.java PhaseOptimizer.java PrepareAst.java ProcessCommonJSModules.java ProcessDefines.java ProcessTweaks.java PureFunctionIdentifier.java RecordFunctionInformation.java RemoveUnusedClassProperties.java RemoveUnusedNames.java RemoveUnusedPrototypeProperties.java RemoveUnusedVars.java RenameLabels.java RenameProperties.java RenamePrototypes.java RenameVars.java ReplaceCssNames.java ReplaceIdGenerators.java ReplaceStrings.java RescopeGlobalSymbols.java RuntimeTypeCheck.java SanityCheck.java ShadowVariables.java SideEffectsAnalysis.java SimpleDefinitionFinder.java StrictModeCheck.java StripCode.java SymbolTable.java TransformAMDToCJSModule.java TypeInferencePass.java TypedScopeCreator.java UnreachableCodeElimination.java VariableVisibilityAnalysis.java

like image 20
Paul Avatar answered Oct 07 '22 21:10

Paul