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(); }
@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); }
/** Compiles a single source file and a single externs file. */ public Result compile(SourceFile extern, SourceFile input, CompilerOptions options) { return compile(ImmutableList.of(extern), ImmutableList.of(input), options); }
public Result compile( SourceFile extern, SourceFile input, CompilerOptions options) { return compile(ImmutableList.of(extern), ImmutableList.of(input), options); }
private Compiler createCompiler( List<SourceFile> inputs, List<SourceFile> externs, CompilerOptions compilerOptions) { Compiler compiler = new Compiler(); compiler.disableThreads(); compiler.compile(externs, inputs, compilerOptions); return compiler; }
public String runtime(String library) { Compiler compiler = compiler(); CompilerOptions options = options(); options.setForceLibraryInjection(ImmutableList.of(library)); compiler.compile(EXTERNS, EMPTY, options); return compiler.toSource(); }
public String runtime(String library) { Compiler compiler = compiler(); CompilerOptions options = options(); options.setForceLibraryInjection(ImmutableList.of(library)); compiler.compile(EXTERNS, EMPTY, options); return compiler.toSource(); }
public CompileResult compile(Path path, String code) { Compiler compiler = compiler(); Result result = compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options()); String source = compiler.toSource(); StringBuilder sourceMap = new StringBuilder(); if (result.sourceMap != null) { try { result.sourceMap.appendTo(sourceMap, path.toString()); } catch (IOException e) { // impossible, and not a big deal even if it did happen. } } boolean transformed = transformed(result); return new CompileResult( source, result.errors, transformed, transformed ? sourceMap.toString() : ""); }
@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); } }
public CompileResult compile(URI path, String code) { Compiler compiler = compiler(); Result result = compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options()); String source = compiler.toSource(); StringBuilder sourceMap = new StringBuilder(); if (result.sourceMap != null) { try { result.sourceMap.appendTo(sourceMap, path.toString()); } catch (IOException e) { // impossible, and not a big deal even if it did happen. } } boolean transpiled = !result.transpiledFiles.isEmpty(); if (result.errors.length > 0) { throw new TranspilationException(compiler, result.errors, result.warnings); } return new CompileResult( source, transpiled, transpiled ? sourceMap.toString() : ""); }
/** Generates the runtime by requesting the "es6_runtime" library from the compiler. */ private static String getEs6Runtime() { CompilerOptions options = getOptions(); options.setLanguageOut(LanguageMode.ECMASCRIPT3); // change .delete to ['delete'] options.setForceLibraryInjection(ImmutableList.of("es6_runtime")); Compiler compiler = new Compiler(); // Threads can't be used in small unit tests. compiler.disableThreads(); SourceFile externs = SourceFile.fromCode("externs", "function Symbol() {}"); SourceFile sourceFile = SourceFile.fromCode("source", ""); compiler.compile(ImmutableList.of(externs), ImmutableList.of(sourceFile), options); return compiler.toSource(); } }
public String compile(String code) { Compiler compiler = new Compiler(); compiler.disableThreads(); SourceFile extern = SourceFile.fromCode("externs.js","function alert(x) {}"); SourceFile input = SourceFile.fromCode("input.js", code); compiler.compile(extern, input, options); return compiler.toSource(); } }
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(); }
private Compiler mockRealJsCompiler(JSError error, Result res, String toSource) { Compiler result = createMock(Compiler.class); expect(result.compile(EasyMock.<List<JSSourceFile>>anyObject(), EasyMock.<List<JSSourceFile>>anyObject(), isA(CompilerOptions.class))).andReturn(res); if (error != null) { expect(result.hasErrors()).andReturn(true); expect(result.getErrors()).andReturn(new JSError[] { error }); } else { expect(result.hasErrors()).andReturn(false); } expect(result.getResult()).andReturn(res); expect(result.toSource()).andReturn(toSource); replay(result); return result; }
/** * Compiles a list of inputs. */ public <T1 extends SourceFile, T2 extends SourceFile> Result compile( List<T1> externs, List<T2> inputs, CompilerOptions options) { // The compile method should only be called once. Preconditions.checkState(jsRoot == null); try { init(externs, inputs, options); if (hasErrors()) { return getResult(); } return compile(); } finally { Tracer t = newTracer("generateReport"); errorManager.generateReport(); stopTracer(t, "generateReport"); } }
/** * Compiles a list of modules. */ public <T extends SourceFile> Result compileModules(List<T> externs, List<JSModule> modules, CompilerOptions options) { // The compile method should only be called once. Preconditions.checkState(jsRoot == null); try { initModules(externs, modules, options); if (hasErrors()) { return getResult(); } return compile(); } finally { Tracer t = newTracer("generateReport"); errorManager.generateReport(); stopTracer(t, "generateReport"); } }
private Compiler mockRealJsCompiler(JSError error, Result res, String toSource) { Compiler result = createMock(Compiler.class); expect(result.compile(EasyMock.<List<JSSourceFile>>anyObject(), EasyMock.<List<JSSourceFile>>anyObject(), isA(CompilerOptions.class))).andReturn(res); if (error != null) { expect(result.hasErrors()).andReturn(true); expect(result.getErrors()).andReturn(new JSError[] { error }); } else { expect(result.hasErrors()).andReturn(false); } expect(result.getResult()).andReturn(res); expect(result.toSource()).andReturn(toSource); replay(result); return result; }
protected CompileResult doCompileContent(JsContent content, CompilerOptions options, List<SourceFile> externs) throws CompilerException { Compiler compiler = new Compiler(getErrorManager()); // We shouldn't reuse compilers // disable JS Closure Compiler internal thread compiler.disableThreads(); SourceFile source = SourceFile.fromCode(content.getSource(), content.get()); Result result = compiler.compile(externs, Lists.newArrayList(source), options); if (result.errors.length > 0) { throw new CompilerException(result.errors); } return new CompileResult(compiler, result); }
@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()); }
@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(); } }