Skip to content

Commit 6f58f32

Browse files
dd32ryelle
andauthored
Build: Append the hash of the build to block.json for CSS cache busting purposes. (#580)
* Append the hash of the build to block.json for cache busting purposes. * Whitespace standardisation. * Use `glob` to find all `block.json` files in each project * Only run the version replacement over files in build --------- Co-authored-by: Kelly Dwan <ryelle@users.noreply.github.com>
1 parent 43dc4ec commit 6f58f32

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

bin/build.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
const chalk = require( 'chalk' );
77
const fs = require( 'fs' ); // eslint-disable-line id-length
8+
const { sync: glob } = require( 'fast-glob' );
9+
const { hashElement } = require( 'folder-hash' );
810
const path = require( 'path' );
911
const postcss = require( 'postcss' );
1012
const rtlcss = require( 'rtlcss' );
@@ -88,6 +90,43 @@ async function maybeBuildPostCSS( inputDir, outputDir ) {
8890
}
8991
}
9092

93+
/**
94+
* Update the block.json version field with the hash of the build.
95+
*
96+
* @param {string} basePath
97+
*/
98+
async function setBlockVersion( basePath ) {
99+
const project = path.basename( basePath );
100+
101+
// Find any block.json files under the project's `build` dir.
102+
const files = glob( '**/build/**/block.json', {
103+
absolute: true,
104+
cwd: basePath,
105+
} );
106+
107+
if ( ! files.length ) {
108+
console.log( chalk.red( `Couldn't find block.json for ${ project }` ) );
109+
return;
110+
}
111+
112+
const options = {
113+
algo: 'sha1',
114+
encoding: 'hex',
115+
};
116+
117+
const hash = await hashElement( basePath, options );
118+
119+
files.forEach( ( blockJson ) => {
120+
const blockJsonContents = require( blockJson );
121+
blockJsonContents.version = blockJsonContents.version?.replace( /(^|-)[0-9a-f]{40}$/, '' ) || '';
122+
blockJsonContents.version += ( blockJsonContents.version ? '-' : '' ) + hash.hash;
123+
124+
fs.writeFileSync( blockJson, JSON.stringify( blockJsonContents, null, '\t' ) );
125+
} );
126+
127+
console.log( chalk.green( `block.json version set for ${ project } to ${ hash.hash }` ) );
128+
}
129+
91130
// If we have more paths that need building, we could switch this to an array.
92131
const projectPath = path.join( path.dirname( __dirname ), 'mu-plugins/blocks' );
93132
const cliProjects = process.argv.slice( 2 );
@@ -115,6 +154,8 @@ projects.forEach( async ( file ) => {
115154
await maybeBuildBlock( path.resolve( path.join( basePath, 'src' ) ), outputDir );
116155

117156
await maybeBuildPostCSS( path.resolve( path.join( basePath, 'postcss' ) ), outputDir );
157+
158+
await setBlockVersion( basePath );
118159
} catch ( error ) {
119160
console.log( chalk.red( `Error in ${ file }:` ), error.message );
120161
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
},
6565
"dependencies": {
6666
"@googlemaps/markerclusterer": "^2.4.0",
67+
"fast-glob": "^3.3.2",
68+
"folder-hash": "^4.0.4",
6769
"google-map-react": "^2.2.1",
6870
"locutus": "^2.0.16",
6971
"lodash.debounce": "^4.0.8",

yarn.lock

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4340,9 +4340,9 @@ debug@2.6.9, debug@^2.6.9:
43404340
dependencies:
43414341
ms "2.0.0"
43424342

4343-
debug@4, debug@4.3.4, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4:
4343+
debug@4, debug@4.3.4, debug@^4.1.0, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.3, debug@^4.3.4:
43444344
version "4.3.4"
4345-
resolved "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz"
4345+
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
43464346
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
43474347
dependencies:
43484348
ms "2.1.2"
@@ -5416,6 +5416,14 @@ flatted@^3.2.9:
54165416
resolved "https://registry.npmjs.org/flatted/-/flatted-3.2.9.tgz"
54175417
integrity sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==
54185418

5419+
folder-hash@^4.0.4:
5420+
version "4.0.4"
5421+
resolved "https://registry.yarnpkg.com/folder-hash/-/folder-hash-4.0.4.tgz#f5ffa14e9fc6f3213598f6a367207adf7d419edc"
5422+
integrity sha512-zEyYH+UsHEfJJcCRSf9ai5I4CTZwZ8ObONRuEI5hcEmJY5pS0FUWKruX9mMnYJrgC7MlPFDYnGsK1R+WFYjLlQ==
5423+
dependencies:
5424+
debug "^4.3.3"
5425+
minimatch "~5.1.2"
5426+
54195427
follow-redirects@^1.0.0, follow-redirects@^1.15.4:
54205428
version "1.15.5"
54215429
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz"
@@ -7660,6 +7668,13 @@ minimatch@~3.0.5:
76607668
dependencies:
76617669
brace-expansion "^1.1.7"
76627670

7671+
minimatch@~5.1.2:
7672+
version "5.1.6"
7673+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.6.tgz#1cfcb8cf5522ea69952cd2af95ae09477f122a96"
7674+
integrity sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g==
7675+
dependencies:
7676+
brace-expansion "^2.0.1"
7677+
76637678
minimist-options@4.1.0:
76647679
version "4.1.0"
76657680
resolved "https://registry.npmjs.org/minimist-options/-/minimist-options-4.1.0.tgz"

0 commit comments

Comments
 (0)