Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/WorkerWithImportMapViaBedfordsShim.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ self.addEventListener('message', function(e) {
if (val[0] === '.') {
const url = baseURL + '/' + val;
importMap.imports[key] = url;
console.log(`Update baseURL from ${val} to ${url}`);
// console.log(`Update baseURL from ${val} to ${url}`);
}
}
}
importScripts(shimCodeUrl);
importShim.addImportMap(importMap);
importShim(scriptURL)
.then(() => {
console.log(`${scriptURL} worker has been loaded`);
// console.log(`${scriptURL} worker has been loaded`);
})
.catch(e => {
setTimeout(() => {
Expand Down
16 changes: 11 additions & 5 deletions src/WorkerWithImportMapViaInlineFrame.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import {EventHandler} from './EventHandler.js';
import {getImportMap} from './getImportMap.js';
window.workersReady = {};
class WorkerWithImportMapViaInlineFrame extends EventHandler {
debug = false;
iframe = document.createElement('iframe');
callbackId = `cb${Math.floor(Math.random()*1000000000)}`;
terminateId = `tm${Math.floor(Math.random()*1000000000)}`;
/**
* @param {URL | string} script - The worker URL.
* @param {object} [options] - The options.
* @param {object|'inherit'} [options.importMap] - The import map or simply `inherit`.
* @returns
*/
constructor(script, options = {}) {
super();
const {iframe, callbackId, terminateId} = this;
if (options.inheritMap) {
const mapEl = document.querySelector('script[type="importmap"]');
options.map = JSON.parse(mapEl.innerHTML);
if (options.importMap === 'inherit') {
options.importMap = getImportMap();
}
if (!options.map) {
if (!options.importMap) {
return new window.Worker(script, options);
}
window.workersReady[terminateId] = function(window) {
Expand All @@ -26,7 +32,7 @@ class WorkerWithImportMapViaInlineFrame extends EventHandler {
const html = `
<html>
<head>
<script type="importmap">${JSON.stringify(options.map)}</script>
<script type="importmap">${JSON.stringify(options.importMap)}</script>
</head>
<body onload="parent.workersReady.${callbackId}(this.window)">
<script>
Expand Down
4 changes: 2 additions & 2 deletions test/test-workerframe-script.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {add} from 'calculator';
self.onmessage = (e) => {
console.log("self.onmessage e", e);
// console.log("self.onmessage e", e);
const {data} = e;
self.postMessage({
message: `self.onmessage> Received: ${data} and add(1, 2) is ${add(1, 2)}`
});
};
self.addEventListener('message', (e) => {
console.log("self.addEventListener('message', e)", e);
// console.log("self.addEventListener('message', e)", e);
const {data} = e;
self.postMessage({
message: `self.addEventListener('message', e)> Received: ${data} and add(1, 2) is ${add(1, 2)}`
Expand Down
3 changes: 1 addition & 2 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// WorkerFrame...
import {WorkerWithImportMapViaInlineFrame, WorkerWithImportMapViaBedfordsShim} from 'esm-worker';
const url = new URL('./test-workerframe-script.js', import.meta.url);
//const workerFrame = new WorkerWithImportMapViaBedfordsShim(url, {type: 'module', importMap: 'inherit'});
const workerFrame = new WorkerWithImportMapViaInlineFrame('./test-workerframe-script.js', {type: 'module', inheritMap: true});
const workerFrame = new WorkerWithImportMapViaBedfordsShim(url, {type: 'module', importMap: 'inherit'});
function stringify(_) {
// return JSON.stringify(_, null, 2);
return JSON.stringify(_);
Expand Down