闭包编译器 es6 let 关键字

closure compiler es6 let keyword

我无法使用 Google Closure Compiler 编译带有 let 关键字的简单 Javascript 代码。

我的 Javascript 文件 example.js:

function display() {
    let a = 'hello';
    console.log( a, 'world' );
}
display();

我的 JAVA 编译代码是:

package compiler;

import static com.google.javascript.jscomp.SourceFile.fromCode;
import static com.google.javascript.jscomp.SourceFile.fromInputStream;
import static java.nio.charset.Charset.defaultCharset;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.google.javascript.jscomp.CompilationLevel;
import com.google.javascript.jscomp.Compiler;
import com.google.javascript.jscomp.CompilerOptions;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.JSError;
import com.google.javascript.jscomp.Result;
import com.google.javascript.jscomp.SourceFile;

public class CompilerMain {

    public static void main(String[] args) throws URISyntaxException, IOException {

        final String script = readScript();

        final Compiler compiler = new Compiler();
        final CompilerOptions options = new CompilerOptions();
//      CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
        CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
        options.setContinueAfterErrors(true);
        options.setLanguageIn(LanguageMode.ECMASCRIPT6);
        options.setLanguageOut(LanguageMode.ECMASCRIPT5);
        options.setExternExports(true);

        System.out.println("Compiling:\n" + script);
        System.out.println("------------------------------");

        final List<SourceFile> externs = new ArrayList<>();
        externs.add( externSourceFileES6("base.js"));
        externs.add( externSourceFileES6("es6_runtime.js"));
//      externs.add( externSourceFileES6("runtime_type_check.js"));

        final List<SourceFile> inputs = new ArrayList<>();
        final SourceFile src = fromCode("stdin.txt", script);
        inputs.add(src);
        final Result result = compiler.compile(externs, inputs, options);

        System.out.println("------------------------------");

        final JSError[] errors = result.errors;
        if (errors.length > 0) {
            for (JSError error : errors) {
                System.err.println(error.toString());
            }
        } else {
            System.out.println(compiler.toSource());
        }

    }

    private static SourceFile externSourceFileES6(final String filename) throws IOException {

        final InputStream inputstream = CompilerMain.class
                .getResourceAsStream("/com/google/javascript/jscomp/js/" + filename);

        return fromInputStream(filename, inputstream, defaultCharset());
    }

    private static String readScript() throws URISyntaxException, IOException {
        final URI uri = CompilerMain.class.getResource("/compiler/example1.js").toURI();
        final Path path = Paths.get(uri);
        return Files.readAllLines(path).stream().collect(Collectors.joining("\n"));
    }

}

输出为:

Compiling:
function display() {
    let a = 'hello';
    console.log( a, 'world' );
}
display();
------------------------------
mars 22, 2017 4:48:02 PM com.google.javascript.jscomp.LoggerErrorManager println
GRAVE: ERROR - Missing externs definition for Symbol. Did you forget to include the ES6 externs?

mars 22, 2017 4:48:02 PM com.google.javascript.jscomp.LoggerErrorManager printSummary
AVERTISSEMENT: 1 error(s), 0 warning(s), 75.0% typed
------------------------------
JSC_MISSING_ES6_EXTERNS. Missing externs definition for Symbol. Did you forget to include the ES6 externs? at (unknown source) line (unknown line) : (unknown column)

Maven 依赖项可能对您有所帮助:

    <dependency>
        <groupId>com.google.javascript</groupId>
        <artifactId>closure-compiler</artifactId>
        <version>v20170124</version>
    </dependency>

请指教

您需要通过调用 getBuiltinExterns 添加适当的外部设置。编译器需要语言 externs。