Skip to content

fix(webpack): keep source map positions in native-class loaders - #11647

Merged
NathanWalker merged 1 commit into
mainfrom
fix/webpack-native-class-sourcemaps
Nov 21, 2026
Merged

NathanWalker merged 1 commit into
mainfrom
fix/webpack-native-class-sourcemaps

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

native-class-strip-loader and native-class-downlevel-loader run before ts-loader and rewrite @NativeClass classes into ES5, but return no source map. ts-loader then builds the module's map against the rewritten text, so for every file with a native class:

  • the map embeds the ES5 output as the file's source (var Foo = /** @class */ (function (_super) {… in a .ts file);
  • every line after a native class resolves to the wrong line. Measured on a real app: 11 lines early in a 1,000-line file, 5 early in another, 1 late in a third. Across 77 @NativeClass files (one app's plus packages/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-loader keeps every newline of the text it replaces and returns an exact map through webpack's ReplaceSource (over OriginalSource, or over SourceMapSource when a map comes in).
  • native-class-downlevel-loader returns 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-loader already 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:

  • new __tests__/loaders/native-class-sourcemap.spec.ts: strip → downlevel → a ts-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;
  • the existing native-class spec's loader context gains 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.

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.
@nx-cloud

nx-cloud Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 309a1af

Command Status Duration Result
nx test apps-automated -c=android ✅ Succeeded 3m 42s View ↗
nx run-many --target=test --configuration=ci --... ✅ Succeeded <1s View ↗

💡 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 2026-11-18 19:01:45 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 18, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11647
npm i https://pkg.pr.new/@nativescript/vite@11647
npm i https://pkg.pr.new/@nativescript/webpack@11647

commit: 309a1af

@NathanWalker NathanWalker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@NathanWalker
NathanWalker merged commit 2f70ec8 into main Nov 21, 2026
10 checks passed
@NathanWalker
NathanWalker deleted the fix/webpack-native-class-sourcemaps branch November 21, 2026 04:26
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.

3 participants