-
-
Save Valexr/d402462df0cd46758c79f9d863652cde to your computer and use it in GitHub Desktop.
Revisions
-
manzt revised this gist
Jul 29, 2021 . No changes.There are no files selected for viewing
-
manzt revised this gist
Jul 28, 2021 . 1 changed file with 2 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ # `esbuild-plugin-inline-webworker` ## Usage @@ -21,9 +21,6 @@ postMessage(hello()); import esbuild from 'esbuild'; import { PluginInlineWorker } from './plugin-inline-webworker.js'; esbuild.build({ entryPoints: ['./index.ts'], bundle: true, @@ -34,7 +31,7 @@ esbuild.build({ ```bash node build # // inline-worker:/Users/hms-manzt/demos/esbuild-plugin-inline-webworker/worker # var worker_default = "data:application/javascript;base64,KCgpPT57dmFyIG89KCk9PiJ3b3JsZCI7cG9zdE1lc3NhZ2UobygpKTt9KSgpOwo="; # # // index.ts -
manzt created this gist
Jul 28, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ # Simple plugin to bundle a web-worker inline with esbuild. ## Usage ```typescript // ./index.ts import workerSrc from 'inline-worker:./worker.ts'; let worker = new Worker(workerSrc); worker.onmessage = e => console.log(e.data); // 'world' // ./util.ts export const hello = () => 'world'; // ./worker.ts import { hello } from './util.ts'; postMessage(hello()); ``` ```javascript // ./build.js import esbuild from 'esbuild'; import { PluginInlineWorker } from './plugin-inline-webworker.js'; import esbuild from 'esbuild'; import { PluginInlineWorker } from './plugin-inline-webworker.js'; esbuild.build({ entryPoints: ['./index.ts'], bundle: true, format: 'esm', plugins: [PluginInlineWorker()], }) ``` ```bash node build # // inline-worker:/Users/hms-manzt/demos/esbuild-plugin-bundle-webworker/worker # var worker_default = "data:application/javascript;base64,KCgpPT57dmFyIG89KCk9PiJ3b3JsZCI7cG9zdE1lc3NhZ2UobygpKTt9KSgpOwo="; # # // index.ts # var worker = new Worker(worker_default); # worker.onmessage = (e) => console.log(e.data); ``` This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ import esbuild from "esbuild"; import { resolve } from "path"; /** * @param {Pick<import('esbuild').BuildOptions, 'minify' | 'format' | 'plugins'>} * @return {import('esbuild').Plugin} */ export const PluginInlineWorker = (opt = {}) => { const namespace = "inline-worker"; const prefix = `${namespace}:`; return { name: namespace, setup(build) { build.onResolve({ filter: new RegExp(`^${prefix}`) }, (args) => { return { path: resolve(args.resolveDir, args.path.slice(prefix.length)), namespace, }; }); build.onLoad({ filter: /.*/, namespace }, async (args) => { const { outputFiles } = await esbuild.build({ entryPoints: [args.path], bundle: true, write: false, format: opt.format || "iife", minify: opt.minify || true, target: build.initialOptions.target, plugins: [ ...(build.initialOptions.plugins || []), ...(opt.plugins || []), ], }); if (outputFiles.length !== 1) { throw new Error("Too many files built for worker bundle."); } const { contents } = outputFiles[0]; const base64 = Buffer.from(contents).toString("base64"); return { loader: "js", contents: `export default "data:application/javascript;base64,${base64}";`, }; }); }, }; };