I have a question regarding the usage of ES6 modules within Safari. It's driving me crazy because it prevents me from sharing my website with Safari users.
My web application and the coming up MWE work perfectly fine in the following environments:
However, my web application does not work within MacOS Safari via my web server (at the moment I'm trying this code on Safari 13.0.4, but all modern versions of the mentioned browsers behave the same way with respect to this problem). Similarly, it does not work within the latest version of iOS Safari.
MWE
./test_class.js
export class TestClass {
constructor() {
console.log("Created a test class");
}
}
./index.html
<!DOCTYPE html>
<script type="module">
console.log("Starting the main script.");
// The following line seems to cause an error in Safari only
import { TestClass } from './test_class.js';
// The rest is not executed due to the error
let test_class = new TestClass;
console.log("Done.");
</script>
Console Output
When I load index.html from my server, Safari's console gives me the error
TypeError: 'text/html' is not a valid JavaScript MIME type.
Additional info (not sure if it's relevant)
When I go to the resource section of Safari, there is indeed an entry for test_class.js that does not contain the code above but instead:
<html>
<body>
<script type="text/javascript" src="/aes.js" ></script>
<script>
function toNumbers(d)
// ...
function toHex()
// ...
var a = toNumbers("..."),
b = toNumbers("..."),
c = toNumbers("...");
document.cookie = "...";
location.href="http://www.my_super_website.com/test_class.js?i=1";
</script>
</body>
</html>
This only occurs when I connect to my server. Does the line location.href="http://www.my_super_website.com/test_class.js?i=1";
make sense? My understanding is that at this point Safari tries to load test_class.js but has some issue with it.
What I've tried so far
import { TestClass } from './test_class.js';
, everything works fine.import
call within a new script file that is then used in the html document.In web development we develop using ES6 and ES7 but those are not supported yet in all browsers.
In order to ship the working code in most browsers (Safari - Chrome - IE9 )
and does not matter the browser. You supposed to use a build process like ( webpack - gulp - grant ) or using existing libraries like ( React, Angular and VUE )
Example: For starting using webpack https://webpack.js.org/guides/getting-started/
witch will grant you get a complete working code by converting all your code into ES5 and bundle that code to ensure reducing chuck sizes