When developing a block with @wordpress/scripts, wp-scripts uses @wordpress/dependency-extraction-webpack-plugin to allow you to import dependencies from WordPress by replacing the import statements with references to the global variables loaded by the enqueued scripts.
For example, this:
import { useEffect } from '@wordpress/element'
Will become:
const { useEffect } = wp.element;
And wp-element will be added as a dependency to the enqueued script to ensure that wp.element is available.
This process works when importing the modules listed in the documentation for the dependency extraction plugin:
@wordpress/*jquerylodash-eslodashmomentreact-domreact
For any other bundled libraries you would need to enqueue them as a dependency manually and reference the global variable as you would if you were using the library anywhere else.
Related Posts:
- Masonry gallery block is working in the block editor but not the template editor
- How to re-render Gutenberg component when object instance is available
- How to add more elements to an already existing section in Gutenberg
- Working with a non-React external library in a custom block
- How to build BOTH non-block components and blocks present in the /src directory using @wordpress/scripts
- How to add an additional dependency to a block index.asset.php file
- Why is the Gutenberg editor not recognizing my updates?
- How to transform a shortcode into a block
- useSelect() plus resolver result is serving cached data incorrectly
- Gutenberg DatePicker component with time set to zero
- How to render HTML content using the Interactivity API?
- How to auto rename JS files to prevent browser cache issues
- How to render initial posts on page load in a Gutenberg block using the Interactivity API?
- How to include block style variations for blocks in posts dynamically rendered via the Interactivity API
- Gutenberg – remove / add blocks with custom script
- What are all the query parameters for getEntityRecords?
- How to use wp.hooks.addAction() in React JS/Gutenberg?
- How to only enqueue block javascript on the frontend when its needed [duplicate]
- Extend core block in Gutenberg
- Gutenberg custom block plugin with custom image sizes
- Get loading state of wp data selector
- Hooking a callback into the code within a Gutenberg block
- conditional layout based on if Innerblocks is not empty
- Gutenberg extend blocks add new class name
- initial open/toggle PluginDocumentSettingPanel panel in document setting block editor gutenberg
- Correctly handling WordPress core data retrieval in Gutenberg
- Is there a client side API for handling transients or options?
- Adding a text element in between title and blocks container
- Gutenberg RichText
- Use useSelect/useDispatch instead of withSelect/withDispatch
- How to handle Gutenberg wp.data async errors?
- How to use wp.hooks.addAction() in React JS/Gutenberg?
- Load script after block is inserted
- How to get value of selected page template in Gutenberg editor?
- How do I access site and block editor state data and use `useSelect()` or `withSelect()` to bind it to my components?
- @wordpress/components Button variants not styled
- Block Editor: How to get title and post content within the WordPress admin UI in Javascript?
- Custom block SelectControl works but equivalent ComboboxControl gives errors – where’s my mistake?
- Gutenberg add extra attributes to custom format
- event/callback on block update?
- can a buttongroup have a label?
- How to disable inline css styles generated by Gutenberg editor?
- Gutenberg getMedia() in post query doesn’t return all featured images
- InnerBlocks restricting does not work on nested blocks
- How to transform a legacy widget into a block
- Add A Tag To A Post Through JavaScript In Gutenberg UI?
- Gutenberg select categories
- How to return div with custom attributes(Coding a Gutenberg Block)
- What is the correct way to import the blocks-editor?
- Open MediaUpload from external component in Gutenberg
- Get Block Attributes in JSON REST API
- Open Featured Image Modal in WordPress Gutenberg onClick of a button
- Gutenberg extend blocks add new class name
- Why isn’t wp_enqueue_script(‘jquery-masonry’) working?
- Using apiFetch for retrieving post data in Gutenberg
- Looping over wordpress meta to create “ ‘s?
- How to display post content in the block editor
- How to load an additional script for a block in the block editor?
- what is the purpose of the namespace argument when customizing blocks?
- Gutenberg consume wp-json data and reflect in frontend the content
- How can I import one custom block into another?
- Gutenberg Blocks: how to determine an index of the current inner block?
- Get terms of a taxonomy using useSelect
- Conditional save return on Gutenberg Block
- Gutenberg select excerpt, use generated excerpt or use more block excerpt
- Gutenberg dependencies in package.json
- find out reason of “Updating failed” in Post-editor
- Gutenberg RangeControl
- Custom Gutenberg block: access dom element via JavaScript
- Problem extending a core block
- Cannot read properties of undefined (reading ‘show_ui’) Error on WordPress Post Editor
- Add a Page Screen is Visually Blank
- Including dependencies using @wordpress/dependency-extraction-webpack-plugin
- Gutenberg sidebar show input field on toggle
- Get the ID of a page in Parent combobox in editor
- Uncaught TypeError: r is not a function
- Issue migrating a checkbox-type meta field to the block editor
- Update block once an API request returns with a value
- How do I make a savable preview like Youtube Gutenberg block?
- iFrame onLoad in custom Gutenberg block
- WordPress Gutenberg: Attribute overwritten by block duplicate
- Table block variation with header section activated
- Filtering Gutenberg Components, not Blocks
- Where to include JS library in gutenberg blocks
- How to delete child block for associated parent block
- Passing object to FormTokenField suggestions
- Is it possible to add javascript to template parts
- How to add a new attribute to core wp block editor without npm?
- How do i get an Inline style in Gutenberg Block show up in front end?
- WordPress adds and tags into HTML blocks after saving
- How do i fix type error
- Gutenberg core/file add style support in js/ json
- Create Youtube embed block with createBlock
- How to change the selected Template using javascript?
- config @wordpress/scripts with webpack and postCSS features
- Jest Unit tests for High order component containing Block Editor
- useBlockProps() nests wrapper with class name inside block wrapper in the editor
- Metabox conditionals depending on post format and template in Gutenberg
- How to access current discussion settings in gutenberg via JS?
- How to get selected category objects in the block editor?