Replies: 4 comments 10 replies
-
Thanks for this @g-elwell! I would be open to discussion on changing our approach on this. I believe the main reason we don't import the libraries is (as you stated) to not import packages that are already available but (again as you've stated) as there is a way around that it could work. Along with all the dev benefits, 'automatically generating a dependency array' sounds like it would solve a few areas where some of our juniors have tripped up on including the right dependancies for their scripts. I would be curious to know if the packages are kept as up to date as the ones available to us from WordPress itself. |
Beta Was this translation helpful? Give feedback.
-
thanks for the info, @g-elwell 👍 i have two concerns:
|
Beta Was this translation helpful? Give feedback.
-
Finally getting round to responding to this but it seems like @jonmcp has covered my concerns about this anyway. I am however not overly concerned about mismatching version as we've seen this in use in various in-production plugins and blocks. It's also used this way with Jest tests when it comes to Gutenberg/WordPress functions, etc. Of course that might mean we've just not encountered issues yet but it still leaves me with a lack of concern over that. It's also worth adding that not even Automattic have this included in their own build scripts for Gutenberg plugins/assets from what I can understand. I've no idea what the reason is for this but I'm at least willing to explore the possibility of allowing this within our own tools. So I'm going to suggest that we add the necessary
This may actually be something we can integrate without the need for the |
Beta Was this translation helpful? Give feedback.
-
I've created an issue from this (#9) and pencilled it in for the |
Beta Was this translation helpful? Give feedback.
-
If you're working on a project and want to use the JavaScript APIs and libraries provided by WordPress, there are two options available:
const { ToggleControl } = wp.components
)import { ToggleControl } from '@wordpress/components
)I was curious about the difference between these and wanted to know which one is the recommended approach. I couldn't find a clear answer, but while looking into it I discovered some interesting info that might be relevant to these build tools.
Destructuring is quick - you don't need to manually install a package and when developing for WordPress you can be certain that the global wp variable is going to be available when your code will run. However, because your code editor isn't aware of the wp global it can't provide you with any hinting or suggestions when you use it.
If you decide to install the packages you need using npm, your code editor can read their contents in order to provide some helpful tooling. In VSCode without any additional config you get suggestions, autocomplete and inline documentation about available imports, function params and component props as well as automatic import suggestions.
As a side note you end up recording which version of each package you are using in your project which might also be useful.
The downside to this approach is that you're importing packages that are already available to you in WordPress and increasing your bundle size needlessly. However, by listing these packages as externals in webpack you can prevent them from being added to your final bundle whilst still benefitting from the improved dev experience.
WordPress actually provide @wordpress/dependency-extraction-webpack-plugin which defines all the modern WordPress packages and libraries as externals in webpack. Also, this plugin will generate a php file for each of your entrypoints containing a dependency array that you can import when you enqueue your script, removing the need to manually maintain an array of dependencies for each of your scripts.
In terms of these build tools, on a basic level adding the
@wordpress/*
packages to the webpack config as externals should be fairly straightforward and would allow you to choose which approach you want to take when it comes to using them in your project.Beta Was this translation helpful? Give feedback.
All reactions