Skip to content

Commit

Permalink
Alternative fix for #393
Browse files Browse the repository at this point in the history
This keeps whitespace around comments while they're
extracted from the content, and leaves it up to
stripWhitespace later on to strip when appropriate,
ensureing that only the contents of the comment are
preserved or stripped (in stripComments), not the
whitespace around it.
  • Loading branch information
matthiasmullie committed Apr 19, 2022
1 parent 0c77ac9 commit 7d4542b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
16 changes: 6 additions & 10 deletions src/JS.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,28 +199,24 @@ protected function stripComments()
$minifier = $this;
$callback = function ($match) use ($minifier) {
if (
substr($match[1], 0, 1) === '!' ||
strpos($match[1], '@license') !== false ||
strpos($match[1], '@preserve') !== false
substr($match[2], 0, 1) === '!' ||
strpos($match[2], '@license') !== false ||
strpos($match[2], '@preserve') !== false
) {
// preserve multi-line comments that start with /*!
// or contain @license or @preserve annotations
$count = count($minifier->extracted);
$placeholder = '/*'.$count.'*/';
$minifier->extracted[$placeholder] = $match[0];

return $placeholder . ($match[3] ? $match[2] . $match[3] : '');
}
// should not remove the \n before a var|let etc. at this stage, because it could be a case like this: var a=1\n/*comment*/\nvar b=2;
if($match[3]) {
return $match[2] . $match[3];
return $match[1] . $placeholder . $match[3];
}

return '';
return $match[1] . $match[3];
};

// multi-line comments
$this->registerPattern('/\n?\/\*(.*?)\*\/(\n?)(var|let|const|enum|function|class|)/s', $callback);
$this->registerPattern('/(\n?)\/\*(.*?)\*\/(\n?)/s', $callback);

// single-line comments
$this->registerPattern('/\/\/.*$/m', '');
Expand Down
18 changes: 16 additions & 2 deletions tests/js/JSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,21 @@ function someOtherFunction() {
'var crypt=function(){}
var Sbox=2',
);
$tests[] = array(
'a = function() {}
/* some comment to be removed */
b = 2',
'a=function(){}
b=2',
);
$tests[] = array(
'a = function() {}
/* @preserve some comment to be preserved */
b = 2',
'a=function(){}
/* @preserve some comment to be preserved */
b=2',
);

// known minified files to help doublecheck changes in places not yet
// anticipated in these tests
Expand All @@ -1359,13 +1374,12 @@ function someOtherFunction() {
}
}

//some other files that are minified correctly, ensure they stay like this
// some other files that are minified correctly, ensure they stay like this
// https://github.com/matthiasmullie/minify/issues/393
$source = trim(file_get_contents(__DIR__.'/sample/source/Decrypt.js'));
$minified = trim(file_get_contents(__DIR__.'/sample/minified2/Decrypt.min.js'));
$tests[] = array($source, $minified);


return $tests;
}
}

0 comments on commit 7d4542b

Please sign in to comment.