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

js: Add Shims option #8165

Closed
bep opened this issue Jan 21, 2021 · 11 comments · Fixed by #8167
Closed

js: Add Shims option #8165

bep opened this issue Jan 21, 2021 · 11 comments · Fixed by #8167
Assignees
Milestone

Comments

@bep
Copy link
Member

bep commented Jan 21, 2021

This is meant to improve the situation as described in https://gohugo.io/hugo-pipes/js#shimming-a-js-library

The above works, but it

  1. Requires you to edit package.json
  2. It only works from the project (i.e. it's not practical to do this in a theme/module)

I have invested some time today looking for a better way, and to cut a very long story short:

I suggest we add add a shims config option that works similar to the browser element in package.json, but resolves files inside the composite assets. So you would do:

{{ $shims := dict "react" "js/shims/react.js"  "react-dom" "js/shims/react-dom.js" }}
{{ $js = $js | js.Build dict "shims" $shims }}

You obviously still needs to create the shim files, but that is simple enough. Thoughts?

I have tested the above, and it works.

/cc @chrischute @richtera

@bep bep self-assigned this Jan 21, 2021
@bep bep added the Proposal label Jan 21, 2021
@bep bep added this to the v0.81 milestone Jan 21, 2021
@chrischute
Copy link

This sounds like a worthwhile improvement. It felt heavy to edit package.json and force the shims to be a project-wide setting.

Is this enabled by a new feature in ESBuild? I remember this issue where the ESBuild creator recommended the package.json browser approach.

@bep
Copy link
Member Author

bep commented Jan 21, 2021

Is this enabled by a new feature in ESBuild?

It's the "import resolver" plugin feature. Not very new.

Note that my main problem isn't the package.json editing -- it's the project relative (or absolute) file paths.

@chrischute
Copy link

Got it. Well I support the change, happy to review or update documentation when it's time.

@richtera
Copy link
Contributor

I think it would be a good idea. I don't understand how the compiled shims' <script> tags would be configured and output (i.e. fingerprint, Integrity and sourcemaps)?

@bep
Copy link
Member Author

bep commented Jan 21, 2021

I don't understand how the compiled shims' <script> tags would be configured and output (i.e. fingerprint, Integrity and sourcemaps)?

In my head, this should just work.

@bep
Copy link
Member Author

bep commented Jan 21, 2021

But, @chrischute -- a question: Given that you have React/ReactDOM included from CDN, what are the scenarios where you need to do an import? I can understand third-party apps, but in your own files?

I'm trying to come up with a failing example, but now all my earlier test setups seems to work fine without any react imports, even though I swear they didn't before...

@chrischute
Copy link

Given that you have React/ReactDOM included from CDN, what are the scenarios where you need to do an import? I can understand third-party apps, but in your own files?

I might be misunderstanding the question, but the main reason I import React is to make the IDE happy. I.e., I want to locally have React in my package for code completion, but want to use the CDN in production. Does that answer your question?

@bep
Copy link
Member Author

bep commented Jan 21, 2021

Does that answer your question?

It does; I have, however, not looked at this problem with those eyes.

@chrischute
Copy link

OK great, maybe you can see an easier solution given this info.

bep added a commit to bep/hugo that referenced this issue Jan 21, 2021
bep added a commit to bep/hugo that referenced this issue Jan 21, 2021
@bep bep added Enhancement and removed Proposal labels Jan 21, 2021
@richtera
Copy link
Contributor

This is very similar to using jsconfig or tsconfig files. ESBuild still does read those to resolve import and require. Using a shims variable is probably more elegant although jsconfig and tsconfig are read and understood by visual studio and adding production version of jsconfig or tsconfig is possible. Adding shims is definitely more elegant.

bep added a commit to bep/hugo that referenced this issue Jan 21, 2021
This commit adds a new `shims` option to `js.Build` that allows swapping out a component with another.

Fixes gohugoio#8165
@bep bep closed this as completed in #8167 Jan 22, 2021
bep added a commit that referenced this issue Jan 22, 2021
This commit adds a new `shims` option to `js.Build` that allows swapping out a component with another.

Fixes #8165
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants