Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): avoid double sourcemap comments w…
Browse files Browse the repository at this point in the history
…ith 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.
  • Loading branch information
clydin authored and angular-robot[bot] committed Apr 26, 2023
1 parent f3a27fa commit e891786
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,20 @@ async function setupServer(
},
load(id) {
const [file] = id.split('?', 1);
const code = outputFiles.get(file)?.contents;
const map = outputFiles.get(file + '.map')?.contents;
const codeContents = outputFiles.get(file)?.contents;
if (codeContents === undefined) {
return;
}

return (
code && {
code: code && Buffer.from(code).toString('utf-8'),
map: map && Buffer.from(map).toString('utf-8'),
}
);
const code = Buffer.from(codeContents).toString('utf-8');
const mapContents = outputFiles.get(file + '.map')?.contents;

return {
// Remove source map URL comments from the code if a sourcemap is present.
// Vite will inline and add an additional sourcemap URL for the sourcemap.
code: mapContents ? code.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '') : code,
map: mapContents && Buffer.from(mapContents).toString('utf-8'),
};
},
configureServer(server) {
// Assets and resources get handled first
Expand Down

0 comments on commit e891786

Please sign in to comment.