Socket
Socket
Sign inDemoInstall

@desmos/esbuild-worker-dedupe

Package Overview
Dependencies
93
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @desmos/esbuild-worker-dedupe

```sh yarn yarn example # check the JS console to see that the main thread and worker code executed successfully. cat example/bundle.ts # notice that the code from shared.ts only appears once ```


Version published
Weekly downloads
432
decreased by-18.03%
Maintainers
2
Created
Weekly downloads
 

Readme

Source

Proof of concept for inlining web worker code with esbuild with shared dependencies deduplicated.

Try it

yarn
yarn example # check the JS console to see that the main thread and worker code executed successfully.
cat example/bundle.ts # notice that the code from shared.ts only appears once

How it works

The idea is to use a two-step process:

First, take advantage of code splitting to factor out the common dependencies between the main entrypoint and the worker, which produces three "chunks", that look sort of like:

main.js

import { SharedThing } from "./shared.js";
// src/main.ts
import { createWorker } from "inlined-worker!./worker"; // NOTE: we intentionally skipped this import in the first step.
// We'll come back to it in the next step.
console.log("main", new SharedThing().id);
createWorker();

worker.js

import { SharedThing } from "./shared.js";
// src/worker.ts
function doSomething() {
  console.log("worker", new SharedThing().id);
}
doSomething();

shared.js

// src/shared.ts
var SharedThing = class {
  constructor() {
    this.id = Math.random();
  }
};
export { SharedThing };

We don't write these chunks to disk -- our plugin configures ESBuild to generate them in memory.

Next, we manually combine these three chunks into a single one as follows:

  1. From shared.js, emit code that: a. wraps the original source in a function, __sharedModuleFn. b. evaluates that function immediately to provide the actual exports from shared.js (e.g. SharedThing in the example above), storing them as __sharedModuleExports
  2. From worker.js, emit code that creates a __workerFn function that accepts an object holding the exports from the shared module, and then code that uses Function.toString() to assemble both of these, at runtime, into a string containing the full source of the worker. This string is used to create an object URL with which to instantiate the worker.

FAQs

Last updated on 31 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc