From 123ecf3dae43e865e767b8482d8f33e99da942c9 Mon Sep 17 00:00:00 2001 From: Igor <35524806+igorschoester@users.noreply.github.com> Date: Mon, 19 Feb 2024 12:41:18 +0100 Subject: [PATCH] Copy block.json files over to root blocks folder For the block registration, we need access to the `block.json` file. Therefore, we need to ensure the path is the same in development and production (zip). This commit solves that by effectively making the root blocks folder the "dist" or "build" folder for our block.json files. When bundling, any `block.json` files found (within our entry scripts) are copied over to the root blocks folder. Keeping the folder structure in place from `packages/js/src` onwards. When creating an artifact, the blocks root folder is copied to the artifact folder. Note: the webpack copy currently has no support for any CSS or JS alongside, but the artifact copy ignore extensions already. --- .gitignore | 3 ++- config/grunt/task-config/copy.js | 1 + config/webpack/webpack.config.base.js | 3 ++- config/webpack/webpack.config.js | 14 +++++++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5e35a743844..08e774f3380 100644 --- a/.gitignore +++ b/.gitignore @@ -95,6 +95,7 @@ test.sh /js/dist/* /css/dist/* /packages/*/dist +/blocks/** yarn-error.log coverage dependencies.json @@ -108,4 +109,4 @@ dependencies.md ############ /.tmp -.cache/phpunit/cache/ \ No newline at end of file +.cache/phpunit/cache/ diff --git a/config/grunt/task-config/copy.js b/config/grunt/task-config/copy.js index c95a555944f..20e4c1dda95 100644 --- a/config/grunt/task-config/copy.js +++ b/config/grunt/task-config/copy.js @@ -28,6 +28,7 @@ module.exports = { "packages/js/images/**", "inc/**", "<%= paths.jsDist %>/**/*.js", + "blocks/**", "languages/**", "src/**", "lib/**", diff --git a/config/webpack/webpack.config.base.js b/config/webpack/webpack.config.base.js index 3c82c356d74..8e7e21df2b3 100644 --- a/config/webpack/webpack.config.base.js +++ b/config/webpack/webpack.config.base.js @@ -10,7 +10,7 @@ const { yoastExternals } = require( "./externals" ); let analyzerPort = 8888; -module.exports = function( { entry, output, combinedOutputFile, cssExtractFileName } ) { +module.exports = function( { entry, output, combinedOutputFile, cssExtractFileName, plugins = [] } ) { return { ...defaultConfig, optimization: { @@ -95,6 +95,7 @@ module.exports = function( { entry, output, combinedOutputFile, cssExtractFileNa // Copied from WP config: Inject the `SCRIPT_DEBUG` global, used for development features flagging. SCRIPT_DEBUG: process.env.NODE_ENV !== "production", } ), + ...plugins, ].filter( Boolean ), }; }; diff --git a/config/webpack/webpack.config.js b/config/webpack/webpack.config.js index c52cae71095..bc5011a2ee1 100644 --- a/config/webpack/webpack.config.js +++ b/config/webpack/webpack.config.js @@ -1,6 +1,7 @@ // External dependencies +const CopyWebpackPlugin = require( "copy-webpack-plugin" ); const { readdirSync } = require( "fs" ); -const { join } = require( "path" ); +const { join, resolve } = require( "path" ); // Variables const root = join( __dirname, "../../" ); @@ -29,6 +30,17 @@ module.exports = [ }, combinedOutputFile: root + "src/generated/assets/plugin.php", cssExtractFileName: "../../../css/dist/plugin-" + pluginVersionSlug + ".css", + plugins: [ + new CopyWebpackPlugin( { + patterns: [ + { + from: "**/block.json", + context: "packages/js/src", + to: resolve( "blocks" ), + }, + ], + } ), + ], } ), baseConfig(