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

[Internal] Injection plugin #100

Merged
merged 2 commits into from
Oct 2, 2024
Merged

Conversation

yoannmoinet
Copy link
Member

@yoannmoinet yoannmoinet commented Sep 30, 2024

What and why?

Having a way to reliably prepend some code to the output bundle would be great.

It needs to:

  • support all the currently supported bundlers (webpack4-5, esbuild, rollup and vite).
  • be usable from anywhere within the ecosystem.
  • support local files, distant files and raw code.
  • not conflict with the existing plugins (build report in particular).

How?

Create a pretty simple injection plugin.

It's composed of 4 different elements:

  • A context.inject() function you'd call to add a new item to inject.
  • A preparation plugin, that resolves all the injections.
  • A file injection plugin, that injects, within the bundle, a stub file where all the injections live (rollup is different, more on this later)
  • A resolution plugin, that resolve the stub file.

Note

Rollup and Vite are different because they offer the banner hook that works as expected OOTB.
We couldn't use the Webpack's BannerPlugin because it doesn't gives you the information about the chunk being entry or not, which is critical if you want to inject something only once.

I have a follow-up PR with the documentation.

@yoannmoinet yoannmoinet marked this pull request as ready for review September 30, 2024 13:21
Copy link
Member

@elbywan elbywan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Rollup and Vite are different because they offer the banner hook that works as expected OOTB.

AFAIK esbuild also provides this: https://esbuild.github.io/api/#banner
Is there a reason for not using it?

@yoannmoinet
Copy link
Member Author

AFAIK esbuild also provides this: https://esbuild.github.io/api/#banner
Is there a reason for not using it?

Forgot to mention it for ESBuild, but it's the same reason as for webpack, it doesn't tell you if the current chunk is an entry or not.
And they offer the inject config, which is pretty close to what we need, minus the resolution.

By far, Webpack is the most complicated here, esbuild and rollup/vite are pretty easy to work with in this instance.

Base automatically changed from yoann/improve-tests-experience to master October 2, 2024 10:05
@yoannmoinet yoannmoinet merged commit 8859075 into master Oct 2, 2024
5 checks passed
@yoannmoinet yoannmoinet deleted the yoann/internal-injection-plugin branch October 2, 2024 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants