Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@angular/build): allow control of source map sources content for application builds #29932

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/angular/build/src/builders/application/schema.json
Original file line number Diff line number Diff line change
@@ -370,6 +370,11 @@
"type": "boolean",
"description": "Resolve vendor packages source maps.",
"default": false
},
"sourcesContent": {
"type": "boolean",
"description": "Output original source content for files within the source map.",
"default": true
}
},
"additionalProperties": false
Original file line number Diff line number Diff line change
@@ -177,6 +177,7 @@ export function createComponentStyleBundler(
// the same as being disabled. Disabling has the advantage of avoiding the overhead
// of sourcemap processing.
sourcemapOptions.styles && !sourcemapOptions.hidden ? 'linked' : false,
sourcesContent: sourcemapOptions.sourcesContent,
outputNames,
includePaths: stylePreprocessorOptions?.includePaths,
// string[] | undefined' is not assignable to type '(Version | DeprecationOrId)[] | undefined'.
Original file line number Diff line number Diff line change
@@ -209,6 +209,82 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
harness.expectFile('dist/browser/main.js.map').content.toContain('"x_google_ignoreList"');
});

it(`should not include 'sourcesContent' field when 'sourcesContent' suboption is false`, async () => {
await harness.writeFile('src/styles.css', `div { flex: 1 }`);

harness.useTarget('build', {
...BASE_OPTIONS,
styles: ['src/styles.css'],
sourceMap: { scripts: true, styles: true, sourcesContent: false },
});

const { result } = await harness.executeOnce();

expect(result?.success).toBeTrue();

harness.expectFile('dist/browser/main.js.map').content.not.toContain('"sourcesContent"');

harness.expectFile('dist/browser/styles.css.map').toExist();
harness.expectFile('dist/browser/styles.css.map').content.not.toContain('"sourcesContent"');
});

it(`should include 'sourcesContent' field when 'sourcesContent' suboption is true`, async () => {
await harness.writeFile('src/styles.css', `div { flex: 1 }`);

harness.useTarget('build', {
...BASE_OPTIONS,
styles: ['src/styles.css'],
sourceMap: { scripts: true, styles: true, sourcesContent: true },
});

const { result } = await harness.executeOnce();

expect(result?.success).toBeTrue();

harness.expectFile('dist/browser/main.js.map').content.toContain('"sourcesContent"');

harness.expectFile('dist/browser/styles.css.map').toExist();
harness.expectFile('dist/browser/styles.css.map').content.toContain('"sourcesContent"');
});

it(`should include 'sourcesContent' field when 'sourcesContent' suboption is not present`, async () => {
await harness.writeFile('src/styles.css', `div { flex: 1 }`);

harness.useTarget('build', {
...BASE_OPTIONS,
styles: ['src/styles.css'],
sourceMap: { scripts: true, styles: true },
});

const { result } = await harness.executeOnce();

expect(result?.success).toBeTrue();

harness.expectFile('dist/browser/main.js.map').content.toContain('"sourcesContent"');

harness.expectFile('dist/browser/styles.css.map').toExist();
harness.expectFile('dist/browser/styles.css.map').content.toContain('"sourcesContent"');
});

it(`should include 'sourcesContent' field when 'sourceMap' is true`, async () => {
await harness.writeFile('src/styles.css', `div { flex: 1 }`);

harness.useTarget('build', {
...BASE_OPTIONS,
styles: ['src/styles.css'],
sourceMap: true,
});

const { result } = await harness.executeOnce();

expect(result?.success).toBeTrue();

harness.expectFile('dist/browser/main.js.map').content.toContain('"sourcesContent"');

harness.expectFile('dist/browser/styles.css.map').toExist();
harness.expectFile('dist/browser/styles.css.map').content.toContain('"sourcesContent"');
});

it('should generate component sourcemaps when sourcemaps when true', async () => {
await harness.writeFile('src/app/app.component.css', `* { color: red}`);

Original file line number Diff line number Diff line change
@@ -586,6 +586,7 @@ function getEsBuildCommonOptions(options: NormalizedApplicationBuildOptions): Bu
outdir: workspaceRoot,
outExtension: outExtension ? { '.js': `.${outExtension}` } : undefined,
sourcemap: sourcemapOptions.scripts && (sourcemapOptions.hidden ? 'external' : true),
sourcesContent: sourcemapOptions.sourcesContent,
splitting: true,
chunkNames: options.namedChunks ? '[name]-[hash]' : 'chunk-[hash]',
tsconfig,
1 change: 1 addition & 0 deletions packages/angular/build/src/tools/esbuild/global-styles.ts
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@ export function createGlobalStylesBundleOptions(
optimization: !!optimizationOptions.styles.minify,
inlineFonts: !!optimizationOptions.fonts.inline,
sourcemap: !!sourcemapOptions.styles && (sourcemapOptions.hidden ? 'external' : true),
sourcesContent: sourcemapOptions.sourcesContent,
preserveSymlinks,
target,
externalDependencies,
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ export interface BundleStylesheetOptions {
inlineFonts: boolean;
preserveSymlinks?: boolean;
sourcemap: boolean | 'external' | 'inline' | 'linked';
sourcesContent?: boolean;
outputNames: { bundles: string; media: string };
includePaths?: string[];
sass?: StylesheetPluginsass;
@@ -77,6 +78,7 @@ export function createStylesheetBundleOptions(
minify: options.optimization,
metafile: true,
sourcemap: options.sourcemap,
sourcesContent: options.sourcesContent,
outdir: options.workspaceRoot,
write: false,
platform: 'browser',
2 changes: 2 additions & 0 deletions packages/angular/build/src/utils/normalize-source-maps.ts
Original file line number Diff line number Diff line change
@@ -13,11 +13,13 @@ export function normalizeSourceMaps(sourceMap: SourceMapUnion): SourceMapClass {
const styles = typeof sourceMap === 'object' ? sourceMap.styles : sourceMap;
const hidden = (typeof sourceMap === 'object' && sourceMap.hidden) || false;
const vendor = (typeof sourceMap === 'object' && sourceMap.vendor) || false;
const sourcesContent = typeof sourceMap === 'object' ? sourceMap.sourcesContent : sourceMap;

return {
vendor,
hidden,
scripts,
styles,
sourcesContent,
};
}