Skip to content

Commit

Permalink
Scripts: Detect block.json changes from modules build (#57927)
Browse files Browse the repository at this point in the history
* Add BlockJsonDependenciesPlugin to modules build

Add a plugin that finds block.json files and adds them as
fileDependencies of the module build. This ensures that changes to
block.json files trigger updates to the module build.

See https://webpack.js.org/contribute/plugin-patterns/

> You may also feed new file paths into the watch graph to receive
> compilation triggers when those files change. Add valid file paths
> into the `compilation.fileDependencies` Set to add them to the
> watched files.
  • Loading branch information
sirreal authored Feb 7, 2024
1 parent de2a8a7 commit 5e683b7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Add experimental support for `viewScriptModule` field in block.json for `build` and `start` scripts ([#57437](https://github.com/WordPress/gutenberg/pull/57437)).

### Enhancements

- Ensure that watched module builds detect block.json changes ([#57927](https://github.com/WordPress/gutenberg/pull/57927)).

### Deprecations

- Experimental support for `viewModule` field in block.json is deprecated in favor of `viewScriptModule` ([#57437](https://github.com/WordPress/gutenberg/pull/57437)).
Expand Down
34 changes: 34 additions & 0 deletions packages/scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { basename, dirname, resolve } = require( 'path' );
const ReactRefreshWebpackPlugin = require( '@pmmmwh/react-refresh-webpack-plugin' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const { realpathSync } = require( 'fs' );
const { sync: glob } = require( 'fast-glob' );

/**
* WordPress dependencies
Expand All @@ -33,6 +34,7 @@ const {
getAsBooleanFromENV,
getBlockJsonModuleFields,
getBlockJsonScriptFields,
fromProjectRoot,
} = require( '../utils' );

const isProduction = process.env.NODE_ENV === 'production';
Expand Down Expand Up @@ -390,6 +392,37 @@ const scriptConfig = {
};

if ( hasExperimentalModulesFlag ) {
/**
* Add block.json files to compilation to ensure changes trigger rebuilds when watching
*/
class BlockJsonDependenciesPlugin {
constructor() {
/** @type {ReadonlyArray<string>} */
this.blockJsonFiles = glob( '**/block.json', {
absolute: true,
cwd: fromProjectRoot( getWordPressSrcDirectory() ),
} );
}

/**
* Apply the plugin
* @param {webpack.Compiler} compiler the compiler instance
* @return {void}
*/
apply( compiler ) {
if ( this.blockJsonFiles.length ) {
compiler.hooks.compilation.tap(
'BlockJsonDependenciesPlugin',
( compilation ) => {
compilation.fileDependencies.addAll(
this.blockJsonFiles
);
}
);
}
}
}

/** @type {webpack.Configuration} */
const moduleConfig = {
...baseConfig,
Expand Down Expand Up @@ -429,6 +462,7 @@ if ( hasExperimentalModulesFlag ) {
// generated, and the default externals set.
! process.env.WP_NO_EXTERNALS &&
new DependencyExtractionWebpackPlugin(),
new BlockJsonDependenciesPlugin(),
].filter( Boolean ),
};

Expand Down

0 comments on commit 5e683b7

Please sign in to comment.