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

ref(inject): Keep sourceMappingURL comment at end #1511

Merged
merged 1 commit into from Mar 9, 2023

Conversation

loewenheim
Copy link
Contributor

This keeps the sourceMappingURL comment at the end of a source file when injecting debug ids. It does this by reading the file to memory and writing it back line by line, but saving the comment line to the very end. It works fine in my limited tests, but it doesn't feel elegant to me. Suggestions for how to improve this would be very welcome.

@kamilogorek
Copy link
Contributor

kamilogorek commented Mar 9, 2023

Maybe we can shortcircuit the iteration for the non-sourcemapurl path?

(pseudocode)

let sourcemap_ref = sourcemap::locate_sourcemap_reference_slice(content).unwrap_or(None);

let lines = if sourcemap_ref.is_some() {
  lines.into_iter().filter(|line| line.starts_with("//# sourceMappingURL=") || line.starts_with("//@ sourceMappingURL=")
} else {
  content.lines()
};

let mut file = File::options().write(true).open(js_path)?;

file.append(lines);
if sourcemap_ref.is_some() {
  file.append(format!("//# sourceMappingURL={sourcemap_ref}"));
}
file.append(snippet);
file.append(debug_id);

@loewenheim
Copy link
Contributor Author

We don't call the function fixup_js_file unless the file contains a sourceMappingURL comment.

@kamilogorek
Copy link
Contributor

Oh, my bad. It shooould be fine then. Probably not the most optimal way to write such transformations, but it's a CLI, we can prefer being obvious over being overly performant.

@loewenheim loewenheim merged commit e7ab126 into master Mar 9, 2023
@loewenheim loewenheim deleted the ref/inject-move-comment branch March 9, 2023 13:00
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.

Sourcemap URL mapping comment should be at the bottom
2 participants