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; }
@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); }
public CompilerOptions defaultCompilerOptions() { CompilerOptions result = new CompilerOptions(); if (compileLevel.equals("advanced")) { CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(result); } else if (compileLevel.equals("whitespace_only")) { CompilationLevel.WHITESPACE_ONLY.setOptionsForCompilationLevel(result); } else { // If 'none', this complier will not run, @see compile CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(result); } return result; }
/** * Prepare options for the Compiler. */ private CompilerOptions getCompilerOptions() { CompilationLevel level = null; try { level = this.compilationLevel; } catch (IllegalArgumentException e) { throw new CoffeeException("Compilation level is invalid", e); } CompilerOptions options = new CompilerOptions(); level.setOptionsForCompilationLevel(options); return options; }
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel( options_);
private void compress() throws UnsupportedEncodingException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream is = new ByteArrayInputStream(content); Writer out = new OutputStreamWriter(baos, charset); CompilerOptions options = new CompilerOptions(); CompilationLevel.SIMPLE_OPTIMIZATIONS .setOptionsForCompilationLevel(options); Compiler.setLoggingLevel(Level.OFF); Compiler compiler = new Compiler(); compiler.disableThreads(); Result result = compiler.compile(new JSSourceFile[] {}, new JSSourceFile[] { JSSourceFile.fromInputStream("is", is) }, options); if (result.success) { Pattern pattern = Pattern.compile("^/\\*.*?\\*/\\s?", Pattern.DOTALL); Matcher matcher = pattern.matcher(new String(content, charset)); while (matcher.find()) { out.write(matcher.group()); } out.write(compiler.toSource()); out.flush(); content = baos.toByteArray(); } is.close(); out.close(); } }
@SuppressWarnings("unchecked") private static CompilerOptions buildCompilerOptionsFromConfig(FilterConfig config) { CompilerOptions compilerOptions = new CompilerOptions(); compilerOptions.setCodingConvention(CodingConventions.getDefault()); Enumeration<String> initParams = config.getInitParameterNames(); while (initParams.hasMoreElements()) { String name = initParams.nextElement().trim(); String value = config.getInitParameter(name); if ("charset".equals(name)) { if (value != null && Charset.isSupported(value)) { compilerOptions.setOutputCharset(Charset.forName(value)); } else { compilerOptions.setOutputCharset(StandardCharsets.UTF_8); } } else if ("compilationLevel".equals(name)) { CompilationLevel compilationLevel = CompilationLevel.valueOf(value); compilationLevel.setOptionsForCompilationLevel(compilerOptions); } else if ("formatting".equals(name)) { if ("PRETTY_PRINT".equals(value)) { compilerOptions.prettyPrint = true; } else if ("PRINT_INPUT_DELIMITER".equals(value)) { compilerOptions.printInputDelimiter = true; } } else if ("loggingLevel".equals(name)) { Compiler.setLoggingLevel(Level.parse(value)); //warning or error } } return compilerOptions; }
CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options); com.google.javascript.jscomp.Compiler compiler = new com.google.javascript.jscomp.Compiler(); Result result = compiler.compile(externs, inputs, options);
@Override public void compress(Reader reader, Writer writer) throws CompressorException { com.google.javascript.jscomp.Compiler compiler = new com.google.javascript.jscomp.Compiler(); compiler.disableThreads(); CompilerOptions options = new CompilerOptions(); CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options); try { SourceFile inputFile = SourceFile.fromReader("input.js", reader); List<SourceFile> inputFiles = new ArrayList<>(); inputFiles.add(inputFile); List<SourceFile> externFiles = new ArrayList<>(); compiler.compile(externFiles, inputFiles, options); writer.write(compiler.toSource()); } catch(IOException x) { throw new CompressorException("Error while compressing javascript", x); } }
private Compiler newCompiler(final CompilerOptions compilerOptions) { Compiler.setLoggingLevel(Level.SEVERE); final Compiler compiler = new Compiler(); compilationLevel.setOptionsForCompilationLevel(compilerOptions); // make it play nice with GAE compiler.disableThreads(); compiler.initOptions(compilerOptions); return compiler; }
private Compiler newCompiler(final CompilerOptions compilerOptions) { Compiler.setLoggingLevel(Level.SEVERE); final Compiler compiler = new Compiler(); compilationLevel.setOptionsForCompilationLevel(compilerOptions); // make it play nice with GAE compiler.disableThreads(); compiler.initOptions(compilerOptions); return compiler; }
public ClosureCompiler() { //We have to ensure a safe concurrency as ModuleGenerator is session-scoped and //multiple sessions can generate the script at the same time. synchronized (lock) { this.options = new CompilerOptions(); CompilationLevel.WHITESPACE_ONLY.setOptionsForCompilationLevel(options); options.setVariableRenaming(VariableRenamingPolicy.OFF); options.setAngularPass(true); options.setTightenTypes(false); options.prettyPrint = false; } }
private Compiler newCompiler(CompilerOptions compilerOptions, CompilationLevel compilationLevel) { Compiler.setLoggingLevel(Level.SEVERE); Compiler compiler = new Compiler(); compilationLevel.setOptionsForCompilationLevel(compilerOptions); // make it play nice with GAE compiler.disableThreads(); compiler.initOptions(compilerOptions); return compiler; }
public static String compile(String code) { Compiler compiler = new Compiler(); CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options); JSSourceFile extern = JSSourceFile.fromCode("externs.js","function alert(x) {}"); // The dummy input name "input.js" is used here so that any warnings or // errors will cite line numbers in terms of input.js. JSSourceFile input = JSSourceFile.fromCode("input.js", code); // compile() returns a Result, but it is not needed here. compiler.compile(extern, input, options); // The compiler is responsible for generating the compiled code; it is not // accessible via the Result. return compiler.toSource(); }
compilationLevel.setOptionsForCompilationLevel(options);
compilationLevel.setOptionsForCompilationLevel(options);
@Override public CharSequence minify(final Config config, final CharSequence source) { final CompilerOptions options = new CompilerOptions(); options.setCodingConvention(new ClosureCodingConvention()); options.setOutputCharset("UTF-8"); options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING); compilationLevel.setOptionsForCompilationLevel(options); Compiler.setLoggingLevel(Level.SEVERE); Compiler compiler = new Compiler(); compiler.disableThreads(); compiler.initOptions(options); String fname = removeExtension(config.getName()) + ".js"; Result result = compiler.compile(defaultExterns, Arrays.asList(SourceFile.fromCode(fname, source.toString())), options); if (result.success) { return compiler.toSource(); } JSError[] errors = result.errors; throw new IllegalStateException(errors[0].toString()); }
public static CompiledSourceWithSourceMap compile(String code, String sourcePath, Map<String, String> closureOptions, Log log) { Compiler compiler = new Compiler(); CompilerOptions options = getOptions(closureOptions, log); CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
options.setLanguageOut(CompilerOptions.LanguageMode.ECMASCRIPT5); CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
@Override public String compress(final String original) { final Compiler compiler = new Compiler(); final CompilerOptions options = new CompilerOptions(); // Advanced mode is used here, but additional options could be set, too. level.setOptionsForCompilationLevel(options); // To get the complete set of externs, the logic in // CompilerRunner.getDefaultExterns() should be used here. final SourceFile extern = SourceFile.fromCode("externs.js", "function alert(x) {}"); // The dummy input name "input.js" is used here so that any warnings or // errors will cite line numbers in terms of input.js. final SourceFile input = SourceFile.fromCode("input.js", original); // compile() returns a Result, but it is not needed here. compiler.compile(extern, input, options); // The compiler is responsible for generating the compiled code; it is not // accessible via the Result. return compiler.toSource(); } }