Skip to content

Commit

Permalink
feat: do code transpilation on worker if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed Jan 31, 2024
1 parent 21578c4 commit 380000f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/to-systemjs-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { toSystemjs } from './to-systemjs.ts';

// @ts-ignore
self.addEventListener('message', async (event: MessageEvent<{ args: Parameters<typeof toSystemjs> }>) => {
const transpiledCode = await toSystemjs(...event.data.args);
// @ts-ignore
self.postMessage({ code: transpiledCode });
// @ts-ignore
self.close();
});
27 changes: 26 additions & 1 deletion src/to-systemjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
rollupPluginVirtual,
} from '../deps.ts';

export const toSystemjs = async (
export const toSystemjsMain = async (
esmCode: string,
rollupOutputOptions: OutputOptions = {},
): Promise<string> => {
Expand Down Expand Up @@ -34,3 +34,28 @@ export const toSystemjs = async (
await bundle.close();
return output[0].code;
};

export const toSystemjsWorker = async (
esmCode: string,
rollupOutputOptions: OutputOptions = {},
): Promise<string> => {
const worker = new Worker(import.meta.resolve('./to-systemjs-worker.ts'), { type: 'module' });
return new Promise((resolve) => {
worker.addEventListener('message', (event: MessageEvent<{ code: string }>) => {
resolve(event.data.code);
}, false);
worker.postMessage({
args: [esmCode, rollupOutputOptions],
});
});
}

export const toSystemjs = async (
esmCode: string,
rollupOutputOptions: OutputOptions = {},
): Promise<string> => {
if (typeof Worker !== 'undefined') {
return toSystemjsWorker(esmCode, rollupOutputOptions);
}
return toSystemjsMain(esmCode, rollupOutputOptions);
}

0 comments on commit 380000f

Please sign in to comment.