refactor(@angular/build): remove esbuild sourcemap workarounds

These are no longer requires since 0.25.1
This commit is contained in:
Alan Agius 2025-03-10 07:57:35 +00:00
parent 97ed10b964
commit 596b9ae60c
3 changed files with 4 additions and 39 deletions

View File

@ -9,7 +9,6 @@
import { NodePath, PluginObj, types } from '@babel/core';
import { Visitor, programVisitor } from 'istanbul-lib-instrument';
import assert from 'node:assert';
import { fileURLToPath } from 'node:url';
/**
* A babel plugin factory function for adding istanbul instrumentation.
@ -23,19 +22,9 @@ export default function (): PluginObj {
visitor: {
Program: {
enter(path, state) {
const inputSourceMap = // eslint-disable-next-line @typescript-eslint/no-explicit-any
(state.file.inputMap as undefined | { toObject(): Record<string, any> })?.toObject();
// istanbul does not support URL as sources.
if (inputSourceMap?.sources) {
inputSourceMap.sources = inputSourceMap.sources.map((s: string) =>
s.startsWith('file://') ? fileURLToPath(s) : s,
);
}
const visitor = programVisitor(types, state.filename, {
// Babel returns a Converter object from the `convert-source-map` package
inputSourceMap,
inputSourceMap: (state.file.inputMap as undefined | { toObject(): object })?.toObject(),
});
visitors.set(path, visitor);

View File

@ -19,7 +19,6 @@ import type {
import assert from 'node:assert';
import { createHash } from 'node:crypto';
import * as path from 'node:path';
import { pathToFileURL } from 'node:url';
import { maxWorkers, useTypeChecking } from '../../../utils/environment-options';
import { AngularHostOptions } from '../../angular/angular-host';
import { AngularCompilation, DiagnosticModes, NoopCompilation } from '../../angular/compilation';
@ -707,10 +706,6 @@ function createCompilerOptionsTransformer(
return {
...compilerOptions,
noEmitOnError: false,
// Using the path as a URL is necessary here; otherwise, esbuild will not generate source maps correctly.
// https://github.com/evanw/esbuild/issues/4070
// https://github.com/evanw/esbuild/issues/4075
outDir: absWorkingDir ? pathToFileURL(absWorkingDir + '/').href : undefined,
inlineSources: !!pluginOptions.sourcemap,
inlineSourceMap: !!pluginOptions.sourcemap,
sourceMap: undefined,

View File

@ -8,8 +8,6 @@
import type { Location, OnLoadResult, PluginBuild } from 'esbuild';
import { readFile } from 'node:fs/promises';
import { isAbsolute } from 'node:path';
import { pathToFileURL } from 'node:url';
import { StylesheetLanguage, StylesheetPluginOptions } from './stylesheet-plugin-factory';
/**
@ -115,7 +113,7 @@ async function compileString(
};
try {
const { imports, map, css } = await less.render(data, {
const { imports, css } = await less.render(data, {
filename,
paths: options.includePaths,
plugins: [resolverPlugin],
@ -123,16 +121,14 @@ async function compileString(
javascriptEnabled: unsafeInlineJavaScript,
sourceMap: options.sourcemap
? {
sourceMapFileInline: false,
sourceMapFileInline: true,
outputSourceFiles: true,
}
: undefined,
} as Less.Options);
return {
// There can be cases where `less` will return an undefined `map` even
// though the types do not specify this as a possibility.
contents: map ? `${css}\n${sourceMapToUrlComment(map)}` : css,
contents: css,
loader: 'css',
watchFiles: [filename, ...imports],
};
@ -194,18 +190,3 @@ function convertExceptionLocation(exception: LessException): Partial<Location> {
lineText: exception.extract && exception.extract[Math.trunc(exception.extract.length / 2)],
};
}
function sourceMapToUrlComment(sourceMap: string): string {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const map = JSON.parse(sourceMap) as Record<string, any>;
// Using file URLs instead of paths ensures that esbuild correctly resolves the source map.
// https://github.com/evanw/esbuild/issues/4070
// https://github.com/evanw/esbuild/issues/4075
map.sources = map.sources.map((source: string) =>
source && isAbsolute(source) ? pathToFileURL(source).href : source,
);
const urlSourceMap = Buffer.from(JSON.stringify(map), 'utf-8').toString('base64');
return `/*# sourceMappingURL=data:application/json;charset=utf-8;base64,${urlSourceMap} */`;
}