Tuesday, February 20, 2018

Using ECMAScript 6 modules

This may be obvious to everyone else, but today I found myself constructing a toy HTML/JS example and wanting to use the new hotness of ECMAScript 6 Modules.

I constructed the following page.

export default function crossPlatformSpanTextUpdate(span, text) {
if ('textContent' in span) {
span.textContent = text;
} else {
span.innerText = text;
}
}
<html>
<head>
<title>An E-commerce retailer</title>
</head>
<body>
<h1>
Hello <span id="greeting">Placeholder</span>!</h1>
<h2>
Please <a href="https://www.blogger.com/buy/12345">buy</a> this product</h2>
</body>
<script src="greeting.js" type="module"></script>
<script type="text/javascript">
document.onready = greetCustomer(document.querySelector("#greeting"));
</script>
</html>
view raw demo.html hosted with ❤ by GitHub
import {getUserPreferredLanguage} from './language.js'
import {crossPlatformSpanTextUpdate} from './cross_platform_span_update.js'
export default function greetCustomer(location) {
loadGreeting(location, getUserPreferredLanguage());
}
function loadGreeting(span, lang) {
var text = greeting(lang);
crossPlatformSpanTextUpdate(span, text);
}
function greeting(lang) {
if (lang === "es") {
return "Dear dear Customer";
}
return "Valued Shopper";
}
view raw greeting.js hosted with ❤ by GitHub
/*
used for language detection from
https://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference
*/
export default function getUserPreferredLanguage() {
return window.navigator.languages[0] ||
[window.navigator.language || window.navigator.userLanguage];
}
view raw language.js hosted with ❤ by GitHub

This refused to work. I kept getting

 ReferenceError: greetCustomer is not defined 

Reading the docs a little more, I decided to change it. I changed my script tag to type module and removed the onready function bit. No joy.

<html>
<head>
<title>An E-commerce retailer</title>
</head>
<body>
<h1>
Hello <span id="greeting">Placeholder</span>!</h1>
<h2>
Please <a href="https://www.blogger.com/buy/12345">buy</a> this product</h2>
</body>
<script src="greeting.js" type="module"></script>
<script type="module">
greetCustomer(document.querySelector("#greeting"));
</script>
</html>
view raw demo.html hosted with ❤ by GitHub

I tried importing directly, using an example I'd seen. Now I got:

SyntaxError: import not found: getUserPreferredLanguage

I finally got things working with the following

<html>
<head>
<title>An E-commerce retailer</title>
</head>
<body>
<h1> Hello <span id="greeting">Placeholder</span>!</h1>
<h2> Please <a href="buy/12345">buy</a> this product</h2>
</body>
<script src="greeting.js" type="module"></script>
<script type="module">
import greetCustomer from './greeting.js'
greetCustomer(document.querySelector("#greeting"));
</script>
</html>
view raw demo.html hosted with ❤ by GitHub
import getUserPreferredLanguage from './language.js'
import crossPlatformSpanTextUpdate from './cross_platform_span_update.js'
export default function greetCustomer(location) {
loadGreeting(location, getUserPreferredLanguage());
}
function loadGreeting(span, lang) {
var text = greeting(lang);
crossPlatformSpanTextUpdate(span, text);
}
function greeting(lang) {
if (lang === "es") {
return "Dear dear Customer";
}
return "Valued Shopper";
}
view raw greeting.js hosted with ❤ by GitHub
It seems like importing modules via the destructuring syntax by default is not the way to go.