Skip to content

Gulp plugin

If you haven't used Gulp before, be sure to check out the related documentation, as it explains how to create a gulpfile.js, as well as install and use plugins. Once you're familiar with that process, you may install this plugin.

Programming interface

The plugin takes a list of PHP scripts as input, and removes the comments and whitespace in these files by applying the php_strip_whitespace() function on their contents:

import gulp from "gulp";
import phpMinifier from "@cedx/php-minifier";

export function compressPhp() {
  return gulp.src("path/to/**/*.php", {read: false})
    .pipe(phpMinifier())
    .pipe(gulp.dest("path/to/out"));
}

Warning

The plugin only needs the file paths, so you should specify the read option to false when providing the file list, and you should not have any other plugin before it.

Options

binary: string = "php"

The plugin relies on the availability of the PHP executable on the target system. By default, the plugin will use the php binary found on the system path.
If the plugin cannot find the default php binary, or if you want to use a different one, you can provide the path to the php executable by using the binary option:

import gulp from "gulp";
import phpMinifier from "@cedx/php-minifier";

export function compressPhp() {
  return gulp.src("path/to/**/*.php", {read: false})
    .pipe(phpMinifier({binary: "C:\\Program Files\\PHP\\php.exe"}))
    .pipe(gulp.dest("path/to/out"));
}

mode: TransformMode = "safe"

The plugin can work in two manners, which can be selected using the mode option:

  • the safe mode: as its name implies, this mode is very reliable. But it is also very slow as it spawns a new PHP process for every file to be processed. This is the default mode.
  • the fast mode: as its name implies, this mode is very fast, but it is not always reliable. It spawns a PHP web server that processes the input files, but on some systems this fails.
import gulp from "gulp";
import phpMinifier from "@cedx/php-minifier";

export function compressPhp() {
  return gulp.src("path/to/**/*.php", {read: false})
    .pipe(phpMinifier({mode: "fast"}))
    .pipe(gulp.dest("path/to/out"));
}

Tip

The plugin defaults to the safe mode, but you should really give a try to the fast one.
The difference is very noticeable.

silent: boolean = false

By default, the plugin prints to the standard output the paths of the minified scripts. You can disable this output by setting the silent option to true.

import gulp from "gulp";
import phpMinifier from "@cedx/php-minifier";

export function compressPhp() {
  return gulp.src("path/to/**/*.php", {read: false})
    .pipe(phpMinifier({silent: true}))
    .pipe(gulp.dest("path/to/out"));
}