protected CompilerOptions getCompilerOptions() { CompilerOptions options = new CompilerOptions(); options.setLanguageIn(this.languageIn); options.setLanguageOut(this.languageOut); options.setCodingConvention(new ClosureCodingConvention()); CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options); WarningLevel.valueOf(this.warningLevel).setOptionsForWarningLevel(options); options.setClosurePass(true); options.skipAllCompilerPasses(); return options; }
protected Compiler getCompiler() { return new Compiler(); }
@Override public String process(final String filename, final String source, final Config conf, final ClassLoader loader) throws Exception { final CompilerOptions copts = new CompilerOptions(); copts.setCodingConvention(new ClosureCodingConvention()); copts.setOutputCharset(StandardCharsets.UTF_8); copts.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING); CompilationLevel level = level(get("level")); level.setOptionsForCompilationLevel(copts); Compiler.setLoggingLevel(Level.SEVERE); Compiler compiler = new Compiler(); compiler.disableThreads(); compiler.initOptions(copts); List<SourceFile> externs = externs(copts); Result result = compiler.compile(externs, ImmutableList.of(SourceFile.fromCode(filename, source)), copts); if (result.success) { return compiler.toSource(); } List<AssetProblem> errors = Arrays.stream(result.errors) .map(error -> new AssetProblem(error.sourceName, error.lineNumber, error.getCharno(), error.description, null)) .collect(Collectors.toList()); throw new AssetException(name(), errors); }
protected String compileJs(SourceFile input, String filename) throws ResourceMinificationException, IOException { Compiler compiler = getCompiler(); List<SourceFile> builtinExterns = AbstractCommandLineRunner.getBuiltinExterns(CompilerOptions.Environment.CUSTOM); Result result = compiler.compile(builtinExterns, Arrays.asList(input), getCompilerOptions()); String compiled = compiler.toSource(); if (!result.success || StringUtils.isBlank(compiled)) { StringBuilder errorString = new StringBuilder("\n"); if (result.errors != null) { for (int i = 0; i < result.errors.length; i++) { errorString.append(result.errors[i].description + "\n"); } } throw new ResourceMinificationException("Error minifying js file " + filename + errorString); } return compiler.toSource(); }
private List<SourceFile> externs(final CompilerOptions coptions) throws IOException { List<SourceFile> externs = CommandLineRunner.getBuiltinExterns(coptions.getEnvironment()); List<String> local = get("externs"); if (local != null) { Class<?> loader = getClass(); for (String js : local) { String path = js.startsWith("/") ? js : "/" + js; try (InputStream stream = loader.getResourceAsStream(path)) { if (stream == null) { throw new FileNotFoundException(path); } externs.add(SourceFile.fromInputStream(path, stream, StandardCharsets.UTF_8)); } } } return externs; }
public void whitespaceOnlyPasses() { runCustomPasses(CustomPassExecutionTime.BEFORE_CHECKS); Tracer t = newTracer("runWhitespaceOnlyPasses"); try { for (PassFactory pf : getPassConfig().getWhitespaceOnlyPasses()) { pf.create(this).process(externsRoot, jsRoot); } } finally { stopTracer(t, "runWhitespaceOnlyPasses"); } }
@Override public void process(Node externs, Node root) { NodeTraversal.traverse(compiler, root, new AddMarkers(compiler)); NodeTraversal.traverse(compiler, root, new AddChecks()); addBoilerplateCode(); new Normalize(compiler, false).process(externs, root); }
@Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { shouldAddTypesOnNewAstNodes = getShouldAddTypesOnNewAstNodes(); NodeTraversal.traverse(compiler, scriptRoot, new CollectUndeclaredNames()); NodeTraversal.traverse(compiler, scriptRoot, this); NodeTraversal.traverse(compiler, scriptRoot, new Es6RenameReferences(renameTable)); LoopClosureTransformer transformer = new LoopClosureTransformer(); NodeTraversal.traverse(compiler, scriptRoot, transformer); transformer.transformLoopClosure(); rewriteDeclsToVars(); TranspilationPasses.maybeMarkFeaturesAsTranspiledAway(compiler, transpiledFeatures); }
private void run() { GatherJ2CLClassGetterSetters gatherer = new GatherJ2CLClassGetterSetters(); NodeTraversal.traverse(compiler, root, gatherer); Map<String, J2clProperty> result = gatherer.getResults(); NodeTraversal.traverse(compiler, root, new DetermineInlinableProperties(result)); new InlinePropertiesPass(result).run(); }
@Override public void process(Node externs, Node root) { NodeTraversal.traverse(compiler, root, this); // Don't inline the VAR declaration if this compilation involves old-style ctemplates. if (compiler.getOptions().syntheticBlockStartMarker == null) { (new ReferenceCollectingCallback(compiler, this, new Es6SyntacticScopeCreator(compiler))) .process(root); } }
@Override public void process(Node externs, Node root) { namespace = new GlobalNamespace(compiler, externs, root); NodeTraversal.traverseRoots(compiler, new AliasesCollector(), externs, root); NodeTraversal.traverseRoots(compiler, new AliasesInliner(), externs, root); }
@Override public void process(Node externs, Node root) { NodeTraversal.traverseRootsEs6(compiler, new CollectUndeclaredNames(), externs, root); NodeTraversal.traverseRootsEs6(compiler, this, externs, root); NodeTraversal.traverseRootsEs6(compiler, new Es6RenameReferences(renameMap), externs, root); LoopClosureTransformer transformer = new LoopClosureTransformer(); NodeTraversal.traverseRootsEs6(compiler, transformer, externs, root); transformer.transformLoopClosure(); varify(); NodeTraversal.traverseRootsEs6( compiler, new RewriteBlockScopedFunctionDeclaration(), externs, root); }
@Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { GlobalVarReferenceMap refMap = compiler.getGlobalVarReferences(); if (refMap != null) { // We don't have a suitable untyped scope to use, but the actual scope probably doesn't // matter, so long as it's a global scope and doesn't persist references to things that // need to be cleaned up. So we just generate a new simple root scope. refMap.updateReferencesWithGlobalScope( Scope.createGlobalScope(compiler.getTopScope().getRootNode())); } }
@Override public void process(Node externs, Node root) { ReferenceCollectingCallback callback = new ReferenceCollectingCallback( compiler, new InliningBehavior(), new Es6SyntacticScopeCreator(compiler), getFilterForMode()); callback.process(externs, root); }
/** * Returns a new tracer for the given pass name. */ Tracer newTracer(String passName) { String comment = passName + (recentChange.hasCodeChanged() ? " on recently changed AST" : ""); if (options.getTracerMode().isOn() && tracker != null) { tracker.recordPassStart(passName, true); } return new Tracer("Compiler", comment); }
@Override public void process(Node externs, Node root) { if (shouldProcess(root)) { new ReferenceCollectingCallback( compiler, new ReferenceCheckingBehavior(), new Es6SyntacticScopeCreator(compiler)) .process(externs, root); } }
@Override public void hotSwapScript(Node scriptRoot, Node originalRoot) { NodeTraversal.traverseEs6(compiler, scriptRoot, new CollectUndeclaredNames()); NodeTraversal.traverseEs6(compiler, scriptRoot, this); NodeTraversal.traverseEs6(compiler, scriptRoot, new Es6RenameReferences(renameMap)); LoopClosureTransformer transformer = new LoopClosureTransformer(); NodeTraversal.traverseEs6(compiler, scriptRoot, transformer); transformer.transformLoopClosure(); varify(); NodeTraversal.traverseEs6(compiler, scriptRoot, new RewriteBlockScopedFunctionDeclaration()); }
private void processDeclarationStatement(TopLevelStatement statement) { GlobalSymbol declaredSymbol = getGlobalSymbol(statement.getDeclaredNameReference().getSymbol()); DeclarationStatementGroup dsg = declaredSymbol.addDeclarationStatement(statement); processDeclarationStatementContainedReferences(statement, declaredSymbol, dsg); }
public GoogleClosureJavascriptMinificationServiceImpl(@Value("${minify.closure.compiler.languageIn:ECMASCRIPT5}") String compilerLanguageIn, @Value("${minify.closure.compiler.languageOut:NO_TRANSPILE}") String compilerLanguageOut) { this.languageIn = null; this.languageOut = null; if (StringUtils.isNotBlank(compilerLanguageIn)) { this.languageIn = CompilerOptions.LanguageMode.valueOf(compilerLanguageIn); this.languageOut = this.languageIn; } if (StringUtils.isNoneBlank(compilerLanguageOut)) { this.languageOut = CompilerOptions.LanguageMode.valueOf(compilerLanguageOut); if (this.languageIn == null) { this.languageIn = this.languageOut; } } if (this.languageIn == null) { throw new IllegalArgumentException("Please set properties \"minify.closure.compiler.languageIn\" or \"minify.closure.compiler.languageOut\" if you wish to use Google Closure Compiler for Javascript minification"); } }
@Override public void minifyJs(String filename, Reader reader, Writer writer) throws ResourceMinificationException { try { SourceFile input = SourceFile.fromReader(filename, reader); String compiled = compileJs(input, filename); writer.write(compiled); } catch (IOException e) { throw new ResourceMinificationException("Error minifying js file " + filename, e); } }