Skip to content

Commit e891786

Browse files
clydinangular-robot[bot]
authored andcommitted
fix(@angular-devkit/build-angular): avoid double sourcemap comments with esbuild dev-server
When using the esbuild-based browser application builder with the dev-server builder, sourcemap URL comments were unintentionally doubled due to vite adding an additional inline sourcemap comment when processing the JavaScript files for the application. To avoid this, JavaScript files now have any sourcemap URL comments removed prior to being processed by vite. The sourcemap content is already passed directly to vite and allows the sourcemaps to be processed and provided to the browser as needed without the previously existing sourcemap URL comment. The removal is done without modifying the esbuild- based builder's options to avoid assuming the builder used with the dev-server has sourcemap options that allow hidden sourcemap generation.
1 parent f3a27fa commit e891786

File tree

1 file changed

+13
-8
lines changed
  • packages/angular_devkit/build_angular/src/builders/dev-server

1 file changed

+13
-8
lines changed

packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,20 @@ async function setupServer(
214214
},
215215
load(id) {
216216
const [file] = id.split('?', 1);
217-
const code = outputFiles.get(file)?.contents;
218-
const map = outputFiles.get(file + '.map')?.contents;
217+
const codeContents = outputFiles.get(file)?.contents;
218+
if (codeContents === undefined) {
219+
return;
220+
}
219221

220-
return (
221-
code && {
222-
code: code && Buffer.from(code).toString('utf-8'),
223-
map: map && Buffer.from(map).toString('utf-8'),
224-
}
225-
);
222+
const code = Buffer.from(codeContents).toString('utf-8');
223+
const mapContents = outputFiles.get(file + '.map')?.contents;
224+
225+
return {
226+
// Remove source map URL comments from the code if a sourcemap is present.
227+
// Vite will inline and add an additional sourcemap URL for the sourcemap.
228+
code: mapContents ? code.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '') : code,
229+
map: mapContents && Buffer.from(mapContents).toString('utf-8'),
230+
};
226231
},
227232
configureServer(server) {
228233
// Assets and resources get handled first

0 commit comments

Comments
 (0)