-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
118 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.