I have recently refactored my EmberJS application to use ember-cli, which requires the use of ES6 module syntax. I have succeeded in refactoring the application, and everything is working fine. The ember-cli packaging is taking my source code and producing a big app.js with my code translated to the following format:
define('app/adapters/application', ['exports', 'ember', 'ember-data', ...], function (exports, Ember, DS, ...) {
...
});
(Side question: how is this "format" called?)
But I have one file which is not controlled by ember-cli: a language file which depends on the settings of the user being logged-in. This must be requested from the backed. I am requesting this via a generated script tag, by using this function:
function loadScript(url, callback) {
var myScript = document.createElement("script");
myScript.setAttribute("type", "text/javascript");
myScript.setAttribute("src", url);
if (callback) {
myScript.onload = callback;
} else {
myScript.onload = function () {
if (DEBUG) {
console.log("DONE > Script from url=%s finished loading", url);
}
};
}
if (DEBUG) {
console.log("loadScript > loading myScript=%o", myScript);
}
document.getElementsByTagName("head")[0].appendChild(myScript);
}
Where the url is dynamically computed depending on the user's language settings. The problem is that the file I am getting from the server is also in ES6 format, but has not been pre-processed. The browser is complaining:
Uncaught SyntaxError: Unexpected reserved word
It has the normal ES6 format:
import xxx from 'yyy';
...
export default zzz;
I would like to integrate this file with my the rest of the code-base, so that:
- it can import from other modules
- other modules (even the ones loaded before it!) can import from it
How can I pre-process this file so that it can be loaded from the backend after the application has been loaded?
Or do I need to use a ES6 module loader? Will System.import('mymodule') perform a request to the backend to load the required module? To which URL? Is it possible to configure the URL used by import?
Aucun commentaire:
Enregistrer un commentaire