Java Code Examples for com.google.javascript.jscomp.Compiler#disableThreads()

The following examples show how to use com.google.javascript.jscomp.Compiler#disableThreads() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClosureCompiler.java    From AngularBeans with GNU Lesser General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 2
Source File: GoogleClosureOutputProcessor.java    From spring-soy-view with Apache License 2.0 5 votes vote down vote up
@Override
public void process(final Reader reader, final Writer writer) throws IOException {
    final String originalJsSourceCode = IOUtils.toString(reader);
    try {
        Compiler.setLoggingLevel(Level.SEVERE);
        final Compiler compiler = new Compiler();
        final CompilerOptions compilerOptions = newCompilerOptions();
        compilationLevel.setOptionsForCompilationLevel(compilerOptions);
        //make it play nice with GAE
        compiler.disableThreads();
        compiler.initOptions(compilerOptions);

        final SourceFile input = SourceFile.fromInputStream("dummy.js", new ByteArrayInputStream(originalJsSourceCode.getBytes(getEncoding())));
        final Result result = compiler.compile(Lists.<SourceFile>newArrayList(), Lists.newArrayList(input), compilerOptions);

        logWarningsAndErrors(result);

        boolean origFallback = false;
        if (result.success) {
            final String minifiedJsSourceCode = compiler.toSource();
            if (StringUtils.isEmpty(minifiedJsSourceCode)) {
                origFallback = true;
            } else {
                writer.write(minifiedJsSourceCode);
            }
        } else {
            origFallback = true;
        }
        if (origFallback) {
            writer.write(originalJsSourceCode);
        }
    } finally {
        reader.close();
        writer.close();
    }
}
 
Example 3
Source File: ClosureJavaScriptCompressor.java    From htmlcompressor with Apache License 2.0 4 votes vote down vote up
@Override
public String compress(String source) {
	
	StringWriter writer = new StringWriter();
	
	//prepare source
	List<JSSourceFile> input = new ArrayList<JSSourceFile>();
	input.add(JSSourceFile.fromCode("source.js", source));
	
	//prepare externs
	List<JSSourceFile> externsList = new ArrayList<JSSourceFile>();
	if(compilationLevel.equals(CompilationLevel.ADVANCED_OPTIMIZATIONS)) {
		//default externs
		if(!customExternsOnly) {
			try {
				externsList = getDefaultExterns();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//add user defined externs
		if(externs != null) {
			for(JSSourceFile extern : externs) {
				externsList.add(extern);
			}
		}
		//add empty externs
		if(externsList.size() == 0) {
			externsList.add(JSSourceFile.fromCode("externs.js", ""));
		}
	} else {
		//empty externs
		externsList.add(JSSourceFile.fromCode("externs.js", ""));
	}
	
	Compiler.setLoggingLevel(loggingLevel);
	
	Compiler compiler = new Compiler();
	compiler.disableThreads();
	
	compilationLevel.setOptionsForCompilationLevel(compilerOptions);
	warningLevel.setOptionsForWarningLevel(compilerOptions);
	
	Result result = compiler.compile(externsList, input, compilerOptions);
	
	if (result.success) {
		writer.write(compiler.toSource());
	} else {
		writer.write(source);
	}

	return writer.toString();

}