handle multi-line comments

This commit is contained in:
2026-05-01 18:27:51 +02:00
parent 92ac475d58
commit c78a2ff7a0
8 changed files with 188 additions and 168 deletions

View File

@@ -30,6 +30,7 @@ const spritesPath = {
class Minifier {
private buffer = "";
private lines: Array<string> = [];
private insideComment = false;
stream;
constructor(writableStream: WritableStream) {
@@ -51,6 +52,32 @@ class Minifier {
}
private processChunk(): string {
const commentFullRegex = /\/\*.*\*\//;
const commentStartRegex = /(.*)\/\*(.*)/;
const commentEndRegex = /(.*)\*\/(.*)/;
if (this.insideComment) {
const matches = this.buffer.match(commentEndRegex);
if (matches === null) {
this.buffer = '';
return '';
}
this.buffer = matches[2];
this.insideComment = false;
}
if (!this.insideComment) {
this.buffer = this.buffer.replace(commentFullRegex, '');
const matches = this.buffer.match(commentStartRegex);
if (matches !== null) {
this.buffer = matches[1];
this.insideComment = true;
return '';
}
}
const newLines = this.buffer.split("\n");
this.buffer = newLines.pop() as string; // keep the last (potentially incomplete) line