-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with jest caching of block.json files (#16899)
* Generate cache key for block index files by concatenating the index with the block json file * Move implementation to the test/unit folder
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const fs = require( 'fs' ); | ||
const babelJest = require( 'babel-jest' ); | ||
const babelJestTransformer = babelJest.createTransformer(); | ||
|
||
module.exports = { | ||
// This transformer extends the babel-jest transformer. | ||
...babelJestTransformer, | ||
|
||
/** | ||
* A cache key generator that extends babel-jest's cache key generator, | ||
* but adds some additional handling for invalidating the cache when a | ||
* change to a block.json file is made. | ||
* | ||
* @param {string} src The source of the file being transformed. | ||
* @param {string} filename The filename of the file being transformed. | ||
* @param {...any} args Any other args passed to the function. | ||
* | ||
* @return {string} The cache key for the file. | ||
*/ | ||
getCacheKey( src, filename, ...args ) { | ||
const isBlockIndex = /block-library[\/\\]src[\/\\].+[\/\\]index\.js/.test( filename ); | ||
|
||
if ( ! isBlockIndex ) { | ||
return babelJestTransformer.getCacheKey( src, filename, ...args ); | ||
} | ||
|
||
const blockJSONFilename = filename.replace( 'index.js', 'block.json' ); | ||
|
||
if ( ! fs.existsSync( blockJSONFilename ) ) { | ||
return babelJestTransformer.getCacheKey( src, filename, ...args ); | ||
} | ||
|
||
// If the file is a block index file and there's a block json file, generate the | ||
// jest cache key for this file by concatenating the block index and block json | ||
// src together. This will result in the cache key changing and the cache being | ||
// invalidated for the index when any changes to the json are made. | ||
const blockJSONSrc = fs.readFileSync( blockJSONFilename ); | ||
return babelJestTransformer.getCacheKey( `${ src }\n${ blockJSONSrc }`, filename, ...args ); | ||
}, | ||
}; |