我正在开发一个 dojo 项目 (1.11.x),最近开始使用 ES6(ES2015) 语法,例如 const、let 和模板文字。在我使用 dojo-util 构建项目之前,它运行良好。我有如下错误
ERROR - Parse error. TypeError: redeclaration of const {variable name}
ERROR - Parse error. illegal character
return `<a href="/xxx/xxx/${a}">${b}</a>`;
^
Run Code Online (Sandbox Code Playgroud)
有没有办法让构建系统识别 ES6 语法或绕过语法检查?
2016 年 12 月最新发布的Dojo 1.12更新为使用 Closure Compiler 20160911,它支持将 ES6 转换为 ES5。
我在一个项目中拥有旧的 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
Run Code Online (Sandbox Code Playgroud)
应用程序/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()
})
Run Code Online (Sandbox Code Playgroud)
然后在你的 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'
]
},
...
Run Code Online (Sandbox Code Playgroud)
应用程序/Main.js
define(["app/es6/Dialog"], function(Dialog) {
Dialog.show();
});
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以将 ES6 集成到您当前的 Dojo 项目中。
我也试图避免通过设置ES6模块“使用严格的” languageOut:ECMASCRIPT5_STRICT如在这里提到,但它打破道场本身。
| 归档时间: |
|
| 查看次数: |
1485 次 |
| 最近记录: |