@cedx/php-minifier
Version:
Minify PHP source code by removing comments and whitespace.
46 lines (45 loc) • 1.27 kB
JavaScript
import { execFile } from "node:child_process";
import { normalize, resolve } from "node:path";
import { promisify } from "node:util";
/**
* Spawns a new process using the specified command.
*/
const run = promisify(execFile);
/**
* Removes comments and whitespace from a PHP script, by calling a PHP process.
*/
export class SafeTransformer {
/**
* The path to the PHP executable.
*/
#executable;
/**
* Creates a new safe transformer.
* @param executable The path to the PHP executable.
*/
constructor(executable = "php") {
this.#executable = normalize(executable);
}
/**
* Releases any resources associated with this object.
* @returns Resolves when this object is finally disposed.
*/
[Symbol.asyncDispose]() {
return this.close();
}
/**
* Closes this transformer.
* @returns Resolves when the transformer has been closed.
*/
close() {
return Promise.resolve();
}
/**
* Processes a PHP script.
* @param file The path to the PHP script.
* @returns The transformed script.
*/
async transform(file) {
return (await run(this.#executable, ["-w", resolve(file)], { maxBuffer: 20 * 1024 * 1024 })).stdout;
}
}