handle multi-line comments
This commit is contained in:
27
build.ts
27
build.ts
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user