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

Allow inject in TransformOptions? #984

Closed
lukeed opened this issue Mar 16, 2021 · 3 comments
Closed

Allow inject in TransformOptions? #984

lukeed opened this issue Mar 16, 2021 · 3 comments

Comments

@lukeed
Copy link
Contributor

lukeed commented Mar 16, 2021

The use case is the same as it is with build and buildSync, but at a per-file level. This is useful for register/loader hooks.

For example, I'm currently doing something like node -r ./bin/register.js test.ts which uses esbuild to transform the contents of test.ts before executing it. This particular case needs some polyfills to be injected. Right now I'm (ab)using banner to inject the polyfill, but that's probably not ideal :)

This could also be used to auto-prepend something like import { h } from 'preact'; at the top of every *.[tj]sx file. This is listed here in the docs, but because it's inject-based, this feature is limited to builds only.

@evanw
Copy link
Owner

evanw commented Mar 18, 2021

This isn't allowed in the transform API because the inject feature requires a file system and the transform API is designed to not imply a file system.

Ideally this would be possible with the build API and a plugin, but it looks like it's not currently possible without writing to the file system. I believe an action item here is to expose the paths of injected files to onResolve plugins so that plugins can capture the paths and virtualize them.

A meta comment: This feature request is part of a growing list of feature requests from people that basically want the transform API to work like the build API. I'm considering removing the transform API in the future and just having people use the build API instead. This should be reasonably ergonomic if a shortcut like #690 is implemented.

@lukeed
Copy link
Contributor Author

lukeed commented Mar 18, 2021

Ahh right... Sorry, overlooked that important detail.

I'll continue using the banner option to inject a content string. This works for my use case, and it'd work with the JSX example too, in a couple cases.

I haven't put much thought into this of course, but I think keeping the transform API would be a nice shortcut, even if all it was a call to build w/ bundle:false forced and skipping the fs.readFile step for entrypoints

Happy for this to be closed btw. Thanks!

@lukeed
Copy link
Contributor Author

lukeed commented Mar 18, 2021

Closing :) Doesn't seem like there are any actionables here & you already have related thought(s) logged.

Thanks!

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

No branches or pull requests

2 participants