Skip to content

Commit 8538190

Browse files
Speed up multi-line comment preservation regex
1 parent 6802f36 commit 8538190

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/JS.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,25 @@ protected function stripComments()
198198
// PHP only supports $this inside anonymous functions since 5.4
199199
$minifier = $this;
200200
$callback = function ($match) use ($minifier) {
201-
$count = count($minifier->extracted);
202-
$placeholder = '/*'.$count.'*/';
203-
$minifier->extracted[$placeholder] = $match[0];
201+
if (
202+
substr($match[1], 0, 1) === '!' ||
203+
strpos($match[1], '@license') !== false ||
204+
strpos($match[1], '@preserve') !== false
205+
) {
206+
// preserve multi-line comments that start with /*!
207+
// or contain @license or @preserve annotations
208+
$count = count($minifier->extracted);
209+
$placeholder = '/*'.$count.'*/';
210+
$minifier->extracted[$placeholder] = $match[0];
211+
212+
return $placeholder;
213+
}
204214

205-
return $placeholder;
215+
return '';
206216
};
217+
207218
// multi-line comments
208-
$this->registerPattern('/\n?\/\*(!|.*?@license|.*?@preserve).*?\*\/\n?/s', $callback);
209-
$this->registerPattern('/\/\*.*?\*\//s', '');
219+
$this->registerPattern('/\n?\/\*(.*?)\*\/\n?/s', $callback);
210220

211221
// single-line comments
212222
$this->registerPattern('/\/\/.*$/m', '');

0 commit comments

Comments
 (0)