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

[Feature] Lifecycle precompile hook to support package.json comments via external package #4342

Open
2 tasks done
bmulholland opened this issue Apr 11, 2022 · 0 comments
Open
2 tasks done
Labels
enhancement New feature or request

Comments

@bmulholland
Copy link

bmulholland commented Apr 11, 2022

  • I'd be willing to implement this feature (contributing guide)
  • (See question below) This feature is important to have in this repository; a contrib plugin wouldn't do

Describe the user story

I need to document our dependencies: why each is added, what it does, when it could be removed. I also need to document pinned versions: why is it pinned, and when can that change?

To do this, I'd like to add comments, which normal JSON does not support. Direct JSON5 support by yarn is not going to happen unless Node adds it, and Node is not going to add it.

I'd like to build a simple package that adds support for comments via JSON5. It would use a package.json5 file, and compile that first to package.json before running the yarn command. I already have a very basic MVP in my project.

What I've already found in my use of that MVP is that having to manually run a command to compile JSON5 -> JSON after every change is annoying, and it's easy to get confused and make mistakes. Ideally, there would be a way to automatically run this precompilation automatically before any yarn operation that reads package.json. Node maintainers have directly suggested that this is the best path forward.

There is no suitable lifecycle hook for such an operation. I've tried the yarn prepack hook, which seems the most common and fundamental, and that doesn't seem to run unless there are certain types of changes in the package.json (I think?). Of course, with this JSON5 plugin, there won't be any changes to package.json until it's compiled.

I know this approach wouldn't support yarn add, but since this is the only way I can get my dependencies documented, I'll have to take that loss. (If someone has a viable way to do this, though, I'd love to hear it!)

Describe the solution you'd like

As above: a lifecycle hook that runs before any operation that relies on package.json -- and ideally one that I can link into as a plugin.

Describe the drawbacks of your solution

Given there are already some lifecycle hooks, I'm guessing that the basic infrastructure/architecture for this is already in place. The main risk is that the hook may not be added to all the places where it's needed. To mitigate that, I'd be more than happy if the hooks were added for just install, update, and update-interactive to start. Could initial support be added as a limited, undocumented, beta feature? This would limit maintenance and I'd be happy to help flesh it out over time.

Describe alternatives you've considered

I've also considered writing a wrapper script that simply calls the compilation before proxying the command to yarn, but creating a wrapper of yarn for every single yarn command doesn't sound fun. And expecting people to use another executable over yarn would also be annoying and complicated.

Could someone help me understand the capabilities of a contrib plugin? Would adding a lifecycle hook be possible in one? (My gut feel says "no," at least without adding some hard-to-maintain monkey patches.)

@bmulholland bmulholland added the enhancement New feature or request label Apr 11, 2022
@bmulholland bmulholland changed the title [Feature] Lifecycle precompile hook for alternate package filetypes [Feature] Lifecycle precompile hook to support package.json comments via external package Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant