{"id":1099,"date":"2017-10-19T16:54:39","date_gmt":"2017-10-19T16:54:39","guid":{"rendered":"https:\/\/www.codedread.com\/blog\/?p=1099"},"modified":"2021-06-03T15:40:32","modified_gmt":"2021-06-03T15:40:32","slug":"web-workers-can-be-es6-modules-too","status":"publish","type":"post","link":"https:\/\/www.codedread.com\/blog\/archives\/2017\/10\/19\/web-workers-can-be-es6-modules-too\/","title":{"rendered":"Web Workers can be ES6 Modules too"},"content":{"rendered":"\n<p>[<strong>Update 2021-06<\/strong>: Chrome and Edge have supported ES6 Modules in Dedicated Workers since Nov 2019. The equivalent Webkit bug is <a href=\"https:\/\/bugs.webkit.org\/show_bug.cgi?id=164860\">https:\/\/bugs.webkit.org\/show_bug.cgi?id=164860<\/a> which seems fixed since April 2021. Yay! The equivalent Mozilla bug is <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=1247687\">https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=1247687<\/a> which work seems to have now started. Yay! Follow along on <a href=\"https:\/\/caniuse.com\/mdn-api_worker_worker_ecmascript_modules\">Can I use<\/a>!]<\/p>\n\n\n<p><a href=\"https:\/\/reactjs.org\/\"><img loading=\"lazy\" decoding=\"async\" style=\"float: right;\" src=\"http:\/\/codedread.com\/clipart\/react.svgz\" alt=\"ReactJS logo\" width=\"100\" height=\"100\"><\/a><\/p>\n<p>I've been playing around a bit with <a href=\"https:\/\/reactjs.org\/\">React<\/a>, since that's where all the hot and shiny is lately. I really like it, but I can see some of the hot and shiny warts - I'll blog about that later after I've <span style=\"text-decoration: line-through;\">been fully brainwashed<\/span> had more time to play with it.<\/p>\n<p>For the purposes of this exercise, one thing I'm trying to do is go the <a href=\"https:\/\/shinglyu.github.io\/web\/2016\/04\/06\/minimal_react.html\">no-build-step route<\/a>, where it's all pure HTML and JavaScript. This means no <a title=\"&#34;Let's address the elephant in the room first. You will be writing XML with your React code.&#34;\" href=\"http:\/\/buildwithreact.com\/tutorial\/jsx\">JSX<\/a>, no npm, no Babel. It's really not too bad at all (unfortunately almost all examples I've seen online use JSX - and understandably).<\/p>\n<p>It's also given me a good stretch at using ES6 modules natively, one hot and shiny wart I've noticed there too.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"float: right;\" src=\"http:\/\/codedread.com\/clipart\/javascript.svgz\" alt=\"JavaScript logo\" width=\"100\" height=\"100\"><\/p>\n<p>Web Workers are a way to write JavaScript that executes off the \"main thread\" of your web app. You pass messages to it, you listen for messages coming back from it. They were introduced a long time ago (seven years!), long before ES6 became a reality, and certainly long before it became possible to <a title=\"Yep, I'm plugging my own blog entries again like it was the early 2000s\" href=\"https:\/\/www.codedread.com\/blog\/archives\/2017\/10\/17\/the-dawn-of-modern-javascript\/\">use ES6 modules natively<\/a>.<code><br>\n&#160;&#160;const worker = new Worker('sketchy.js');<br>\n&#160;&#160;worker.onmessage = function(msg) {<br>\n&#160;&#160;&#160;&#160;\/\/ do something with messages sent back by worker<br>\n&#160;&#160;};<br>\n&#160;&#160;worker.postMessage({...}); \/\/ send something to the worker<br>\n<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"float: right;\" src=\"http:\/\/codedread.com\/clipart\/villain.svgz\" alt=\"A picture of a comic villain\" width=\"100\" height=\"100\"><\/p>\n<p>Workers operate in a different JavaScript context, a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/WorkerGlobalScope\">WorkerGlobalScope<\/a>, not the global Window context that normal JavaScript files do. As a result, they seem like a decent way for me to execute user code (that is, code entered by the user into an editor on the page) and avoid common security problems with this, since they do not have access to the DOM, the browser window, cookies, etc. Someone let me know if I'm wrong about this!<\/p>\n<p>Anyway, that's why I want to use Workers. But all the other times I've used Workers, I've hit up against the fact that they are not modules and can't import other modules. This isn't great if you're trying to change over all your project to ES6 modules. Code should be sharable between your main web app, your Web Workers, your Service Workers, etc.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"float: right;\" src=\"http:\/\/codedread.com\/clipart\/rollercoaster.svgz\" alt=\"A picture of a rollercoaster\" width=\"200\" height=\"200\"><\/p>\n<p>Well turns out I was just using it wrong - kinda.<\/p>\n<p>When you create a Worker, you can also <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Worker\/Worker\">specify options<\/a>, and one of those options lets you specify that the Worker is a Module:<code><br>\n&#160;&#160;const worker = new Worker('sketchy.js', {type:'module'});<br>\n<\/code><\/p>\n<p>This is great, except for one thing... Chrome doesn't seem to support this yet. <a href=\"https:\/\/bugs.chromium.org\/p\/chromium\/issues\/detail?id=680046\">Bug 680046<\/a>. But good news - work seems to have gotten started on that this month! One can hope that we'll soon be in a world where we can be all modules, all the time.<\/p>","protected":false},"excerpt":{"rendered":"<p>[Update 2021-06: Chrome and Edge have supported ES6 Modules in Dedicated Workers since Nov 2019. The equivalent Webkit bug is https:\/\/bugs.webkit.org\/show_bug.cgi?id=164860 which seems fixed since April 2021. Yay! The equivalent Mozilla bug is https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=1247687 which work seems to have now started. Yay! Follow along on Can I use!] I&#8217;ve been playing around a bit with [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,25,11,28],"tags":[],"class_list":["post-1099","post","type-post","status-publish","format-standard","hentry","category-javascript","category-software","category-technology","category-web"],"_links":{"self":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/1099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/comments?post=1099"}],"version-history":[{"count":18,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/1099\/revisions"}],"predecessor-version":[{"id":1297,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/1099\/revisions\/1297"}],"wp:attachment":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/media?parent=1099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/categories?post=1099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/tags?post=1099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}