andrewn / neue-radio

Neue Radio: Prototype connected object using web technologies
10 stars 6 forks source link

Export websocket as ES module, node.js and legacy browser (fixes #95) #97

Closed andrewn closed 6 years ago

andrewn commented 6 years ago

Following from the discussion in #95, here's the implementation with ES being the primary export for the browser.

node.js

const createWebSocketOrWhatever = require('websocket');

const ws = createWebSocketOrWhatever();

script tag

<script src="/websocket/umd.js"></script>
<script>
  const ws = createWebsocket(); // this name is globally exported
</script>

ES Module

<script type="module">
 import createWebSocketYouKnow from '/websocket'
 const ws = createWebSocketYouKnow();
</script>

The above would also be usable within a module:

// external.html
<script type="module">
 import main from './main.js';
 main();
</script>

// main.js
import createWebSocketYouKnow from '/websocket';
const ws = createWebSocketYouKnow();

In this repo, we've mostly been using the script tag approach but I think going forward we should be using ES modules like we're doing in the more recent (private) apps. #96 tracks updating all the code.