2016-07-24 10 views
1

Ich arbeite an einem Dojo-Projekt (1.11.x) und vor kurzem begann mit ES6 (ES2015) Syntax wie Konst, Let und Vorlage Literale. Es funktionierte gut, bis ich das Projekt mit dojo-util erstellen. Ich habe Fehler wie untenDojo Build-Systeme erkennt es6 nicht Syntax

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

Gibt es eine Möglichkeit der Überprüfung das Build-System erkennt ES6 Syntax oder umgeht die Syntax zu machen?

+0

Ich nehme an, Sie erwarten, dass Ihr Code nur auf einem Browser läuft, der ES6 unterstützt (also kein IE10 zum Beispiel)? Andernfalls müssen Sie Ihr ES6 zuerst in ES5 umwandeln, und es löst Ihr Problem – ben

+0

BTW, ich denke, das Problem liegt an der Optimierung (shrinksafe, closure oder uglifyjs) – ben

Antwort

1

Die neueste Version von Dojo 1.12 vom Dezember 2016 wurde aktualisiert, um den Closure Compiler 20160911 zu verwenden, der das Umsetzen von ES6 auf ES5 unterstützt.

Ich habe in einem Projekt ältere ES5-Module und neue in ES6.

In ES6-Modulen müssen Sie am Anfang "use strict" hinzufügen, sonst schlägt das Gebäude fehl.

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() 
}) 

Dann in Ihrem app.profile.js optimizeOptions Objekt

... 
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(); 
}); 
hinzufügen

Auf diese Weise können Sie ES6 in Ihr aktuelles Dojo-Projekt integrieren.

versuchte ich auch, indem languageOut "use strict" in ES6 Modulen zu vermeiden: ECMASCRIPT5_STRICT als mention here aber es bricht Dojo selbst.

+0

danke für die Antwort. Ich werde es versuchen. –

+1

Da dies immer noch nicht funktioniert, habe ich Dojo gebeten, dies zu klären. Siehe https://bugs.dojotoolkit.org/ticket/19020#ticket – PaulR

+0

Sie haben bestätigt, dass dies immer noch ein offenes Problem ist, also müssen wir warten. – PaulR