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 request] Replace global objects with polyfills #451

Closed
remorses opened this issue Oct 12, 2020 · 4 comments
Closed

[Feature request] Replace global objects with polyfills #451

remorses opened this issue Oct 12, 2020 · 4 comments

Comments

@remorses
Copy link
Contributor

My use case is to replace the process global object with a polyfill

You could do that adding an option to replace global objects with modules, this way you can for example replace the process global object with a polyfill stored in a local file that has the process polyfill as default export

Example

Building with a setting like --inject=proces:process-polyfill.js

The code below

console.log(`This platform is ${process.platform}`);

Would become

import process from './process-polyfill.js'
console.log(`This platform is ${process.platform}`);

This feature request is similar to what the @rollup/plugin-inject does in rollup

@evanw
Copy link
Owner

evanw commented Oct 13, 2020

I think something like this would be good to add. I got most of the way to implementing what is essentially this proposal, but then it occurred to me that there's a more general form of this that would have the same effect here: injecting a small bit of code into each file.

You could then specify --inject:"import process from './process-polyfill.js'" and it would prefix all files with that code. The bundler already has tree shaking that can remove unused imports. This would also address #53 and potentially other use cases as well (e.g. modifying code using a plugin without affecting source maps). So I'm tempted to do that instead of this proposal.

@remorses
Copy link
Contributor Author

remorses commented Oct 13, 2020

some problems that could arise injecting code in every module are

  • duplicate import for the same name: what if there was already an import process from 'process-polyfill'?
  • appending code with relative imports would not work because those import paths are relative to the importer file

to overcome these issues you would need to check that the module being injected to does not already define the injected identifiers and you would also need to resolve import paths relative to the importer module

@evanw
Copy link
Owner

evanw commented Oct 13, 2020

Yeah I was thinking of doing both of those things. Or more specifically, the injected file would still be a separate file (parsed once and resolved in a specific directory) but esbuild would automatically generate imports of the injected file into each file for all top-level variables not shadowed by that injecting file.

@evanw evanw closed this as completed in 02c6627 Oct 18, 2020
@evanw
Copy link
Owner

evanw commented Oct 18, 2020

The final solution is somewhat different than what I proposed above. You have to explicitly export the local variables you want to expose as globals. There is more information in the release notes.

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