kmagiera / babel-watch

Reload your babel-node app on JS source file changes. And do it fast.
MIT License
528 stars 70 forks source link

Unable to transpile node_modules #141

Open farsil opened 1 year ago

farsil commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

babel-watch is meant to extend the behaviour of @babel/node with fast reload. According to the conversation: https://github.com/babel/babel/issues/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.