npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@desmos/esbuild-worker-dedupe

v3.0.0

Published

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

Downloads

3,277

Readme

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.