constomatic
v1.0.1
Published
Injects a random alphanumeric string as a value in a specified set of PHP constants. Designed for use in a watch task as a means of automated cachebusting for a WordPress script/style enqueue.
Downloads
3
Maintainers
Readme
constomatic
Install
$ npm install -D constomatic
Usage
Gulp not required, but recommended.
Create a gulp task. (See options reference below)
/gulpfile.js (Gulp v3 example)
var constomatic = require("constomatic");
gulp.task("bump-css", function() {
constomatic({
src: "functions.php",
dest: "/",
constNames: ["CSS_VERSION", "JS_VERSION"]
});
});
/gulpfile.js (Gulp v4 example)
const constomatic = require("constomatic");
const updateStylesVersion = cb => {
constomatic({
src: "functions.php",
dest: "/",
constNames: ["CSS_VERSION"],
hashLength: 7
});
cb();
};
Create at least one const (define style) definition in your src php file and set any alphanumeric string as the value.
/functions.php
define('CSS_VERSION', 'abc123');
define('JS_VERSION', 'abc123');
Run the task in terminal, or set it as a part of your watch setup.
Add to a watch task (gulp@v3)
gulp.task("default", ["browser-sync"], function() {
gulp.watch("library/scss/**/*.scss", ["styles", "bump-css"]);
gulp.watch("*.php", ["bs-reload"]);
});
or gulp@v4
const watchStyles = () => {
watch("library/scss/**/*.scss", series(compileStyles, updateStylesVersion));
};
Check out your newly hashed values in your php file.
/functions.php
define('CSS_VERSION', 'gbkPNaG');
define('JS_VERSION', 'iO75jf5');
Now you can echo the const values into any corresponding wp_register_style/script statements
Options
src
Type: String
Default: "/build/functions.php"
Path to the source file. Usually set to "functions.php"
dest
Type: String
Default: "/"
Path to the folder where file with the hashed consts will be output.
constName
Type: Array
Accepts an array of php constant names to be hashed
hashLength
Type: Number
Default: 6
Specifies the number of characters to be used in the hashed value provided to each constant
semVer - Experimental Only! This feature is in development
Type: Boolean
Default: false
"Semantic-ish" versioning. Turning this on will increment a 0-9 counter using a format matching define('CONST_NAME', '1.0.0');
LICENSE
MIT