-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
ref(inject): Inject code snippet at the start #1567
Conversation
Edit: sourcemaps are now getting adjusted as required. |
This pull request has gone three weeks without activity. In another week, I will close it. But! If you comment or otherwise update it, I will reset the clock, and if you label it "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
Picking this up again because it turned out to be necessary in an internal test. |
Looks good to me overall. |
One thing I definitely still need to handle is embedded sourcemaps. |
TODO: Handle when no sourcemap file is available locally. In this case, we should append at the end of the file instead, as sourcemap cannot have its mappings fixed up. |
This now also handles the edge case of a sourcemap not being available, by essentially falling back to the status quo behavior. I also refactored |
This injects the code snippet as early in the file as possible, which is to say, after an initial block of
"use …";
statements, if there is one. Since those statements must itself be at the start of the file (except for empty lines and comments), we skip empty lines and comments and then any number of"use …";
statements before injecting. For example, the filewould be modified to
Modifying the minified source file in this way requires us to fiddle with the sourcemap as well since every line number below the insertion point has now increased by one. Thus, we simply add
;
at the start of the sourcemap's mapping string to denote the code snippet's empty line mapping.Question: Is it sound to just always add the empty mapping at the beginning? If any of the lines before the injection point had mappings, they would get scrambled by this change, but since they're only comments and
"use …";
, can they even have nontrivial mappings?