Skip to content

fix(webpack): keep source map positions in app-css-loader - #11646

Merged
NathanWalker merged 1 commit into
mainfrom
fix/webpack-app-css-loader-sourcemap
Nov 21, 2026
Merged

NathanWalker merged 1 commit into
mainfrom
fix/webpack-app-css-loader-sourcemap

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

app-css-loader runs as a post loader on the app entry module. When an app.css/app.scss sits next to the entry, it prepends two lines (// Added by app-css-loader and the css import) but returns the incoming source map unchanged. Every position in the entry file is then looked up in a map made for the code without those two lines, so stack frames and breakpoints in that file resolve to the wrong line. The error isn't a constant two lines, because TypeScript emit changes line counts (removed declare statements and so on).

Seen in a scratch app reporting to Sentry: the throw on app.ts:28 was reported as :31, and its caller on :62 as :64. Sentry and a local SourceMapConsumer lookup agreed with each other, and the map's sourcesContent matched the file, so the upload was fine and the map itself was wrong. Only the entry file is affected.

What is the new behavior?

The loader builds the output with webpack's sources (ConcatSource of a RawSource prefix and a SourceMapSource of the module) and returns that map, so positions after the prefix keep their original lines and columns. Without an incoming map it just prepends the import. It also stops passing the module code through ts-dedent, which could re-indent it.

New spec __tests__/loaders/app-css-loader.spec.ts:

  • transpiles a small app.ts with its source map, as ts-loader does;
  • runs the loader and checks through the returned map that a throw and a call still resolve to their original lines (this failed before the fix: line 6 resolved to 9);
  • covers the no-css and no-map paths.

Also checked on a real production bundle (Terser-minified ESM, devtool: 'source-map') with the patched loader: the throw maps to app.ts:28 and the inlined call to :62:2, matching the source.

Two other loaders rewrite code and pass the old map through: native-class-downlevel-loader (replaces @NativeClass classes with an ES5 transpile before ts-loader) and native-class-strip-loader (changes line counts only for multi-line decorator arguments). They need a composed map rather than an offset, so they're left for a separate change.

app-css-loader prepends two lines to the app entry module (a comment and
the app css import) but passed the incoming source map through unchanged.
Every position in the entry file was then resolved against a map for the
unprefixed code, so stack frames and breakpoints in it landed a few lines
off. Offset the map with webpack's ConcatSource/SourceMapSource instead,
and stop running the module code through ts-dedent.
@nx-cloud

nx-cloud Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 55cf982

Command Status Duration Result
nx run-many -t build -p core,webpack5,vite ✅ Succeeded 23s 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 17:52:58 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@11646
npm i https://pkg.pr.new/@nativescript/vite@11646
npm i https://pkg.pr.new/@nativescript/webpack@11646

commit: 55cf982

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

Using sources.ConcatSource (RawSource prefix + SourceMapSource) cleanly offsets the incoming map by the prepended CSS import lines, and dropping ts-dedent avoids unintended indentation changes on the entry module. Passing the raw prefix through when map is absent and covering all three paths in app-css-loader.spec.ts looks great.

@NathanWalker
NathanWalker merged commit 587460b into main Nov 21, 2026
10 checks passed
@NathanWalker
NathanWalker deleted the fix/webpack-app-css-loader-sourcemap branch November 21, 2026 04:25
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