Skip to content

Commit 5e37db2

Browse files
committed
Add a code sample [skip ci]
1 parent 4477f7e commit 5e37db2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

example/Transformer.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {FastTransformer, SafeTransformer} from "@cedx/php-minifier";
2+
import console from "node:console";
3+
import {mkdir, readdir, writeFile} from "node:fs/promises";
4+
import {dirname, join, relative} from "node:path";
5+
import {env} from "node:process";
6+
7+
// Choose an appropriate transformer.
8+
await using transformer = env.PHPMINIFIER_MODE == "fast" ? new FastTransformer : new SafeTransformer;
9+
10+
// Scan the input directory for PHP files.
11+
const input = "path/to/source/folder";
12+
const files = (await readdir(input, {recursive: true, withFileTypes: true}))
13+
.filter(item => item.isFile() && item.name.endsWith(".php"));
14+
15+
// Write the transformed files to the output directory.
16+
const output = "path/to/destination/folder";
17+
for (const file of files) {
18+
const fullPath = join(file.parentPath, file.name);
19+
const relativePath = relative(input, fullPath);
20+
console.log(`Minifying: ${relativePath}`);
21+
22+
const script = await transformer.transform(fullPath);
23+
const target = join(output, relativePath);
24+
await mkdir(dirname(target), {recursive: true});
25+
await writeFile(target, script);
26+
}

0 commit comments

Comments
 (0)