-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
make babel injectable in babel-register #7273
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/6724/ |
Could you please update the documentation of babel-register? |
@xtuc ofcourse, my bad. I updated the PR. |
packages/babel-register/README.md
Outdated
cache: true, | ||
|
||
// Specify the version of babel-core used for transformation. (Optional) | ||
babel: require('babel-core') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
require("@babel/core")
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicolo-ribaudo Imagine calling babel-register
from a location where a different version of @babel/core
is available than the one that is depended on by babel-register
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. @babel/register
should also work with @babel/core
, same for babel-register and babel-core.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, in my case, it was using @babel/core
but a conflicting version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please change babel-core
to @babel/core
, then I think we're ready to merge
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
packages/babel-register/README.md
Outdated
cache: true, | ||
|
||
// Specify the version of babel-core used for transformation. (Optional) | ||
babel: require('babel-core') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please change babel-core
to @babel/core
, then I think we're ready to merge
Thanks for your PR @Janpot. I'll go ahead and merge this, tests are green and it doesn't have a big impact anyway. |
This seems okay as a short-term fix, but it seems like ideally we'd suggest users use multiple versions of This is also super dangerous because |
Yes, I agree, on the other hand, this allows to tight together babel-register and babel-core versions. I don't know enough babel-register, but It seems to have quite some logic, i'm wondering if we could configure it through babel-core (by using the instance in argument). |
In my use case I want to be able to register babel in a plugin to require a module from the user's project after which I will immediately deregister it to continue normal operation. I want to register the exact babel that is passed to the plugin. |
@Janpot That's the issue though, there is no "just register it and then undo it". Say for instance the user has their own script doing
and then later in your process your logic kicks off with
That Personally while I appreciate the goal of this, I think it would be entirely unsafe to use in any realworld code, so I'd personally say we should revert this. If the user expects to compile macros, it should either be up to them to add
somewhere, or it should be up to Generally we don't recommend publishing code and expecting it to be processed with Babel afterward, so personally it sounds like this usecase would be addressed by precompiling the macro before it was published anyway. |
Sorry about that it's my bad, let's revert this. |
Well that's the thing, this wouldn't be used for published code at all since Also the |
I thought the motivation for this PR was to use the babel-core version from a different module? In what cases do you need two babel versions in the same project?
I'm not sure I follow, but I think I just don't have quite enough information. What do you mean it's all among transpiled code? |
@loganfsmyth Ok, so the problem arises in the new
Turns out this wasn't really possible. within the plugin, after the macro file is identified, it is just plain import myMacro from './my.macro'; It would be used as a macro, but it wouldn't be evaluated as an ES6 module. It wouldn't be evaluated with the same transpiler as all the surrounding code. I think this goes against expectations. Also, since it wasn't transpiled, using a macro inside a macro is impossible and sharing code between the macro and the rest of the project as well, unless I wrote everything I want to share in commonjs style. (Or precompile it, or anything else that would defeat the use of convenience tools like CRA, that are supposed to work batteries-included, out-of-the-box) So solution to both of my problems: transpile imported modules on the fly when executing the macro. Surrounding the Now when trying out this solution, I could make it work in one of my local folders without CRA, but when testing whether it would work in a CRA app, it gave errors that it didn't recognize certain (newer) AST node types. It seemed like it was trying to use babel-core that got installed in my top-level I hope with this background, my request makes a bit more sense. To me, your comment about the revert makes a lot of sense, it seems like maybe we rather need a const babel = require('@babel/core');
const x = babel.transformRequire('./some-to-transpile-file.js'); or a const requireWithBabel = require('@babel/register/require');
const x = requireWithBabel('./some-to-transpile-file.js', {
babel: require('@babel/core')
}); Or it could be interesting to explore if we can create a context to be used with node const createContext = require('@babel/register/context');
const vm = require('vm');
vm.runInContext(`
import * from './some-to-transpile-file.js';
`, createContext(require('@babel/core'))); ? |
I'm still for reverting this, but I also do want to try to work to make sure we nail down the issue here. If you haven't ejected, I admit I'm not an expert, but is there way to load babel-register here in the first place? I don't think so, right? If you are doing it in here, you should be able to just install whatever version you want for If you've ejected your CRA setup with Babel 6, you should be able to install Maybe the issue here is that you're trying to use Babel 7 when CRA is still Babel 6?
I do think there are a decent number of possible improvements to be made on the API. |
I made the babel instance used by
babel-register
configurable to allow for the use case in #7263.