Modern JavaScript
The Block Editor uses modern JavaScript features including:- ES6+ Syntax: Arrow functions, destructuring, modules, classes
- JSX: JavaScript XML syntax for React components
- Modules: Import/export statements for code organization
- Async/Await: Modern asynchronous programming patterns
Example of modern JavaScript
JavaScript build process
Modern JavaScript needs to be compiled (transpiled and bundled) before browsers can execute it. WordPress provides@wordpress/scripts to handle this build process.
Why we need a build process
1
Transpilation
Convert modern JavaScript (ES6+) to code compatible with older browsers
2
JSX transformation
Convert JSX syntax to standard JavaScript function calls
3
Module bundling
Combine multiple files into optimized bundles
4
Asset optimization
Minify code, optimize imports, generate source maps
Source vs build directories
Setting up the build process
Install @wordpress/scripts
Add build scripts to package.json
Build commands
- Production build
- Development mode
build/ directory for production.What wp-scripts does
@wordpress/scripts handles:
JavaScript transformation
JSX to JavaScript
CSS/SCSS processing
Dependency management
Generatesindex.asset.php with WordPress dependencies:
This file is automatically generated and should not be edited manually.
WordPress packages
The Block Editor provides numerous packages for block development:Core packages
Package categories
- @wordpress/blocks: Block registration and utilities
- @wordpress/block-editor: Block editing components and hooks
- @wordpress/components: Reusable UI components
- @wordpress/data: State management (Redux-like stores)
- @wordpress/element: React abstraction layer
- @wordpress/i18n: Internationalization functions
- @wordpress/compose: Higher-order components and hooks
- @wordpress/api-fetch: WordPress REST API wrapper
Module imports and exports
Exporting from modules
Importing modules
React and JSX
The Block Editor uses React for building user interfaces:React components
React Hooks
Debugging
Development mode
Runnpm start for development builds with:
- Source maps for debugging
- Readable code
- Hot reloading
Browser DevTools
Use React DevTools browser extension to inspect component hierarchy and props.Console logging
TypeScript support
@wordpress/scripts has built-in TypeScript support:
Next steps
wp-scripts
Learn more about the build tool
Creating blocks
Start building your first block