Closure 编译器不编译 ES6 库

Closure Compiler not Compiling ES6 library

我正在使用 Closure 编译器编写一个相当大的 JavaScript 库,但我制作了一个较小的模型作为此问题的示例:

src/main.js:

export function main(str) {
    console.log("From main: " + str);
}

命令:

google-closure-compiler --compilation_level ADVANCED_OPTIMIZATIONS --language_in ECMASCRIPT6 --language_out ECMASCRIPT6 --js_module_root src/ --module_resolution node --js src/main.js --js_output_file build/main.min.js

预期结果(大致):

export function main(str){console.log("From main: "+str)}

实际结果:

'use strict';

长话短说,我已经苦苦挣扎了几个小时,但我无法弄清楚如何让 Closure Compiler 遵守 ES6 "export" 语句。我试过谷歌搜索很多次,但我只能得到 "goog.modules" 的结果,这不是我想要的。任何帮助将不胜感激。

If you compile just the function below with ADVANCED_OPTIMIZATIONS, Closure Compiler produces empty output:

function displayNoteTitle(note) { alert(note['myTitle']); }

Because the function is never called in the JavaScript that you pass to the compiler, Closure Compiler assumes that this code is not needed!

Ref

您应该添加一个调用您的函数的部分

function displayNoteTitle(note) {
  alert(note['myTitle']);
}
displayNoteTitle({'myTitle': 'Flowers'});

或者您可以将其添加到 window 对象

function displayNoteTitle(note) {
  alert(note['myTitle']);
}
// Store the function in a global property referenced by a string:
window['displayNoteTitle'] = displayNoteTitle;

Live example

Closure Compiler 目前不支持模块作为输出(任何格式)。