From 3b133729dc473e9cc023a3885aaa26c71e7b3fcf Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Fri, 7 Oct 2022 11:10:05 -0400 Subject: [PATCH] [Create Block] Allows custom keys to be generated in block.json files and package.json files (#44649) * Add any custom fields added to template defaults or variants to the block.json file. * Introduce customPackageJSON to template to allow customization of the resulting package.json file. * Update changelog. * Update changelog to include PR reference. * Provide a dedicated key for customBlockJSON. * Update changelog. * Add docs for new template properties. --- packages/create-block/CHANGELOG.md | 4 ++++ packages/create-block/docs/external-template.md | 2 ++ packages/create-block/lib/init-block.js | 2 ++ packages/create-block/lib/init-package-json.js | 2 ++ packages/create-block/lib/scaffold.js | 4 ++++ 5 files changed, 14 insertions(+) diff --git a/packages/create-block/CHANGELOG.md b/packages/create-block/CHANGELOG.md index b2ed3b07f182e..e638ddbb49c41 100644 --- a/packages/create-block/CHANGELOG.md +++ b/packages/create-block/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### New Feature + +- Add new `customPackageJSON` and `customBlockJSON` keys to allow templates to define custom keys for the resulting `package.json` and `block.json` files respectively.[#44649](https://github.com/WordPress/gutenberg/pull/44649) + ## 4.3.0 (2022-10-05) ## 4.2.0 (2022-09-21) diff --git a/packages/create-block/docs/external-template.md b/packages/create-block/docs/external-template.md index cb06530ee165a..5eac143a49f1a 100644 --- a/packages/create-block/docs/external-template.md +++ b/packages/create-block/docs/external-template.md @@ -74,6 +74,7 @@ The following configurable variables are used with the template files. Template - `customScripts` (default: {}) – the list of custom scripts to add to `package.json` . It also allows overriding default scripts. - `npmDependencies` (default: `[]`) – the list of remote npm packages to be installed in the project with [`npm install`](https://docs.npmjs.com/cli/v8/commands/npm-install) when `wpScripts` is enabled. - `npmDevDependencies` (default: `[]`) – the list of remote npm packages to be installed in the project with [`npm install --save-dev`](https://docs.npmjs.com/cli/v8/commands/npm-install) when `wpScripts` is enabled. +- `customPackageJSON` (no default) - allows definition of additional properties for the generated package.json file. **Plugin header fields** ([learn more](https://developer.wordpress.org/plugins/plugin-basics/header-requirements/)): @@ -101,3 +102,4 @@ The following configurable variables are used with the template files. Template - `editorScript` (default: `'file:./index.js'`) – an editor script definition. - `editorStyle` (default: `'file:./index.css'`) – an editor style definition. - `style` (default: `'file:./style-index.css'`) – a frontend and editor style definition. +- `customBlockJSON` (no default) - allows definition of additional properties for the generated block.json file. diff --git a/packages/create-block/lib/init-block.js b/packages/create-block/lib/init-block.js index f58008e7f5252..e8a321f85fed7 100644 --- a/packages/create-block/lib/init-block.js +++ b/packages/create-block/lib/init-block.js @@ -29,6 +29,7 @@ async function initBlockJSON( { editorScript, editorStyle, style, + customBlockJSON, } ) { info( '' ); info( 'Creating a "block.json" file.' ); @@ -56,6 +57,7 @@ async function initBlockJSON( { editorScript, editorStyle, style, + ...customBlockJSON, } ).filter( ( [ , value ] ) => !! value ) ), null, diff --git a/packages/create-block/lib/init-package-json.js b/packages/create-block/lib/init-package-json.js index 06bb170394d01..bbc4436ac1d0e 100644 --- a/packages/create-block/lib/init-package-json.js +++ b/packages/create-block/lib/init-package-json.js @@ -24,6 +24,7 @@ module.exports = async ( { npmDevDependencies, customScripts, isDynamicVariant, + customPackageJSON, } ) => { const cwd = join( process.cwd(), slug ); @@ -58,6 +59,7 @@ module.exports = async ( { ...( wpEnv && { env: 'wp-env' } ), ...customScripts, }, + ...customPackageJSON, } ).filter( ( [ , value ] ) => !! value ) ) ); diff --git a/packages/create-block/lib/scaffold.js b/packages/create-block/lib/scaffold.js index 5f603ed346633..6f57cdb85a393 100644 --- a/packages/create-block/lib/scaffold.js +++ b/packages/create-block/lib/scaffold.js @@ -44,6 +44,8 @@ module.exports = async ( editorStyle, style, variantVars, + customPackageJSON, + customBlockJSON, } ) => { slug = slug.toLowerCase(); @@ -99,6 +101,8 @@ module.exports = async ( editorScript, editorStyle, style, + customPackageJSON, + customBlockJSON, ...variantVars, };