-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Create Block] Allows custom keys to be generated in block.json files and package.json files #44649
Conversation
… resulting package.json file.
Size Change: 0 B Total Size: 1.27 MB ℹ️ View Unchanged
|
@ryanwelcher this is awesomesauce! I took it for a test spin and noticed an oddity. Here are my steps for testing (a bit different from yours):
I'm testing against my own custom block template (download
Once the block is output. I noticed that the
Perhaps my custom block template is not written properly? Or I'm missing something? |
I think it’s all good with your template, @colorful-tones. It looks like a side-effect of using the spread operator for fields passed from
How do you both feel about offering By the way, excellent job @ryanwelcher, taking care of the PR so fast! |
Makes sense about the spread operator. I would vote for |
Super excited about this PR. Agreed on |
Thanks for the feedback! I felt that the spread approach was a little naive so I agree that a dedicated key is probably the best approach. I'll add the |
@ryanwelcher Admittedly, this might need to be another ticket - but what would you think about adding support for |
Interesting idea! Templates hosted on npm are currently supported by create-block. Does that suit the need or were you hoping to be able to use a git repo for example? |
Ah, we can likely use the npm approach! I didn't realise that was even possible (and thought we'd need to get our users to download templates manually first etc). I'll check that out and open a new issue/PR if needed! |
Here is an older example I made and the usage looks like:
Feel free to reach out with any questions! |
@lgladdy, it might already work with GitHub URLs as it's supported by https://docs.npmjs.com/cli/v8/commands/npm-install Example: This is what we run behind the scenes: gutenberg/packages/create-block/lib/templates.js Lines 190 to 192 in 154c0ee
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryanwelcher I'm not sure of the use case for customPackageJSON
, but it could potentially be useful and I'm just blanking on an immediate use case.
I tested and the customBlockJSON
works wonderfully. Thanks for the great work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great one 👍 We still should document new options in the docs folder so it’s easier to discover.
One example is adding prettier config: |
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a bit of overlap with customScripts
, npmDependencies
, and npmDevDependencies
. It doesn’t have to be a bad thing as we would have to put special care to handle them in a similar way through customPackageJSON
. I’m mostly noticing here that this new option might override all other settings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point. I wonder if we just filter those keys out of customPackageJSON
before adding it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It isn’t a blocker. We can look into it once it starts to confuse people.
Although create-block isn't really part of WordPress 6.2, Gutenberg 14.4 is part of the Gutenberg plugins version that are said to arrive at WordPress 6.2, so it would be a great occasion to let developers know about this enhancement in a Dev Note for the Fieldguide of WP6.2 |
What?
This PR allows template authors to pass custom block.json properties by adding them to the
default
property or anyvariant
and introduces a new key calledcustomPackageJSON
that will add keys to the generated package.json file.Closes: #44626
Why?
Block creation via the create-block package has limited options that can be passed via a custom template. With the functionality introduced by this PR, it becomes more flexible both when new features are added to other packages ( see #44185 ) and especially for block developers who need/want to customize their process.
Testing Instructions
This can be tested with a custom template or by changing the built-in template information in the package.
Custom block.json keys
node ./index.js test-block
block.json
filenode ./index.js test-block --variant=static|dynamic
( use correct variant based on what you updatedblock.json
fileCustom package.json keys
customPackageJSON
key to the defaults with any key/value pairnode ./index.js test-block