2016 年 12 月的最新版本 Dojo 1.12 已更新为使用支持将 ES6 转换为 ES5 的 Closure Compiler 20160911。
我在一个项目中有旧的 ES5 模块和 ES6 中的新模块。
在 ES6 模块中你必须在开头添加 "use strict",否则构建失败。
error(307) Failed to evaluate module tagged as pure AMD
(fell back to processing with regular expressions). module: app/es6/Test;
error: SyntaxError: Block-scoped declarations (let, const, function, class)
not yet supported outside strict mode
app/es6/Dialog.js
"use strict"
define(["dijit/ConfirmDialog"], (ConfirmDialog) => {
let id = '1'
const dialog = new ConfirmDialog({
title: "Delete",
content: `Are you sure you want to delete ${id} ?`,
style: "width: 300px"
})
dialog.show()
})
然后在你的 app.profile.js 中添加 optimizeOptions 对象
...
optimizeOptions: {
languageIn: 'ECMASCRIPT6',
languageOut: 'ECMASCRIPT5'
},
layerOptimize: "closure.keeplines",
optimize: "closure.keeplines",
cssOptimize: "comments",
mini: true,
stripConsole: "all",
selectorEngine: "lite",
useSourceMaps: false,
...
layers: {
"dojo/dojo": {
includeLocales: [ 'en-us' ],
include: [ "dojo/dojo", "dojo/hash" ],
boot: true,
customBase: true
}
"app/Main": {
includeLocales: [ 'en-us' ],
include: [
'app/Header',
'app/Main'
]
},
...
app/Main.js
define(["app/es6/Dialog"], function(Dialog) {
Dialog.show();
});
这样您可以将 ES6 集成到您当前的 Dojo 项目中。
我还试图通过将 languageOut: ECMASCRIPT5_STRICT 设置为 mention here 来避免在 ES6 模块中“使用严格”,但这会破坏 Dojo 本身。