Skip to content

Commit cf5ceac

Browse files
author
Cédric Belin
committed
Port the GulpPlugin class
1 parent 949e1a8 commit cf5ceac

12 files changed

+118
-153
lines changed

lib/fast_transformer.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import {Transformer} from "./transformer.js";
2-
31
/**
42
* Removes comments and whitespace from a PHP script, by calling a Web service.
53
*/
6-
export class FastTransformer implements Transformer {
4+
export class FastTransformer {
75

86
/**
97
* Creates a new fast transformer.

lib/gulp_plugin.d.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {Transform, TransformCallback} from "node:stream";
2+
3+
/**
4+
* Defines the options of a {@link GulpPlugin} instance.
5+
*/
6+
export type GulpPluginOptions = Partial<{
7+
8+
/**
9+
* The path to the PHP executable.
10+
*/
11+
binary: string;
12+
13+
/**
14+
* The operation mode of the plugin.
15+
*/
16+
mode: "fast"|"safe";
17+
18+
/**
19+
* Value indicating whether to silence the plugin output.
20+
*/
21+
silent: boolean;
22+
}>;
23+
24+
/**
25+
* Minifies PHP source code by removing comments and whitespace.
26+
*/
27+
export class GulpPlugin extends Transform {
28+
29+
/**
30+
* Creates a new plugin.
31+
* @param options An object providing values to initialize this instance.
32+
*/
33+
constructor(options?: GulpPluginOptions);
34+
35+
/**
36+
* Transforms input and produces output.
37+
* @param chunk The chunk to transform.
38+
* @param encoding The encoding type if the chunk is a string.
39+
* @param callback The function to invoke when the supplied chunk has been processed.
40+
* @returns The transformed chunk.
41+
*/
42+
_transform(chunk: File, encoding: NodeJS.BufferEncoding, callback: TransformCallback): Promise<void>;
43+
}

lib/index.d.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
11
import {Transform} from "node:stream";
2+
import {GulpPluginOptions} from "./gulp_plugin.js";
23
export * from "./fast_transformer.js";
4+
export * from "./gulp_plugin.js";
35
export * from "./safe_transformer.js";
4-
export * from "./transformer.js";
5-
6-
/**
7-
* Defines the options of a {@link GulpPlugin} instance.
8-
*/
9-
export type GulpPluginOptions = Partial<{
10-
11-
/**
12-
* The path to the PHP executable.
13-
*/
14-
binary: string;
15-
16-
/**
17-
* The operation mode of the plugin.
18-
*/
19-
mode: "fast"|"safe";
20-
21-
/**
22-
* Value indicating whether to silence the plugin output.
23-
*/
24-
silent: boolean;
25-
}>;
266

277
/**
288
* Creates a new plugin.

lib/safe_transformer.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import {Transformer} from "./transformer.js";
2-
31
/**
42
* Removes comments and whitespace from a PHP script, by calling a PHP process.
53
*/
6-
export class SafeTransformer implements Transformer {
4+
export class SafeTransformer {
75

86
/**
97
* Creates a new safe transformer.

lib/transformer.d.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/fast_transformer.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class FastTransformer
4545

4646
# Gets an ephemeral TCP port chosen by the system.
4747
_getPort: -> new Promise (fulfill, reject) ->
48-
server = createServer().unref().on "error", reject
48+
server = createServer().unref().on("error", reject)
4949
server.listen host: "127.0.0.1", port: 0, ->
5050
{port} = server.address()
5151
server.close -> fulfill port

src/gulp_plugin.coffee

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import log from "fancy-log"
2+
import {Buffer} from "node:buffer"
3+
import {Transform} from "node:stream"
4+
import {FastTransformer} from "./fast_transformer.js"
5+
import {SafeTransformer} from "./safe_transformer.js"
6+
7+
# Minifies PHP source code by removing comments and whitespace.
8+
export class GulpPlugin extends Transform
9+
10+
# Creates a new plugin.
11+
constructor: (options = {}) ->
12+
super objectMode: true
13+
14+
binary = options.binary ? "php"
15+
transformer = if options.mode ? "safe" is "fast" then new FastTransformer binary else new SafeTransformer binary
16+
close = -> await transformer.close()
17+
@on("end", close).on("error", close)
18+
19+
# Value indicating whether to silence the plugin output.
20+
@_silent = options.silent ? no
21+
22+
# The instance used to process the PHP code
23+
@_transformer = transformer
24+
25+
# Transforms input and produces output.
26+
_transform: (chunk, encoding, callback) ->
27+
try
28+
log "Minifying: #{chunk.relative}" unless @_silent
29+
chunk.contents = Buffer.from await @_transformer.transform(chunk.path), encoding
30+
callback null, chunk
31+
32+
catch error
33+
failure = if error instanceof Error then error else String error
34+
callback new PluginError "@cedx/php-minifier", failure, fileName: chunk.path

src/index.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./fast_transformer.js"
2+
export * from "./gulp_plugin.js"
23
export * from "./safe_transformer.js"

src/php_minifier/GulpPlugin.hx

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/php_minifier/TransformMode.hx

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)