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

Add resolvePlugin and resolvePreset methods to babel-core API #4729

Merged

Conversation

rmacklin
Copy link
Contributor

Q A
Bug fix? no
Breaking change? no
New feature? yes
Deprecations? no
Spec compliancy? no
Tests added/pass? yes
Fixed tickets n/a
License MIT
Doc PR will create one once merged

This PR adds resolvePlugin and resolvePreset methods to the babel-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's dirname.)

[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.

@rmacklin
Copy link
Contributor Author

Supersedes #4612

@rmacklin rmacklin changed the title Add resolve plugin and resolve preset Add resolvePlugin and resolvePreset Oct 15, 2016
@codecov-io
Copy link

codecov-io commented Oct 15, 2016

Current coverage is 89.94% (diff: 100%)

No coverage report found for master at 2dc919d.

Powered by Codecov. Last update 2dc919d...6ec93d7

@rmacklin rmacklin force-pushed the add-resolvePlugin-and-resolvePreset branch from 0df3823 to 3aa3814 Compare October 15, 2016 02:04
@rmacklin
Copy link
Contributor Author

rmacklin commented Oct 15, 2016

Well, the tests I added are failing on old node versions (since resolve doesn't go up the directory tree to find transform-es2015-arrow-functions in the root node_modules). I could test this differently by just checking what gets passed to resolve, but to do that I'd need to add something like sinon spies, which we probably don't want. Thoughts?

@rmacklin
Copy link
Contributor Author

rmacklin commented Oct 15, 2016

Another option is to add another layer of abstraction, which encapsulates only the logic determining what strings we want to pass to resolve. Originally I didn't want to do this because of the extra level of indirection, but ultimately the extracted logic is precisely what we want to test, and doing so doesn't involve calling resolve in the tests, so it works on any node version.

I just pushed some new commits with this refactoring to extract and test these two methods: getPossiblePluginNames and getPossiblePresetNames and CI is now passing. And now that I've done it, I don't have issues with the extra layer of indirection because resolvePlugin and resolvePreset became "dead simple" wrappers.

@rmacklin rmacklin force-pushed the add-resolvePlugin-and-resolvePreset branch from ca520ba to 6016cac Compare October 15, 2016 22:06
@rmacklin rmacklin changed the title Add resolvePlugin and resolvePreset Add resolvePlugin and resolvePreset methods to babel-core API Oct 15, 2016
@rmacklin rmacklin force-pushed the add-resolvePlugin-and-resolvePreset branch from 6016cac to ba0c3d1 Compare October 15, 2016 23:30
@rmacklin
Copy link
Contributor Author

rmacklin commented Oct 15, 2016

Rebased to resolve merge conflicts with #4734. Since I was already rebasing, I squashed out 6592c2c and 6df614d to make the history cleaner and allow each commit to pass CI.

@rmacklin rmacklin force-pushed the add-resolvePlugin-and-resolvePreset branch 2 times, most recently from 0908a2e to 9f28d4d Compare October 15, 2016 23:44
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
@rmacklin rmacklin force-pushed the add-resolvePlugin-and-resolvePreset branch from 9f28d4d to f095ef1 Compare October 16, 2016 18:11
"DRY"s the duplicated algorithm in resolvePlugin and resolvePreset
@rmacklin rmacklin force-pushed the add-resolvePlugin-and-resolvePreset branch from f095ef1 to 6ec93d7 Compare October 16, 2016 18:19
@hzoo hzoo added the PR: New Feature 🚀 A type of pull request used for our changelog categories label Oct 21, 2016
@rmacklin rmacklin closed this Nov 14, 2016
@rmacklin rmacklin reopened this Nov 14, 2016
@rmacklin
Copy link
Contributor Author

rmacklin commented Nov 14, 2016

@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"), [
Copy link
Member

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?

Copy link
Contributor Author

@rmacklin rmacklin Dec 8, 2016

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.

@loganfsmyth
Copy link
Member

This looks good to me.

@loganfsmyth loganfsmyth merged commit ce0c620 into babel:master Dec 20, 2016
@rmacklin rmacklin deleted the add-resolvePlugin-and-resolvePreset branch December 20, 2016 18:03
@hzoo
Copy link
Member

hzoo commented Jan 20, 2017

Released in https://github.com/babel/babel/releases/tag/v6.22.0

@loganfsmyth
Copy link
Member

@rmacklin Beyond making the transition from this code to what you wanted most easily, was there a specific reason you made these helpers return null? I'm thinking it might be nicer to have them throw if the name can't be resolved, in Babel 7.x.

@rmacklin
Copy link
Contributor Author

rmacklin commented Mar 28, 2017

@loganfsmyth Looking at this code again, I think resolvePlugin and resolvePreset returned null because they replaced the previous uses of resolve, which was returning null if an error was caught.

It's probably fine to change them to throw in Babel 7.x - if someone wanted null instead they could write a small wrapper that catches the error.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 6, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: New Feature 🚀 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants