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
Add resolvePlugin and resolvePreset methods to babel-core API #4729
Add resolvePlugin and resolvePreset methods to babel-core API #4729
Conversation
Supersedes #4612 |
Current coverage is 89.94% (diff: 100%)
|
0df3823
to
3aa3814
Compare
Well, the tests I added are failing on old node versions (since |
Another option is to add another layer of abstraction, which encapsulates only the logic determining what strings we want to pass to I just pushed some new commits with this refactoring to extract and test these two methods: |
ca520ba
to
6016cac
Compare
6016cac
to
ba0c3d1
Compare
0908a2e
to
9f28d4d
Compare
This encapsulates the logic for turning an acceptable plugin name into the absolute path for that plugin. It can be used to preprocess a plugins list to map each plugin to its absolute path, which is necessary if `babel.transform` is going to be executed on a file outside the directory subtree where the plugins are installed. This adds a getPossiblePluginNames helper encapsulating the logic for what plugin names we should try to resolve, and the resolvePlugin method just calls this helper and actually resolves them.
This encapsulates the logic for turning an acceptable preset name into the absolute path for that preset. It can be used to preprocess a presets list to map each preset to its absolute path, which is necessary if `babel.transform` is going to be executed on a file outside the directory subtree where the presets are installed. This adds a getPossiblePresetNames helper encapsulating the logic for what preset names we should try to resolve, and the resolvePreset method just calls this helper and actually resolves them.
Previously these were testing the logic that is now encapsulated in getPossiblePresetNames and tested in a unit test
9f28d4d
to
f095ef1
Compare
"DRY"s the duplicated algorithm in resolvePlugin and resolvePreset
f095ef1
to
6ec93d7
Compare
@loganfsmyth @hzoo @kaicataldo @DrewML (or any other maintainers that can look at this) Can we merge this? (And sorry if you got duplicate notifications...had some issues posting from my phone) |
}); | ||
|
||
it("inserts babel-preset after @org/", function() { | ||
assert.deepEqual(getPossiblePresetNames("@babel/es2015"), [ |
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.
I'm kind of thinking if you did @babel/es2015
you probably would ever mean "babel-preset-@babel/es2015",
so we should remove this option?
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.
I agree, but in this PR I just wanted to keep things as straightforward as possible, so I preserved the existing behavior. I can remove that in a separate PR if there's consensus on it after this one is merged.
This looks good to me. |
@rmacklin Beyond making the transition from this code to what you wanted most easily, was there a specific reason you made these helpers return |
@loganfsmyth Looking at this code again, I think It's probably fine to change them to throw in Babel 7.x - if someone wanted |
This PR adds
resolvePlugin
andresolvePreset
methods to thebabel-core
API.Exposing these publicly is useful because it allows plugins/preset names to be mapped to their absolute paths, which is necessary if
babel.transform
will process a file that is not a descendant of the directory in which the babel plugins/presets were installed[1]. (Otherwise, babel will fail to resolve the plugins/presets because it'll try resolve from the processed file'sdirname
.)[1] One use case for this (the one that prompted me to make this change) is to work with ruby gems that include javascript assets (which is common among Rails plugins). Unlike node modules, ruby gems are installed globally, which means those javascript files won't be inside the application subtree (where your babel plugins and presets are installed). I'm developing a sprockets plugin to allow transpiling via babel 6 (sprockets-es6 is stuck on babel 5) and with these methods I can transform the user-supplied plugins/presets to their absolute paths so that processing assets inside globally-installed ruby gems will work.