Repository navigation
fix(vite-plugin-angular): avoid stack overflow for large inline sourcemaps - #2796
Conversation
…emaps Replace the regular expression in `extractInlineSourceMap` with index-based string searches. Matching a multi-megabyte base64 payload with the regex threw "Maximum call stack size exceeded" while the build optimizer processed `@angular/compiler` with sourcemaps enabled. Extraction accepts the same input as before: only a terminal `//# sourceMappingURL=data:application/json[;charset=...];base64,...` comment followed by nothing but whitespace is removed, together with one preceding newline. Code that follows the comment after any JavaScript line terminator, or a marker inside a string literal, is left untouched. Closes analogjs#2669
✅ Deploy Preview for analog-blog ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for analog-app ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for analog-docs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
View your CI Pipeline Execution ↗ for commit 8b5f501
💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗ ☁️ Nx Cloud last updated this comment at |
PR Checklist
Closes #2669
extractInlineSourceMap(used by the build optimizer whenbuild.sourcemapis enabled) matched the whole inlinesourceMappingURLcomment with one regular expression. On@angular/compiler's transformed output, which has a ~4 MiB base64 map, thatString.matchcall threwRangeError: Maximum call stack size exceededand failed the build (vite-plugin-angular: extractInlineSourceMap causes stack overflow for large Angular compiler sourcemaps analogjs/analog#2669).This picks up fix(vite-plugin-angular): avoid stack overflow for large inline sourcemaps analogjs/analog#2704 by mootec. That PR was closed after its review feedback went unanswered. This PR addresses that feedback (details below).
Affected scope
Recommended merge strategy for maintainer [optional]
Commit preservation note [optional]
N/A
What is the new behavior?
extractInlineSourceMapnow finds the comment with string searches instead of one regex over the payload:lastIndexOf('//# sourceMappingURL=data:application/json')trimEnd()the rest of the file and split it at the first;base64,;base64,against/^(?:;charset=[^;,]+)?$/. This part is short./[^A-Za-z0-9+/=]/finds a character outside the base64 alphabet. This single-character test doesn't backtrack.It accepts and rejects the same inputs as the old regex. A map is extracted only when the comment is at the end of the file, with nothing but whitespace after the payload. The comment is removed together with one preceding
\n. In every other case, the code is returned unchanged withmap: null.How this handles the review feedback on #2704:
sourceCode, notsourceCode + '\n'.\nis removed, matching the old\n?. The existing tests are unchanged and pass.;base64,, only base64 characters and trailing whitespace are allowed. New test: the marker inside a string literal is left alone.it.eachtest covers LF, CRLF, CR, LS and PS followed byconsole.log(a);. With #2704's final revision, the CR, LS and PS cases fail.On #2704,
prettier:checkalso failed in CI. This branch passespnpm prettier:check.Test plan
nx format:check(alsopnpm prettier:checkfor the whole repo)pnpm build: not run for the whole workspace.pnpm nx build vite-plugin-angularpasses, and so does the rootpreparebuild (tools/scripts/build-release.mts).pnpm test: not run for the whole workspace.pnpm nx test vite-plugin-angularpasses: 66 files, 1093 tests, 8 of them new.New spec cases in
angular-build-optimizer-plugin.spec.ts:\r\n) after the comment is still acceptedmap: null)sourceMappingURLmarker inside a string literal is kept unchangedManual checks:
codeandmapoutputs were identical for every input.;charset=values all completed in about 8 to 275 ms, with no exceptions.oxlint --quietandeslintreport no errors on the changed files.About reproducing the crash: I couldn't reproduce the stack overflow locally. On Node 24.14.1 / macOS arm64, the old regex matched payloads of up to 80 MiB without throwing. I also tried two-byte strings,
--regexp-interpret-all, and workers with 0.5 MB stacks. Because the old code doesn't fail here, I didn't add a child-process--stack-sizetest, since it couldn't fail. The large-payload case therefore checks that large maps still extract correctly; it doesn't reproduce the crash. In #2669, the reporter (Windows 11, Node 24.22.0) confirmed that an index-based extraction fixed their Storybook build.Does this PR introduce a breaking change?
Other information
vite-plugin-angularsuite sometimes aborts a test worker. The error isFATAL ERROR: v8::ToLocalChecked Empty MaybeLocalinnode::cjs_lexer::Parse, and it happens inngp-compile-plugin.spec.tsorcomposable-plugins.spec.ts. This also happens on unmodifiedbetawith my Node 24.14.1 (.node-versionpins 24.22.0), so it isn't caused by this change. Clean runs pass.[optional] What gif best describes this PR or how it makes you feel?