andrewn / neue-radio

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

Proposal: Bundle WebSocket code #95

Closed andrewn closed 6 years ago

andrewn commented 6 years ago

I'm proposing we use Rollup to bundle our shared WebSocket code for the browser and node.js. We've avoided this so far but it results in some limitations:

node.js

const createWebSocketOrWhatever = require('websocket');

const ws = createWebSocketOrWhatever();

script tag

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

ES Module

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

The above would also be usable within a module:

<script type="module">
 import createWebSocketYouKnow from '/websocket/es.js';
 const ws = createWebSocketYouKnow();
</script>
// external.html
<script type="module">
 import main from './main.js';
 main();
</script>

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

The only sad thing is /websocket/es.js. I think we could have /websocket/es if we get express to serve it but it will have to be a different URL.

pixelblend commented 6 years ago

If we change this line, would we be able to mount the es.js at /weboscket?

andrewn commented 6 years ago

Good idea, we can map es.js as /websocket.

In shared/websocket/package.json we could set "main": "umd.js" so that in node.js you will still require('websocket');