Master @wordpress/scripts to automate your WordPress development. Learn to configure package.json and build modern JavaScript with Webpack and Babel.
Previously in this course, we covered Setting up the WordPress Development Environment, where we established our local workspace and plugin scaffolding. Now, we're ready to modernize our development workflow.
Writing raw, uncompiled JavaScript for WordPress is a recipe for maintenance headaches. As we prepare to build a React-based admin dashboard for our Knowledge Base plugin, we need a professional build pipeline. That is where @wordpress/scripts comes in.
In the modern WordPress ecosystem, we don't ship source code directly to the browser. Instead, we use tools to transpile modern JavaScript (ESNext/JSX) into browser-compatible code.
@wordpress/scripts is a collection of reusable scripts maintained by the WordPress core team. It abstracts away the complexity of configuring Webpack and Babel.
By using this package, you avoid the "configuration hell" that often plagues custom Webpack setups.
First, ensure you have Node.js installed. Open your terminal, navigate to your plugin directory, and initialize your package.json file:
Bashnpm init -y
Next, install the WordPress scripts package as a development dependency:
Bashnpm install @wordpress/scripts --save-dev
This command adds the necessary tools to your node_modules folder and updates your package.json. Now, open that file and update the scripts section to leverage the built-in commands:
JSON{ "scripts": { "build": "wp-scripts build", "start": "wp-scripts start" } }
To see this in action, create a folder named src in your plugin root and add an index.js file:
JAVASCRIPT// src/index.js const greeting = "Hello from the build pipeline!"; console.log(greeting);
Now, run the build command in your terminal:
Bashnpm run build
@wordpress/scripts looks for src/index.js by default and outputs a bundled file into build/index.js. If you inspect build/index.js, you'll see your code wrapped in a Webpack runtime environment. This build/index.js is the file you will eventually enqueue in WordPress using the Enqueuing scripts and styles the correct way in WordPress guidelines.
src folder called utils.js and export a simple function: export const log = (msg) => console.log('Log: ' + msg);.src/index.js and use it.npm run build again.build/ directory: Always add build/ to your .gitignore file. You should never commit your built assets to version control; only commit the source code.npm install --save-dev for build tools. If you accidentally install them as production dependencies, your plugin will be unnecessarily bloated when you package it for distribution.src/index.js file. Remember: the browser needs the compiled output in build/index.js.By mastering these tools, you're setting the stage for the more complex React development we'll tackle later in this course. You now have a repeatable, professional environment for compiling your assets.
Up next: Configuring ESLint and Prettier to ensure our code quality remains high as our codebase grows.
Learn to build interactive React forms in WordPress using controlled components. Master state management and submission events for your admin dashboard.
Read moreLearn how to scaffold your React admin dashboard by registering a WordPress menu, creating a root container, and mounting your application into the DOM.
Understanding WordPress Data Store Architecture
Registering a Custom Data Store
Writing Selectors for Data Access
Defining Actions and Reducers
Implementing Resolvers for Data Fetching
Optimizing Performance with Selectors
Handling Complex State Dependencies
Implementing Nonce Verification
Advanced Sanitization Techniques
Input Validation and Error Handling
Protecting Admin Screens
Production Build Pipeline
Debugging React in the WordPress Admin
Building Search and Filter Functionality
Internationalization in React
Managing File Uploads via REST API
Optimizing API Response Times
Working with Date and Time in React
Implementing Drag-and-Drop Sorting
Creating Custom Hooks for API Logic
Integrating with Gutenberg Blocks
Handling Conflict Resolution
Building a Modal Confirmation System
Implementing Activity Logging
Using Webpack Aliases
Unit Testing API Endpoints
Unit Testing React Components
Handling Large Datasets with GraphQL
Implementing Real-time Updates with Web