gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
76.07k stars 7.54k forks source link

`hugo mod npm pack` ignores `script` sections #8319

Open cmahnke opened 3 years ago

cmahnke commented 3 years ago

I'm not sure if this issue type is right, but anyways:

I'm trying to create a base theme for some of my sites using the hugo mod npm pack command (which would be better documented by providing a simple example btw). My node module setup is a bit different from the normal use cases since I need to run patch-package after one of my dependencies is installed to fix some issues that haven't been released yet. To do this I have a script section in the package.hugo.json file of the theme.

While hugo mod npm pack merges the dependencies and devDependencies the script section is discarded without any warning at all.

Is this on purpose? If yes, my proposal would be a flag for hugo mod npm pack to include it. If not, behavior can be considered a bug - anyways a warning or some --debug message would be nice.

bep commented 3 years ago

Is this on purpose?

No, I wrote the first version of npm pack knowing that 1) It would certainly not cover all bases and 2) I would never have started writing it if that was the requirement.

For my use cases it works good.

If yes, my proposal would be a flag for hugo mod npm pack to include it.

I assume most use the script section to have some kind of "build setup" -- which I don't want to merge into the top project. I need to think a little about this (a tip: js.Build now has ways to patch libraries)

cmahnke commented 3 years ago

For my use cases it works good.

I'm not saying that the initial use case isn't covered very well. ;)

If yes, my proposal would be a flag for hugo mod npm pack to include it.

I assume most use the script section to have some kind of "build setup" -- which I don't want to merge into the top project. I need to think a little about this (a tip: js.Build now has ways to patch libraries)

Yes, that's what I would assume as well. That's the reason why I'll not proposing it as the default behaviour. On the other hand others might also want to (re-) use setup steps for inherited dependencies. I can think of other approaches to cover the use case, one would be a new section in package.hugo.json called hugo-scripts, but I'm not sure if this would be considered legal. A second one would be some flag --merge-sections for npm-pack which would have the defaults dependencies,devDependencies but can be just set to dependencies,devDependencies,scripts to cover my case. The benefit would be that the following things would be possible as well:

hugo mod npm pack --merge-sections dependencies,devDependencies,licence,workspaces

This way it would be also possible to get some metadata in the site scope.

Regarding your tip: I guess you're referring to inject, from my point of view it's not a problem at all to get the patches somehow into the build (I'm currently using a site specific package.json to achieve this) - but this requires some duplication - I'm looking for a solution that is a) completely transparent and b) decouples the site from the theme to be able to update them independent from each other.

bep commented 3 years ago

hugo mod npm pack --merge-sections dependencies,devDependencies,licence,workspaces

Hmm. The problem with the above is that it's not tied to the project itself.

If I want to build a random Hugo project I would ideally:

  1. git clone
  2. cd thesite
  3. hugo mod npm pack
  4. hugo server

So, if we could somehow find a way inside package-hugo.json to signal upwards that "you need to consider this section" (comments?), that would maybe work.

One question re scripts: Do you somehow hook these script into the npm install command? Or is it something you need to do as a separate step?

cmahnke commented 3 years ago

Hmm. The problem with the above is that it's not tied to the project itself.

That's right, so this won't be required if there is a section in config.toml (or a config in another format) which would tie it to the project. The parameter (or a similar one) for npm pack would still be there for debugging purposes.

[mod.npm.pack]
  merge = ["dependencies", "devDependencies", "scripts"]

But actually I don't really want it to be tied to the project, but to the theme (see example below) so such a section could be part of the theme.toml file. Which on the other hand won't be suitable either, since it currently only specifies metadata, not behaviour. This leads back to package.hugo.json.

If I want to build a random Hugo project I would ideally:

  1. git clone
  2. cd thesite
  3. hugo mod npm pack
  4. hugo server

So, if we could somehow find a way inside package-hugo.json to signal upwards that "you need to consider this section" (comments?), that would maybe work.

Ideally, I won't want to run hugo mod npm pack for two reasons: a) It is actually project specific as well, not needed by all projects, b) it doesn't run it's depended steps (like npm or yarn) by itself - for good reasons. If hugo mod npm pack should be always run, like your example implies, it would be nice, if running it would point one to the next step, like "No Node dependencies detected - you can run hugo server next" or "Packed node dependencies - run npm install next" From my point of view this is just a question of personal taste, since one could put package.hugo.json in the repository and it would be provided by the checkout - only npm install would be required.

My use case is better expressed by the following steps:

  1. hugo new site thesite
  2. cd thesite
  3. git submodule add https://github.com/some-hugo-theme-with-npm-dependencies.git themes/some-hugo-theme-with-npm-dependencies
  4. hugo mod npm pack
  5. npm install (or yarn install)
  6. hugo server

I don't want to mix up things here but the first question after reading your list was how do I even know I'm supposed to know that I have to run hugo mod npm pack? Is there a warning if package.hugo.json exists in a theme but not in the project directory?

Having some script specific subsection in comments in package.hugo.json of the theme would certainly solve my issue.

One question re scripts: Do you somehow hook these script into the npm install command? Or is it something you need to do as a separate step?

In my setup there is on postinstall script which triggers the ones actually needed since it's tied to the install lifecycle event.


Just in case someone else has a similar use case, here a solution using jq. This does just a "stupid" merge of everything.

jq -s '.[0] * .[1]'  package.hugo.json themes/some-hugo-theme-with-npm-dependencies/package.hugo.json

If one has more then one theme:

find . -name "package.hugo.json" -o -name "package.json" -depth 0 | xargs jq -s add

Update 29.4.2020: The examples above are written for MacOS, as it uses the BSD variants of find and xargs this won't work on Linux (as well as GitHub Runners for your GitHub Pages). xargs needs -r to not run the command if the input is empty and find need to have the -depth argument as second - after the path.

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help. If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open. If this is a feature request, and you feel that it is still relevant and valuable, please tell us why. This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.