In my view scripts I am adding javascript using the view helper inlineScript
and echoing it in the footer of my template. I am now attempting to minify the generated html using this solution.
My issue is that I include inline comments (e.g. //this is a comment
) throughout my code (as I am a good developer) which is causing all code following to be regarded as comment as well (as all new lines are removed and the following code is placed onto the same line as the inline comment).
How do I extend inlineScript
to remove comments and / or minify the code using, for example, mrclay minify?
Something I tried was:
<?php echo preg_replace('#//.*#', '', $this->inlineScript()) ?>
Which causes issues on pages where I have code such as:
jQuery(el).attr('data-toggle', 'popover')
.attr('data-trigger', 'hover')
.attr('data-html', 'true')
.attr('data-content', [
'<img style="height: 75px; border: 1px solid #000; margin: 5px" src="',
'//' + location.hostname + '/img/broker-logo/' + el.value,
'"/>'
].join(''))
And, a variation of above,
<?php echo preg_replace('#[\s\t]+//.*#', '', $this->inlineScript()) ?>
Which checks for comments on that don't have anything before it. This raises issues where I have code followed by a comment at the end of the line:
var $el = jQuery('table.hover') //only apply effect to hover tables
which produces the same undesirable result as the original issue.