Repository navigation
fix(webpack): keep source map positions in native-class loaders - #11647
Conversation
The strip and downlevel loaders rewrite @nativeclass classes before ts-loader runs but returned no source map. The module's map then described the rewritten text: it embedded the ES5 output as the file's source, and every line after a native class resolved to the wrong line (11 lines off in a real 1000-line file). The strip loader also changed the line count when the decorator sat at the start of the file or after blank lines. The strip loader now keeps every newline it replaces and returns an exact map through webpack's ReplaceSource. The downlevel loader maps untouched code to itself and each downleveled class through the ES5 transpile's own map, so ts-loader composes a map back to the original file, including lines inside the class. Emitted code is unchanged.
|
View your CI Pipeline Execution ↗ for commit 309a1af
💡 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 |
commit: |
NathanWalker
left a comment
There was a problem hiding this comment.
Nice work preserving newline counts in native-class-strip-loader via ReplaceSource and shifting/anchoring line-start mappings in native-class-downlevel-loader so ts-loader composes back to the original .ts source instead of embedding the ES5 snippet.
One tiny non-blocking note: in OutputBuilder.appendOriginal / appendGenerated, for (const char of text) increments column by 1 per code point rather than char.length UTF-16 code units (which lineStartsOf and source maps use), though column resets on every \n so it would only ever affect columns after a surrogate pair on the same line.
PR Checklist
__tests__/configurationsuites fail onmaintoo, with the same 14 tests: stale snapshots andTS2550onArray.prototype.at. Everything else passes, including the new spec.)What is the current behavior?
native-class-strip-loaderandnative-class-downlevel-loaderrun beforets-loaderand rewrite@NativeClassclasses into ES5, but return no source map.ts-loaderthen builds the module's map against the rewritten text, so for every file with a native class:var Foo = /** @class */ (function (_super) {…in a.tsfile);@NativeClassfiles (one app's pluspackages/core's), 753 of 1,001 declarations checked resolved to the wrong line.Inside a debugger or Sentry this looks plausible, because the embedded source agrees with the wrong lines. It only shows when you compare with the real file.
The strip loader also changed the line count on its own: it added a newline when the decorator was at the start of a file, and its leading
\s*swallowed blank lines above the decorator.What is the new behavior?
native-class-strip-loaderkeeps every newline of the text it replaces and returns an exact map through webpack'sReplaceSource(overOriginalSource, or overSourceMapSourcewhen a map comes in).native-class-downlevel-loaderreturns a map that sends untouched code back to itself at token level, and each downleveled class through the ES5 transpile's own map, shifted to the class's position in the file. Every generated line gets a mapping from its first column, so composing never falls back to the rewritten text. An incoming map is composed in. Overlapping (nested) marked classes keep the previous behavior.ts-loaderalready composes an input map (mapToInputSourceMap), so the module's final map embeds the original TypeScript and points at its real lines, including lines inside the native class.The emitted code is unchanged: running both loaders over the 77 files above gives byte-identical output before and after, and 0 of the 1,001 declarations resolve to the wrong line afterwards.
Tests:
__tests__/loaders/native-class-sourcemap.spec.ts: strip → downlevel → ats-loader-style compile and map composition, asserting lines inside and after the class, the embedded source, and the strip loader's line count. All three failed before the change;async(), since the downlevel loader now finishes asynchronously.Also checked in a real webpack production build (Terser,
devtool: 'source-map'): the map embeds the original.ts, and positions inside the class and after it resolve to their lines in the file.Related: #11646 fixes the same kind of stale map in
app-css-loader.