@ideasonpurpose/build-tools-wordpress 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +1 -1
- package/bin/format-php-prettier.js +88 -0
- package/package.json +11 -10
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. Dates are d
|
|
|
4
4
|
|
|
5
5
|
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
|
|
6
6
|
|
|
7
|
+
#### v2.1.1
|
|
8
|
+
|
|
9
|
+
> 9 April 2025
|
|
10
|
+
|
|
11
|
+
- Clean up outputs a little
|
|
12
|
+
|
|
7
13
|
#### v2.1.0
|
|
8
14
|
|
|
9
15
|
> 1 April 2025
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @ideasonpurpose/build-tools-wordpress
|
|
2
2
|
|
|
3
|
-
#### Version 2.1.
|
|
3
|
+
#### Version 2.1.2
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@ideasonpurpose/build-tools-wordpress)
|
|
6
6
|
[](https://github.com/ideasonpurpose/build-tools-wordpress#readme)
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This is an experimental proof-of-concept for formatting mixed HTML & PHP
|
|
5
|
+
* files from a single function.
|
|
6
|
+
*
|
|
7
|
+
* TODO: Testing, naming, modularization, VS Code extension
|
|
8
|
+
*/
|
|
9
|
+
import prettier from "prettier";
|
|
10
|
+
import prettierConfig from "@ideasonpurpose/prettier-config" with { type: "json" };
|
|
11
|
+
|
|
12
|
+
import { readFile, writeFile } from "fs/promises";
|
|
13
|
+
import { resolve, basename } from "path";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Prettier API doesn't recognize overrides, so we extract them
|
|
17
|
+
*/
|
|
18
|
+
const htmlOptions = prettierConfig.overrides.find(
|
|
19
|
+
(o) => o.files === "*.html",
|
|
20
|
+
)?.options;
|
|
21
|
+
|
|
22
|
+
const phpOptions = prettierConfig.overrides.find(
|
|
23
|
+
(o) => o.files === "*.php",
|
|
24
|
+
)?.options;
|
|
25
|
+
|
|
26
|
+
function tokenizeHTML(htmlContent) {
|
|
27
|
+
const phpCodeBlocks = {};
|
|
28
|
+
let tokenCount = 0;
|
|
29
|
+
|
|
30
|
+
// Tokens are end-padded to the length of the span, up to 80 characters
|
|
31
|
+
const tokenizedHTML = htmlContent.replace(
|
|
32
|
+
/<\?(?:php|=)[\s\S]*?\?>/gs,
|
|
33
|
+
(phpCodeBlock) => {
|
|
34
|
+
const codeLength = Math.min(phpCodeBlock.length, 80);
|
|
35
|
+
const token = `__php_${tokenCount++}__`.padEnd(codeLength, "_");
|
|
36
|
+
phpCodeBlocks[token] = phpCodeBlock;
|
|
37
|
+
return token;
|
|
38
|
+
},
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
return { tokenizedHTML, phpCodeBlocks };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function unTokenizeHTML(htmlContent, tokens) {
|
|
45
|
+
let phpContent = htmlContent;
|
|
46
|
+
for (const token in tokens) {
|
|
47
|
+
phpContent = phpContent.replace(new RegExp(token, "g"), tokens[token]);
|
|
48
|
+
}
|
|
49
|
+
return phpContent;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function formatHTMLThenPHP(filepath) {
|
|
53
|
+
try {
|
|
54
|
+
const startTime = process.hrtime.bigint();
|
|
55
|
+
const rawFile = await readFile(filepath, "utf8");
|
|
56
|
+
|
|
57
|
+
const { tokenizedHTML, phpCodeBlocks } = tokenizeHTML(rawFile);
|
|
58
|
+
|
|
59
|
+
const htmlFormatted = await prettier.format(tokenizedHTML, {
|
|
60
|
+
...prettierConfig,
|
|
61
|
+
...htmlOptions,
|
|
62
|
+
parser: "html",
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const phpUnTokenized = unTokenizeHTML(htmlFormatted, phpCodeBlocks);
|
|
66
|
+
|
|
67
|
+
const phpFormatted = await prettier.format(phpUnTokenized, {
|
|
68
|
+
...prettierConfig,
|
|
69
|
+
...phpOptions,
|
|
70
|
+
parser: "php",
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
await writeFile(filepath, phpFormatted, "utf8");
|
|
74
|
+
const endTime = process.hrtime.bigint();
|
|
75
|
+
const duration = Number(endTime - startTime);
|
|
76
|
+
|
|
77
|
+
console.log(`${basename(filepath)} ${(duration / 1e6).toFixed(2)}ms`);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error("Error:", error);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (process.argv[2]) {
|
|
84
|
+
const fullPath = resolve(process.argv[2]);
|
|
85
|
+
formatHTMLThenPHP(fullPath);
|
|
86
|
+
} else {
|
|
87
|
+
console.error("Error: A filepath is required.");
|
|
88
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ideasonpurpose/build-tools-wordpress",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.2",
|
|
4
4
|
"description": "Build scripts and dependencies for IOP's WordPress development environments.",
|
|
5
5
|
"homepage": "https://github.com/ideasonpurpose/build-tools-wordpress#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"bin": {
|
|
22
22
|
"iop-build-port-reporter": "./bin/port-reporter.js",
|
|
23
23
|
"iop-build-zip-archive": "./bin/zip.js",
|
|
24
|
-
"iop-project-init": "./bin/project-init.js"
|
|
24
|
+
"iop-project-init": "./bin/project-init.js",
|
|
25
|
+
"iop-html-php-prettier": "./bin/format-php-prettier.js"
|
|
25
26
|
},
|
|
26
27
|
"directories": {
|
|
27
28
|
"lib": "lib"
|
|
@@ -32,19 +33,19 @@
|
|
|
32
33
|
},
|
|
33
34
|
"prettier": "@ideasonpurpose/prettier-config",
|
|
34
35
|
"dependencies": {
|
|
35
|
-
"@ideasonpurpose/prettier-config": "^1.0.
|
|
36
|
+
"@ideasonpurpose/prettier-config": "^1.0.1",
|
|
36
37
|
"@ideasonpurpose/stylelint-config": "^1.1.1",
|
|
37
38
|
"@rollup/plugin-commonjs": "^28.0.3",
|
|
38
39
|
"@rollup/plugin-json": "^6.1.0",
|
|
39
40
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
40
41
|
"@svgr/webpack": "^8.1.0",
|
|
41
|
-
"@wordpress/dependency-extraction-webpack-plugin": "^6.
|
|
42
|
+
"@wordpress/dependency-extraction-webpack-plugin": "^6.22.0",
|
|
42
43
|
"ansi-html": "^0.0.9",
|
|
43
44
|
"archiver": "^7.0.1",
|
|
44
45
|
"auto-changelog": "^2.5.0",
|
|
45
46
|
"autoprefixer": "^10.4.21",
|
|
46
47
|
"babel-loader": "^10.0.0",
|
|
47
|
-
"caniuse-lite": "^1.0.
|
|
48
|
+
"caniuse-lite": "^1.0.30001713",
|
|
48
49
|
"chalk": "^5.4.1",
|
|
49
50
|
"chalk-cli": "^5.0.1",
|
|
50
51
|
"classnames": "^2.5.1",
|
|
@@ -54,9 +55,9 @@
|
|
|
54
55
|
"cross-env": "^7.0.3",
|
|
55
56
|
"css-loader": "^7.1.2",
|
|
56
57
|
"cssnano": "^7.0.6",
|
|
57
|
-
"dotenv": "^16.
|
|
58
|
+
"dotenv": "^16.5.0",
|
|
58
59
|
"esbuild-loader": "^4.3.0",
|
|
59
|
-
"eslint": "^9.
|
|
60
|
+
"eslint": "^9.24.0",
|
|
60
61
|
"filesize": "^10.1.6",
|
|
61
62
|
"fs-extra": "^11.3.0",
|
|
62
63
|
"globby": "^14.1.0",
|
|
@@ -73,17 +74,17 @@
|
|
|
73
74
|
"pretty-hrtime": "^1.0.3",
|
|
74
75
|
"read-package-up": "^11.0.0",
|
|
75
76
|
"replacestream": "^4.0.3",
|
|
76
|
-
"sass-embedded": "^1.86.
|
|
77
|
+
"sass-embedded": "^1.86.3",
|
|
77
78
|
"sass-loader": "^16.0.5",
|
|
78
79
|
"semver": "^7.7.1",
|
|
79
|
-
"sharp": "^0.
|
|
80
|
+
"sharp": "^0.34.1",
|
|
80
81
|
"sort-package-json": "^3.0.0",
|
|
81
82
|
"string-length": "^6.0.0",
|
|
82
83
|
"style-loader": "^4.0.0",
|
|
83
84
|
"svgo": "^3.3.2",
|
|
84
85
|
"svgo-loader": "^4.0.0",
|
|
85
86
|
"version-everything": "^0.11.4",
|
|
86
|
-
"webpack": "^5.
|
|
87
|
+
"webpack": "^5.99.5",
|
|
87
88
|
"webpack-bundle-analyzer": "^4.10.2",
|
|
88
89
|
"webpack-dev-middleware": "^7.4.2",
|
|
89
90
|
"webpack-dev-server": "^5.2.1"
|