fix(@angular/build): prevent stripping nested sourceMappingURL comments#33545
fix(@angular/build): prevent stripping nested sourceMappingURL comments#33545clydin wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new utility function, removeSourceMappingURL, to safely strip top-level //# sourceMappingURL= comments from JavaScript code without affecting occurrences inside string literals, template literals, or block comments. This utility replaces several inline regex replacements across the codebase. The review feedback highlights a critical edge case where the proposed regular expression fails to handle nested template literals when a sourcemap comment is placed directly inside them without inner quotes. To address this, the reviewer suggests implementing a lightweight state-machine parser instead of a regular expression and provides corresponding unit tests to verify this behavior.
When compiling files, the dev server and transformers strip original `//# sourceMappingURL=` comments. However, using a naive RegExp like `/^\/\/# sourceMappingURL=[^\r\n]*/gm` matches the comment even when it is embedded inside multiline template literals or string literals (such as inline web worker code), which causes template literals to become unterminated and fail compilation. This introduces a robust, lexer-style utility `removeSourceMappingURL` that ignores occurrences of the comment when they appear inside single/double quotes, template literals, or block comments, preventing syntax errors in compiled chunks.
5194b41 to
a6c7b73
Compare
When compiling files, the dev server and transformers strip original
//# sourceMappingURL=comments. However, using a naive RegExp like/^\/\/# sourceMappingURL=[^\r\n]*/gmmatches the comment even when it is embedded inside multiline template literals or string literals (such as inline web worker code), which causes template literals to become unterminated and fail compilation.This introduces a robust, lexer-style utility
removeSourceMappingURLthat ignores occurrences of the comment when they appear inside single/double quotes, template literals, or block comments, preventing syntax errors in compiled chunks.