question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Workers using CommonJS syntax

See original GitHub issue

Feature request

What is the expected behavior? For Webpack to support workers using CommonJS syntax when targeting node.

The following works in Node:

const { Worker } = require("worker_threads");
const path = require("path");

const worker = new Worker(path.join(__dirname, "./worker.js"));

But is not currently transformed by Webpack.

What is motivation or use case for adding/changing the behavior? https://github.com/pinojs/pino/issues/1465

Essentially looking for a way that pino could support bundling it’s workers without needing a specialized plugin for each existing bundler.

P.S. It could be useful for libraries starting workers dynamically to be able to get the URL/filename of a transformed worker module bundle instead of just immediately starting it or receiving a custom class that starts it.

How should this be implemented in your opinion? Recognize that as an additional syntax and transform it appropriately in a similar way the existing worker syntax is transformed.

Are you willing to work on this yourself? yes (But I’m not familiar with Webpack internals, and not sure where to start learning them in the docs).

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
vankopcommented, Jun 20, 2022

please use esm syntax as suggested with new URL(...). cjs syntax very unlikely will be supported…

0reactions
alexander-akaitcommented, Jun 28, 2022

Yes, I think small and clear API will be great here

Read more comments on GitHub >

github_iconTop Results From Across the Web

CommonJS vs. ES modules in Node.js - LogRocket Blog
Learn about the differences between CommonJS and ES modules when using them in Node.js applications to organize software code.
Read more >
How the module system, CommonJS & require works
Learn how the Node.js module system & CommonJS works and what does `require` ... The simplest example can be the following using CommonJS:...
Read more >
Modules in JavaScript – CommonJS and ESmodules Explained
Hi everyone! In this article we're going to take a look at modules in JavaScript. Modules are a technique heavily used in today's...
Read more >
ES modules in service workers - web.dev
ES modules can be imported in one of two ways: either statically, using the import ... from '...' syntax, or dynamically, using the...
Read more >
JavaScript - Parcel
To use ES module or CommonJS syntax in a service worker, use the type: 'module' option as described in Classic scripts above. Parcel...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found