关闭编译器es6 let关键字

时间:2017-03-22 15:54:14

标签: javascript java ecmascript-6 google-closure-compiler

我无法使用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>

请告知

1 个答案:

答案 0 :(得分:5)

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