Skip to content
Merged
Show file tree
Hide file tree
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
60 changes: 26 additions & 34 deletions packages/angular/build/src/utils/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import { fileURLToPath } from 'node:url';
* @returns The code with top-level sourcemap comments removed.
*/
export function removeSourceMappingURL(code: string): string {
if (!code.includes('//# sourceMappingURL=')) {
return code;
}

const result: string[] = [];
let lastCopiedIndex = 0;
let i = 0;
const len = code.length;

Expand All @@ -30,7 +35,6 @@ export function removeSourceMappingURL(code: string): string {
let currentState:
'normal' | 'string_double' | 'string_single' | 'template' | 'comment_block' | 'comment_line' =
'normal';
let isStrippingComment = false;

while (i < len) {
const char = code[i];
Expand All @@ -39,20 +43,19 @@ export function removeSourceMappingURL(code: string): string {
if (currentState === 'normal') {
if (char === '/' && nextChar === '*') {
currentState = 'comment_block';
result.push('/*');
i += 2;
continue;
}
if (char === '/' && nextChar === '/') {
// Detect if the comment is escaped (e.g. inside a regex literal like `/\/\/#/`).
let isEscaped = false;
let prevIdx = result.length - 1;
while (prevIdx >= 0 && /\s/.test(result[prevIdx])) {
let prevIdx = i - 1;
while (prevIdx >= 0 && /\s/.test(code[prevIdx])) {
prevIdx--;
}
if (prevIdx >= 0 && result[prevIdx] === '\\') {
if (prevIdx >= 0 && code[prevIdx] === '\\') {
let bsCount = 0;
while (prevIdx >= 0 && result[prevIdx] === '\\') {
while (prevIdx >= 0 && code[prevIdx] === '\\') {
bsCount++;
prevIdx--;
}
Expand All @@ -62,34 +65,35 @@ export function removeSourceMappingURL(code: string): string {
}

if (!isEscaped && code.startsWith('//# sourceMappingURL=', i)) {
currentState = 'comment_line';
isStrippingComment = true;
i += 21; // Skip the '//# sourceMappingURL=' prefix
if (i > lastCopiedIndex) {
result.push(code.slice(lastCopiedIndex, i));
}
// Skip the rest of the comment line up to the newline
i += 21;
while (i < len && code[i] !== '\n' && code[i] !== '\r') {
i++;
}
lastCopiedIndex = i;
continue;
} else {
currentState = 'comment_line';
isStrippingComment = false;
result.push('//');
i += 2;
continue;
}
}
if (char === '"') {
currentState = 'string_double';
result.push('"');
i++;
continue;
}
if (char === "'") {
currentState = 'string_single';
result.push("'");
i++;
continue;
}
if (char === '`') {
currentState = 'template';
stack.push({ type: 'template', braceDepth: 0 });
result.push('`');
i++;
continue;
}
Expand All @@ -98,7 +102,6 @@ export function removeSourceMappingURL(code: string): string {
if (top) {
top.braceDepth++;
}
result.push('{');
i++;
continue;
}
Expand All @@ -110,84 +113,73 @@ export function removeSourceMappingURL(code: string): string {
// Exiting a template literal interpolation ${ ... }
stack.pop();
currentState = 'template';
result.push('}');
i++;
continue;
}
}
result.push('}');
i++;
continue;
}

result.push(char);
i++;
} else if (currentState === 'string_double') {
if (char === '\\') {
result.push(char, nextChar || '');
i += 2;
continue;
}
if (char === '"') {
currentState = 'normal';
}
result.push(char);
i++;
} else if (currentState === 'string_single') {
if (char === '\\') {
result.push(char, nextChar || '');
i += 2;
continue;
}
if (char === "'") {
currentState = 'normal';
}
result.push(char);
i++;
} else if (currentState === 'template') {
if (char === '\\') {
result.push(char, nextChar || '');
i += 2;
continue;
}
if (char === '$' && nextChar === '{') {
// Entering template literal interpolation context
currentState = 'normal';
stack.push({ type: 'template', braceDepth: 0 });
result.push('${');
i += 2;
continue;
}
if (char === '`') {
stack.pop();
currentState = 'normal';
}
result.push(char);
i++;
} else if (currentState === 'comment_block') {
if (char === '*' && nextChar === '/') {
currentState = 'normal';
result.push('*/');
i += 2;
continue;
}
result.push(char);
i++;
} else if (currentState === 'comment_line') {
if (char === '\n' || char === '\r') {
currentState = 'normal';
isStrippingComment = false;
result.push(char);
i++;
continue;
}
if (!isStrippingComment) {
result.push(char);
}
i++;
}
}

if (lastCopiedIndex === 0) {
return code;
}

if (lastCopiedIndex < len) {
result.push(code.slice(lastCopiedIndex));
}

return result.join('');
}

Expand Down
10 changes: 10 additions & 0 deletions packages/angular/build/src/utils/source-map_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,14 @@ describe('removeSourceMappingURL', () => {
const code = '// # sourceMappingURL=main.js.map\n//# sourceMappingURL=main.js.map';
expect(removeSourceMappingURL(code)).toBe('// # sourceMappingURL=main.js.map\n');
});

it('should return the exact input string when no sourcemap comment is present', () => {
const code = 'const x = 1;\nconsole.log(x);';
expect(removeSourceMappingURL(code)).toBe(code);
});

it('should handle sourcemap comments with CRLF newlines', () => {
const code = 'console.log("hello");\r\n//# sourceMappingURL=main.js.map\r\nconst next = 2;';
expect(removeSourceMappingURL(code)).toBe('console.log("hello");\r\n\r\nconst next = 2;');
});
});