Minimize HTML, but don't touch PHP with Gulp

Problem

I have a lot of .php files, mostly containing HTML, but also some PHP lines at the top (like form launch code or similar). Therefore they look like

<?php
if($someValue){
    //doSth
}
//more content
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <title>My Website</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
<!-- Content and scripts here -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</body>
</html>

purpose

My goal is to reduce HTML (and maybe even inline javascript, but it's a bit more) without touching PHP on top. I use Gulp as an auto-build tool and would like to see a solution using this tool and any additional packages as needed.

+4
source share
1 answer

gulp -htmlmin html- minifier, ( npmjs.com github), . , , ignoreCustomFragments.

var gulp = require(gulp),
    htmlmin = require(gulp-htmlmin);

gulp.task('htmltask', function(){
  return gulp.src(['./dev/*.html','./dev/*.php'])
      .pipe(htmlmin({
        collapseWhitespace: true,
        ignoreCustomFragments: [ /<%[\s\S]*?%>/, /<\?[=|php]?[\s\S]*?\?>/ ]
      }))
      .pipe(gulp.dest('./site'));
});

ignoreCustomFragments regex /<\?[=|php]?[\s\S]*?\?>/, , <? <?php ?>.

html- minifier php, ignoreCustomFragments.

+6

Source: https://habr.com/ru/post/1658014/


All Articles