-
Notifications
You must be signed in to change notification settings - Fork 0
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
#93 React Init WordPress Block Changes #153
Conversation
The Block ItselfThis will ultimately replace the WebpackI will defer to you on what we should use for minimal WP friction. Originally we discussed Vite as it is our default for JS projects. If we're using Webpack, we should move the Tailwind config into Webpack as well. No need to have 2 bundlers. Red UnderlinesThis is just installing the appropriate It does look like these packages are either not able to properly infer types for TypeScript. Installing the appropriate packages resolves the errors you were seeing, but creates a couple of new ones like Dynamic Block / Server Side RenderingI think I understand what you're saying. Essentially, all of the React app is conditionally rendering things based on user state and auction state. This can't be saved since it will change based on changing user and auction state, so we have to make everything dynamic. Dist folderWe can change that. All that is doing is making sure we have a clean directory, but that is only affecting local dev. Swapping everything over to Webpack would also work. CSSWe shouldn't need unique CSS for this block (ideally), just the Tailwind config that Nathan's added with Vite. Formatting and LintingWe can move all of that into the WP scripts versions. |
@@ -0,0 +1,30 @@ | |||
/** |
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.
@bd-viget How hard of a requirement is it for save.tsx
and edit.tsx
to match? For example, while we are in the editor, we don't really want or need make a websocket connection, but we definitely need that on the page visible to the user.
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.
I think as long as you load additional components conditionally (such as where the <ul>
is getting included) and they load other React Components, I don't think Gutenberg makes a fuss, but it's been a while since I've worked with custom blocks, so I hope you don't end up in a trial/error situation. If that happens and the block keeps breaking, maybe get to a point where you like and I can try to see if I can make Gutenberg happy :D.
Also, just to clarify, the files themselves don't need to match, just the returned markup can't be like:
save.tsx
<div>This is my block.</div>
edit.tsx
<p><span>This is something completely different.</span></p>
(this may not be the best of examples, sorry!)
I'll be honest, I don't quite understand the difference in Webpack and Vite as far as how 1 functions different from the other. Is it like a preference thing, like choosing
Works for me! Whatever makes sense as far as consolidation to streamline this I'm totally good with.
Ok, so does this mean we need to put those in
Yeah I was already seeing that yellow underline there, but again, it compiles so not terribly worried about that.
Exactly, and if you want to use one of the core blocks as an guide/example, you can find them here. (Maybe check out the Comments block?)
👍
👍 If you know how to do that, I'm all for 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.
@nick-telsan Crap! Sorry, this file was not supposed to be committed!
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.
Removed it in #154!
Webpack tends to just be harder to work with than Vite. Most of our Ruby projects or standalone JS projects benefit from Vite over Webpack. But, in this case, Vite has been harder to finesse into WordPress. I think we're good to just run with wp-scripts and webpack approach since those work well with WordPress. For those outstanding issues, what I want to do is merge these PRs into #115 and just clean up our PRs a bit. We can leave Vite, formatting, and linting as they are for a minute while we get everything stable, and then knock those out. I'm open to either or b) merging in #115 with Vite serving Tailwind and then adjusting that in another PR. |
* [#93] React Init - Adds prettier - Adds lint-staged and husky - Adds bin scripts - Adds initial react app * [#93] React Init WordPress Block Changes (#153) - Custom React Block for Bidding - Adds packages for WP Script blocks - Adjust TS Config * [#94] Metrics but WP (#155) - Adds driver and metrics component - Adjusts Tailwind content paths - Adds inputs for new components - Adds script to handle components in dev mode - Moves metrics into the WP block * [#95] Countdown Timer (#148) - Adds countdown timer component and functions - Adjusts initial state for demo - Adjusts font styles - Fixes gitignore build * [#96] Bid Button (#149) - Adds clsx - Adds bid button * [#97] Free Bids (#150) - Adds free bids button - Adds demo data * [#100] Participation (#151) - Adds participation component * [#101] Free Bids (#152) - Adds free bids promo - Adds temporary hand icon * [#140] Websocket Connection (#167) - Installs zod and zustand - Adds attributes to block.json - Adds auction store - Adds types - Changes initial state to data attributes - Adjusts use of data attributes and useAuction - Adds websocket connection - Hardcodes WS URL for now.
* [#93] React Init - Adds prettier - Adds lint-staged and husky - Adds bin scripts - Adds initial react app * [#93] React Init WordPress Block Changes (#153) - Custom React Block for Bidding - Adds packages for WP Script blocks - Adjust TS Config * [#94] Metrics but WP (#155) - Adds driver and metrics component - Adjusts Tailwind content paths - Adds inputs for new components - Adds script to handle components in dev mode - Moves metrics into the WP block * [#95] Countdown Timer (#148) - Adds countdown timer component and functions - Adjusts initial state for demo - Adjusts font styles - Fixes gitignore build * [#96] Bid Button (#149) - Adds clsx - Adds bid button * [#97] Free Bids (#150) - Adds free bids button - Adds demo data * [#100] Participation (#151) - Adds participation component * [#101] Free Bids (#152) - Adds free bids promo - Adds temporary hand icon * [#140] Websocket Connection (#167) - Installs zod and zustand - Adds attributes to block.json - Adds auction store - Adds types - Changes initial state to data attributes - Adjusts use of data attributes and useAuction - Adds websocket connection - Hardcodes WS URL for now.
* [#93] React Init - Adds prettier - Adds lint-staged and husky - Adds bin scripts - Adds initial react app * [#93] React Init WordPress Block Changes (#153) - Custom React Block for Bidding - Adds packages for WP Script blocks - Adjust TS Config * [#94] Metrics but WP (#155) - Adds driver and metrics component - Adjusts Tailwind content paths - Adds inputs for new components - Adds script to handle components in dev mode - Moves metrics into the WP block * [#95] Countdown Timer (#148) - Adds countdown timer component and functions - Adjusts initial state for demo - Adjusts font styles - Fixes gitignore build * [#96] Bid Button (#149) - Adds clsx - Adds bid button * [#97] Free Bids (#150) - Adds free bids button - Adds demo data * [#100] Participation (#151) - Adds participation component * [#101] Free Bids (#152) - Adds free bids promo - Adds temporary hand icon * [#140] Websocket Connection (#167) - Installs zod and zustand - Adds attributes to block.json - Adds auction store - Adds types - Changes initial state to data attributes - Adjusts use of data attributes and useAuction - Adds websocket connection - Hardcodes WS URL for now.
Summary
This PR makes some adjustments to the Typescript files and structure to use
wp-scripts
to build the JS asset files for the custom Bidding Block.@nick-telsan I wasn't 100% sure the purpose of this block (is it supposed to replace the Bid Now block, or accompany it, or does this render other stuff, or is this the Node service element... I have no idea). I left the final output relatively similar, but removed the placeholder attributes for now with the assumption we will retrieve these another way.
Issues
Notes
edit.tsx
,index.tsx
, andsave.tsx
files and I'm not really sure how to resolve them (if possible).TS2307: Cannot find module @wordpress/[module-name] or its corresponding type declarations.
block.json
:TS2732: Cannot find module ./block.json. Consider using --resolveJsonModule to import module with .json extension.
- I don't know what that means, but everything is working just fine.save.tsx
:Void function return value is used
- It still compiles correctly, so not sure if it's a big deal.Edit
property HTML/Markup is supposed to match theSave
property, and if they don't match up, what you end up with is an error in the block that says the block contains invalid markup. To avoid this, I think the trick is to make as many elements as dynamic as possible. (You may not run into this issue, but if you do, it may require making some adjustments or using another component).<div id= data-xx=
element type initializer approach isn't really going to work. The JS relies on the element to be present in order to retrieve the Auction ID, and that just ends up not working on the front-end or causes the markup to be different. To workaround this, I gave the block an attribute ofauctionId
, and am loading it using the WordPressuseSelect
module to grab the Current Post ID. From there, the rest of the data can be populated dynamically, however, I wish it was as simple as requesting some meta using the Post ID. Some of the values may be obtainable that way, but most of them are calculated with ourgoodbids()->auctions...
API, so what we will probably need to do is go ahead and start setting up some custom WordPress REST API endpoints to retrieve this information (or maybe we can do it in 1 endpoint). /cc @clatwellnpm run format:fix
, so sorry if it adds a lot of noise/changes, but I couldn't even commit these files without those updates.block.json
and add a file inside theblocks/block-name
folder.dist
folder gets emptied (due toemptyOutDir: true
), everything goes into thebuild
directory. This is probably not ideal, however, I ran into some blockers here and wanted to get your feedback before doing anything else.emptyOutDir
so all the files can go in the same folder, but that may be all we need to do.wp-scripts
uses webpack, we're not usingvite
to compile the custom React block, but if you know of a way to make this work, by all means feel free to change it.webpack.config.cjs
andtsconfig.json
to use"outDir": "./blocks/react"
, however, that doesn't appear to be working (the files are inbuild/blocks
). If you know how to configure this properly, it seemed to make sense to include these blocks with the rest of the blocks, however, if it just isn't right at all, I guess we just remove those lines?tsconfig
JSON files, I was just following the instructions I found to get Typescript working withwp-scripts
, but if you look at it and know what's going on and think we don't need them, I trust your judgment on removing/changing those values.npm run build
will compile everything.npm run start
will putwp-scripts
in dev mode, however frequent changes to the block could result in a "block contains invalid markup" type error (mentioned before), and may require you to either remove the block and re-add it, or runnpm run build
. Again, not my area of expertise.registerBlockType()
insrc/blocks/bidding/index.tsx
, however, I'm also callingregister_block_type()
insrc/classes/Blocks/Blocks.php
. I don't think the PHP one is doing much of anything, but theallowed_block_types_filter
appears to be the only way to restrict a block to a particular post type (although I'm doing it wrong anyway). You may find a few things like this in here, but once I got it working I was too afraid to change stuff, so figured we could clean it up later.wp-prettier
although I didn't implement it, however, I was curious if we should be using it since we're in a WordPress plugin? I didn't look too much into this other than discovering it exists.blocks
array via theconfig.json
file which I haven't really been utilizing much, but need to.I think that's about all the notes I can detail out for this. If something isn't working or making sense, hit me up and we can go over it! Thanks for your patience!!