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.

Babel-node does not transpile files references by workers. Can/should it?

See original GitHub issue

I find that using new Worker(file.js) (and file.js uses import statements), that babel loads the referenced script fine but babel-node doesn’t. Is this a bug or related to how babel-node works (my understanding of these things is not so good)?

I have just posted a StackOverflow question with more detail: https://stackoverflow.com/questions/59662352/how-to-get-babel-node-to-convert-files-referenced-by-workers-to-commonjs-like

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JLHwungcommented, Jan 14, 2020

babel-cli transforms all the JavaScript files inside the src folder. It operates on per-file level and does not process the module references.

How does Node allow 3rd party code to be executed per module extension?

Under the hood babel-node uses pirates, which overwrites Module._extension[".js"]. So when a module is loaded, the babel transform defined here will be executed: https://github.com/babel/babel/blob/a0a9c64a470acec77bec1da0b4bceec4ccf7f446/packages/babel-register/src/node.js#L62

1reaction
JLHwungcommented, Jan 9, 2020

The Worker constructor does not invoke Module constructor so babel-node could not hook into the loader and apply the transformation. You can use @babel/register to hook into any files loaded inside your worker:

file-wrapper.js

require("@babel/register")
require("file.js") // your worker entry

main.js

new Worker("file-wrapper.js")
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get babel-node to convert files referenced by Workers ...
Babel converts all the files to commonjs, and loading the worker script works. BUT. When I use @babel/node , this doesn't work: babel-node...
Read more >
babel/node - Babel.js
babel -node is a CLI that works exactly the same as the Node.js CLI, with the added benefit of compiling with Babel presets...
Read more >
TypeScript rules for Bazel - GitHub Pages
This will produce an opaque directory of .js file outputs, which you won't be able to individually reference. Any other use case for...
Read more >
Build Performance - webpack
The thread-loader can be used to offload expensive loaders to a worker pool. warning. Don't use too many workers, as there is a...
Read more >
Comparing Babel, Sucrase, and Similar Libraries
These browsers do not necessarily need to be compatible with the just added JavaScript feature. Here is an example of how Babel transpiles...
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