Skip to content

Commit

Permalink
Port the GulpPlugin class
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Nov 4, 2024
1 parent 46db498 commit 13544d2
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 153 deletions.
4 changes: 1 addition & 3 deletions lib/fast_transformer.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {Transformer} from "./transformer.js";

/**
* Removes comments and whitespace from a PHP script, by calling a Web service.
*/
export class FastTransformer implements Transformer {
export class FastTransformer {

/**
* Creates a new fast transformer.
Expand Down
43 changes: 43 additions & 0 deletions lib/gulp_plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {Transform, TransformCallback} from "node:stream";

/**
* Defines the options of a {@link GulpPlugin} instance.
*/
export type GulpPluginOptions = Partial<{

/**
* The path to the PHP executable.
*/
binary: string;

/**
* The operation mode of the plugin.
*/
mode: "fast"|"safe";

/**
* Value indicating whether to silence the plugin output.
*/
silent: boolean;
}>;

/**
* Minifies PHP source code by removing comments and whitespace.
*/
export class GulpPlugin extends Transform {

/**
* Creates a new plugin.
* @param options An object providing values to initialize this instance.
*/
constructor(options?: GulpPluginOptions);

/**
* Transforms input and produces output.
* @param chunk The chunk to transform.
* @param encoding The encoding type if the chunk is a string.
* @param callback The function to invoke when the supplied chunk has been processed.
* @returns The transformed chunk.
*/
_transform(chunk: File, encoding: NodeJS.BufferEncoding, callback: TransformCallback): Promise<void>;
}
24 changes: 2 additions & 22 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
import {Transform} from "node:stream";
import {GulpPluginOptions} from "./gulp_plugin.js";
export * from "./fast_transformer.js";
export * from "./gulp_plugin.js";
export * from "./safe_transformer.js";
export * from "./transformer.js";

/**
* Defines the options of a {@link GulpPlugin} instance.
*/
export type GulpPluginOptions = Partial<{

/**
* The path to the PHP executable.
*/
binary: string;

/**
* The operation mode of the plugin.
*/
mode: "fast"|"safe";

/**
* Value indicating whether to silence the plugin output.
*/
silent: boolean;
}>;

/**
* Creates a new plugin.
Expand Down
4 changes: 1 addition & 3 deletions lib/safe_transformer.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {Transformer} from "./transformer.js";

/**
* Removes comments and whitespace from a PHP script, by calling a PHP process.
*/
export class SafeTransformer implements Transformer {
export class SafeTransformer {

/**
* Creates a new safe transformer.
Expand Down
18 changes: 0 additions & 18 deletions lib/transformer.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/fast_transformer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class FastTransformer

# Gets an ephemeral TCP port chosen by the system.
_getPort: -> new Promise (fulfill, reject) ->
server = createServer().unref().on "error", reject
server = createServer().unref().on("error", reject)
server.listen host: "127.0.0.1", port: 0, ->
{port} = server.address()
server.close -> fulfill port
34 changes: 34 additions & 0 deletions src/gulp_plugin.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import log from "fancy-log"
import {Buffer} from "node:buffer"
import {Transform} from "node:stream"
import {FastTransformer} from "./fast_transformer.js"
import {SafeTransformer} from "./safe_transformer.js"

# Minifies PHP source code by removing comments and whitespace.
export class GulpPlugin extends Transform

# Creates a new plugin.
constructor: (options = {}) ->
super objectMode: true

binary = options.binary ? "php"
transformer = if options.mode ? "safe" is "fast" then new FastTransformer binary else new SafeTransformer binary
close = -> await transformer.close()
@on("end", close).on("error", close)

# Value indicating whether to silence the plugin output.
@_silent = options.silent ? no

# The instance used to process the PHP code
@_transformer = transformer

# Transforms input and produces output.
_transform: (chunk, encoding, callback) ->
try
log "Minifying: #{chunk.relative}" unless @_silent
chunk.contents = Buffer.from await @_transformer.transform(chunk.path), encoding
callback null, chunk

catch error
failure = if error instanceof Error then error else String error
callback new PluginError "@cedx/php-minifier", failure, fileName: chunk.path
1 change: 1 addition & 0 deletions src/index.coffee
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./fast_transformer.js"
export * from "./gulp_plugin.js"
export * from "./safe_transformer.js"
61 changes: 0 additions & 61 deletions src/php_minifier/GulpPlugin.hx

This file was deleted.

11 changes: 0 additions & 11 deletions src/php_minifier/TransformMode.hx

This file was deleted.

35 changes: 35 additions & 0 deletions test/gulp_plugin_test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {GulpPlugin} from "@cedx/php-minifier"
import {doesNotReject, ifError, ok} from "node:assert/strict"
import {resolve} from "node:path"
import {after, describe, it} from "node:test"
import File from "vinyl"

# Tests the features of the `GulpPlugin` class.
describe "GulpPlugin", ->
describe "_transform()", ->
map = new Map [
["should remove the inline comments", "<?= 'Hello World!' ?>"]
["should remove the multi-line comments", "namespace dummy; class Dummy"]
["should remove the single-line comments", "$className = get_class($this); return $className;"]
["should remove the whitespace", "__construct() { $this->property"]
]

describe "fast", ->
file = new File path: resolve "res/sample.php"
plugin = new GulpPlugin mode: "fast", silent: yes
after -> plugin.emit "end"

for [key, value] from map then it key, ->
await doesNotReject plugin._transform file, "utf8", (error, chunk) ->
ifError error
ok chunk.contents.toString().includes value

describe "safe", ->
file = new File path: resolve "res/sample.php"
plugin = new GulpPlugin mode: "safe", silent: yes
after -> plugin.emit "end"

for [key, value] from map then it key, ->
await doesNotReject plugin._transform file, "utf8", (error, chunk) ->
ifError error
ok chunk.contents.toString().includes value
34 changes: 0 additions & 34 deletions test/php_minifier/GulpPluginTest.hx

This file was deleted.

0 comments on commit 13544d2

Please sign in to comment.