Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to transpile node_modules #141

Open
farsil opened this issue Jan 12, 2023 · 4 comments
Open

Unable to transpile node_modules #141

farsil opened this issue Jan 12, 2023 · 4 comments

Comments

@farsil
Copy link

farsil commented Jan 12, 2023

Hello,

My application requires certain dependencies in node_modules to be transpiled. However, it seems that babel-watch completely ignores node_modules no matter what my config is.

I looked in the source code and noticed this if at line 68 in runner.js:

if (filename.split(path.sep).indexOf('node_modules') < 0) {
  babelWatchLoader(module_, filename, defaultHandler);
} else {
  defaultHandler(module_, filename);
  if (filename.indexOf('/node_modules/source-map-support/') !== -1 && module_.exports.install !== undefined) {
    // When app is running in babel-watch the source-map-support library should not be used by separately. The
    // runner process is initializing source-map-support passing a special source map loader that makes it possible
    // to use maps generated by the "parent" process instead of reading them from the filesystem.
    // We don't allow for source-map-support library to be reinitialized
    module_.exports.install = () => {};
  }
}

which could explain the exclusion. I am not a babel expert by any means, but is there a way to circumvent this restriction?

@STRML
Copy link
Collaborator

STRML commented Jan 12, 2023

Hi. I'm sure you could modify the library or maybe even symlink the dependency into a src folder so babel-watch can see it. But generally this is not a supported use case. Your node modules, not being project source, should be runnable as they sit on disk. Given they should only be transpiled once in any case, it makes little sense to have them watched by a file watcher.

@farsil
Copy link
Author

farsil commented Jan 12, 2023

I agree with you, but unfortunately the packages I am using are not runnable as they are because they are not distributed in CJS format and I can't do much about it. I'll try to find a working configuration for my project.

@STRML
Copy link
Collaborator

STRML commented Jan 12, 2023

Yeah - packages shouldn't be distributed that way! But that said a symlink or a pre build step before babel watch runs, might fix it for you.

@IMalyugin
Copy link
Contributor

IMalyugin commented Jan 18, 2023

babel-watch is meant to extend the behaviour of @babel/node with fast reload. According to the conversation: babel/babel#8802 @babel/node has "ignore" regex option that is set to node_modules by default, but it can be overridden.

Might be a viable idea to add a similar setting to babel-watch. As long as it doesn't cause issues with cascaded dependencies. As for only transpiling it once, a lot of different builds use webpack or similar tools, setting exception for node_modules they support.

I used to always write nodejs dependencies in pure cjs, without transpilation to avoid this but recently I had to pull some ts-dependencies into NodeJS - and yep, had to rewrite the distribution system to provide both esm/cjs, while generating package.json "exports" on the fly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants