dojo构建系统无法识别es6语法

bxjv4tth  于 2022-12-08  发布在  Dojo
关注(0)|答案(2)|浏览(143)

我正在做一个dojo项目(1.11.x),最近开始使用ES6(ES 2015)语法,比如const、let和template文字。在我使用dojo-util构建项目之前,它一直工作得很好。我遇到了如下错误

ERROR - Parse error. TypeError: redeclaration of const {variable name}
ERROR - Parse error. illegal character
                     return `<a href="/xxx/xxx/${a}">${b}</a>`;
                            ^

有没有办法让构建系统识别ES6语法或者绕过语法检查?

kcugc4gi

kcugc4gi1#

2016年12月发布的最新Dojo 1.12版本已更新为使用支持ES6到ES5转换的闭包编译器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来避免在ES6模块中“使用strict”:ECMASCRIPT5_STRICTmention here相同,但它会破坏Dojo本身。

xdnvmnnf

xdnvmnnf2#

由于Dojo 1.x上的开发似乎已经停滞,并且无法轻松迁移到Dojo 2.x,我们不得不想出一个解决方案。作为开发人员,我们之所以停留在ES 5特性上,仅仅是因为构建过程无法处理它,这是越来越荒谬的。
这就是为什么我想出了一个解决方案,目前正在我们公司测试。对于感兴趣的人,这是我们如何解决这个问题(仍然使用Dojo构建过程的核心部分):

  • 在构建配置文件中禁用优化(layerOptimize:false,优化:错误)
  • 在package. js中将所有小部件标记为非AMD(amd:函数(){返回false;})
  • 这会导致所有小部件在构建中得到警告,但不会失败
  • 在构建过程完成后,所有的“层”文件都是完整的(可能包括ES6的特性),但没有被缩小,因此文件的大小非常大。
  • 在Maven(或您选择构建工具)中,运行任何适合您的小型化器。
  • 我们使用具有以下设置的最新Closure编译器,它将ES 2017代码转换为ES 5。
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
      <exec executable="java" resolveexecutable="true" failonerror="true">
        <arg value="-jar" />
        <arg value="${google.closure.compiler.es2017}" />
        <arg value="--language_in=ECMASCRIPT_2017" />
        <arg value="--language_out=STABLE" />
        <arg value="--js" />
        <arg value="${dojo.build.path}/build/layers/OriginalLayer.js" />
        <arg value="--js_output_file" />
        <arg value="${dojo.build.path}/build/layers/MinifiedLayer.js" />
      </exec>
    </execution>
  </executions>
</plugin>

这仍然是实验性的,但第一个结果看起来很好,似乎没有回归。

相关问题