-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
feat: support other js package managers #349
Conversation
@ahangarha @justin808 I would appreciate some initial feedback as I think it'll require a major change and probably a bit of overhauling of the test suite so want to make sure you're ok with the general direction before I continue to sink more time into it. I would also like to land the addition of |
I like the concept! My main concern is the balance of the:
|
I don't think so so long as Shakapacker (& co) don't try to do too much - imo the focus for gems like Shakapacker and That also goes for In practice this means the main use-case of A good example of this is with Shakapacker technically already stretches this because it provides bin scripts for I expect that approach should make it easier to be compatible across different major versions and managers, and I think that's already been validated somewhat by how straightforward the logic and test suite for
I think that lies in the eyes of the downstream developer - people will tell you that there are advantages to each of the different managers, and they're not wrong though I think it's not entirely wrong either to argue just one of those managers is enough. I wouldn't say this is necessarily something Shakapacker & co need, but I think it would make them better since it would be giving people downstream more flexibility. For what it's worth, we prefer |
fwiw I've got CI passing for my PR to I'm going to attempt to make this opt-in to start with. |
@G-Rath what's pending on this draft PR? Is this ready for final review? |
@justin808 yup I think it should be good to get at least an initial review - CI is green for rails-template and react-rails so I'm pretty confident this works, I just need to write some tests; I ended up feature flagging this, which is why CI is currently passing.
|
Ok I'm going to officially mark this ready for review I have also opened a draft PR adding support to Once this has gone through an initial round of review, I'll look to officially publish the I think the main leftover that will need discussion is what to do about the
|
Why have a separate package_json gem? If react-rails and react-on-rails both use shakapacker, does it need to be something else? |
Because shakapacker might not be the only use-case, and it doesn't necessarily make sense to have the whole of shakapacker as a dependency for this - I can imagine other gems like jsbundling and even non-rails gems wanting to take advantage of this. The gem also needs to be installed natively (I.e with bundler inline) for scripts like the Ackama rails-template and shakapackers own template.rb, and I'm not sure how well self-installing will work. I'm not against it moving into shakapacker or being a shakacode gem, but I think it's better to start as its own gem for now - one of the things I'll be doing now that I've finished the initial implementation here is looking elsewhere in the ecosystem to see what might benefit from this and that'll help inform if it should be its own gem or not. |
@G-Rath Any updates? |
@justin808 what updates are you expecting? I'm currently waiting for you to review this PR |
@justin808 |
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.
very quick review
I might be a bit confused...
let me know your thoughts.
|
||
There is experimental support for using package managers besides Yarn classic for managing JavaScript dependencies using the [`package_json`](https://github.com/G-Rath/package_json) gem. | ||
|
||
This can be enabled by setting the environment variable `SHAKAPACKER_USE_PACKAGE_JSON_GEM` to `true`; Shakapacker will then use the `package_json` gem which in turn will look for the [`packageManager`](https://nodejs.org/api/packages.html#packagemanager) property in the `package.json` or otherwise the `PACKAGE_JSON_FALLBACK_MANAGER` environment variable to determine which manager to use, defaulting to `npm` if neither are found. |
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.
doesn't the gem need to be added if configuring to tru?
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.
Usually, we have a config Shakapacker.yml that can be overridden by the ENV value.
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.
doesn't the gem need to be added if configuring to tru?
The install should be handled by shakapacker
- ackama/rails-template#466 is passing CI using the same self-install method as shakapacker
.
Usually, we have a config Shakapacker.yml that can be overridden by the ENV value.
We can't rely on a config alone because this is used when setting up shakapacker
- so the config won't exist and users won't have a chance to change it.
Also this is just to allow this to be released as part of stable minor versions so people can test it before it gets made the default in a new major; the env variable can be axed entirely if you're happy to stick with just beta versions and a new major release
@@ -18,6 +18,7 @@ Gem::Specification.new do |s| | |||
s.required_ruby_version = ">= 2.6.0" | |||
|
|||
s.add_dependency "activesupport", ">= 5.2" | |||
s.add_dependency "package_json" |
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.
do we want this to be an optional dependency, then throw an error if config is true but gem not installed?
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 don't think so since the long-term goal is that this won't be optional - the only reason it is currently is to allow it to be released in a minor version so people can try it out while still being backwards compatible; that compatibility isn't impacted by the gem being listed as a dependency, and it reduces the complexity of the implementation
@@ -41,12 +42,28 @@ | |||
say %( Add <%= javascript_pack_tag "application" %> within the <head> tag in your custom layout.) | |||
end | |||
|
|||
def package_json | |||
if @package_json.nil? |
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.
Why not traditional memorization?
b/c this value might be false?
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.
Because we have to ensure the package_json
gem has been installed and required before we attempt to use it - this is still using memorization, it just has another step as part of the initial assignment.
|
||
def self.require_package_json_gem | ||
unless use_package_json_gem | ||
raise "PackageJson should not be used unless SHAKAPACKER_USE_PACKAGE_JSON_GEM is true" |
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 confused since this is a config value.
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 not sure what you mean by this - SHAKAPACKER_USE_PACKAGE_JSON_GEM
is not a configuration value; are you saying that you think the error message should be more explicit about it wanting an environment variable?
OK -- looks OK.. Let's push out a version of the other ruby gem first, and then I'll merge this. Just be ready in case anybody reports any issues! |
Only general comment is that code with all the if statements is pretty awkward. I guess once we consider this stable, we'll remove the ENV and if statements. |
Just need to fix the CHANGELOG? and see #369. |
…t now has an env variable)
It seems that requires which happen after this are mucked up due to the load paths having been changed - this really only seems to impact RSpec which requires the diff library lazily
…e_json` is enabled
…e everything is valid
@justin808 have added a changelog - I think we're good to get this landed and do a new release |
So nice, please lets get this merged! ❤️ |
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.
LGTM
Summary
This adds support for package managers other than
yarn
by using a new prototype library I've created calledpackage_json
which provides an abstraction over top ofpackage.json
and the respective managers.Related to #195
Pull Request checklist
Remove this line after checking all the items here. If the item does not apply to the PR, both check it out and wrap it by
~
.Other Information
Foremost I'd like to get feedback from the maintainers about if this is an approach they'd be ok with; beyond that I'm happy to have feedback both on the implementation here & related repos, and on
package_json
itself - at this point the underlying logic is pretty stable, but I'm very much open to changing the API if the folks have opinions.I've got this working locally and in our
rails-template
with all three package managers - I'm still working on getting the test suites for here andreact-rails
passing, and will also be adding more actual tests torails-template
to confirm everything is actually working with the different managers.Note that currently Yarn Berry is not supported by
package_json
mainly because it can't be installed vianpm
like the others can - I've already looked into the mappings for its commands, and will tackle that once I've got everything across the board.This will also require a similar change to
react-rails
which I've got locally but will do that PR later.