@wordpress/scripts package, commonly referred to as wp-scripts, is a set of configuration files and scripts that primarily aims to standardize and simplify the development process of WordPress projects that require a JavaScript build step.
A JavaScript build step refers to the process of transforming, bundling, and optimizing JavaScript source code and related assets into a format suitable for production environments. These build steps often take modern JavaScript (ESNext and JSX) and convert it to a version compatible with most browsers.
If you use
@wordpress/create-block package to scaffold the structure of files needed to create and register a block, you’ll also get a modern JavaScript build setup (using wp-scripts) with no configuration required, so you don’t need to worry about installing wp-scripts or enqueuing assets.What wp-scripts Can Do
Here are a few things thatwp-scripts can do:
- Compilation: Converts modern JavaScript (ESNext and JSX) into code compatible with most browsers, using Babel
- Bundling: Uses webpack to combine multiple JavaScript files into a single bundle for better performance
- Code Linting: Provides configurations for ESLint to help ensure code quality and conformity to coding standards
- Code Formatting: Incorporates Prettier for automated code styling to maintain consistent code formatting across projects
- Sass Compilation: Converts Sass (.scss or .sass) files to standard CSS
- Code Minification: Reduces the size of the JavaScript code for production to ensure faster page loads
Node.js Requirement: This package requires Node.js version with long-term support status (Active LTS or Maintenance LTS). It is not compatible with older versions.
Quick Start
Installation
Ensure you have Node.js and
npm installed on your computer. Review the Node.js development environment guide if not.If you have not created a
package.json file before, navigate to the project folder in the terminal and run:An interactive prompt will walk you through the steps. When it asks for the “entry point”, enter
build/index.js.Basic Usage
Once installed, you can run the predefined scripts provided withwp-scripts by referencing them in the scripts section of your package.json file:
npm run {script name}.
The Build Process
The two scripts you will use most often arestart and build since they handle the build step.
Development Mode
When working on your project, use thenpm run start command:
build/index.js will not be optimized.
Production Mode
When you are ready to deploy your project, use thenpm run build command:
Build Output
After the build finishes, you will see:build/index.js- The compiled JavaScript filebuild/index.asset.php- Contains an array of dependencies and a version number (for cache busting)
*.asset.php file is essential for properly enqueuing your scripts with WordPress. Without the wp-scripts build process, you’ll need to manually create these dependency files.
Available Scripts
Here’s a comprehensive example demonstrating most of the capabilities included:Build Options
Thebuild and start commands support various options:
Start Options
Thestart command has additional development-specific options:
Maintaining Code Quality
To help developers improve the quality of their code,wp-scripts comes pre-configured with tools like ESLint and Prettier:
Running Tests
Beyond just writing code, verifying its functionality is crucial.wp-scripts includes Jest, a JavaScript testing framework, and both end-to-end and unit testing scripts:
Enqueuing Assets
If you register a block viaregister_block_type the scripts defined in block.json will be automatically enqueued.
To manually enqueue files in the editor, in any other context, here’s a typical implementation:
Entry Points
Listing Entry Points
The simplest way to list JavaScript entry points is to pass them as arguments for the command:entry-one.js and entry-two.js in the project’s root and output the generated files into the build directory.
Automatic block.json Detection
A convenient alternative for blocks is using automatic entry point detection. The source code directory (default is./src) and its subdirectories are scanned for the existence of block.json files. If one or more are found, the JavaScript files listed in metadata are treated as entry points.
Example block.json:
Fallback Entry Point
The fallback entry point issrc/index.js (other supported extensions: .jsx, .ts, and .tsx) in case there is no block.json file found. In that scenario, the output generated will be written to build/index.js.
Importing Styles in JavaScript
You can import CSS, SCSS, and SASS files directly in your JavaScript:index.css- All imported CSS files are bundled into one chunk named after the entry point (editor only)style-index.css- Importedstyle.cssfile(s) get bundled into one file meant for both front-end and editor
.module to the extension, e.g. style.module.scss.
Using Assets
Fonts and Images
It is possible to reference font (woff, woff2, eot, ttf, otf) and image (bmp, png, jpg, jpeg, gif, webp) files from CSS:
SVG Files
You can import SVG files as React components:Advanced Configuration
Whilewp-scripts provides a solid default configuration, there might be cases where you need more specialized setups. The good news is wp-scripts is highly adaptable.
Default Webpack Config
@wordpress/scripts bundles the default webpack config used as a base by the WordPress editor:
- Entry: Detected by scanning
block.jsonfiles in thesrcdirectory. Fallback:src/index.js - Output:
build/[name].js - Loaders: babel-loader, @svgr/webpack, url-loader, css-loader, postcss-loader, sass-loader
- Plugins: CopyWebpackPlugin, MiniCssExtractPlugin, DependencyExtractionWebpackPlugin
Provide Your Own Webpack Config
You can provide your own webpack config when:- The command receives a
--configargument:wp-scripts build --config my-own-webpack-config.js - There is a file called
webpack.config.jsorwebpack.config.babel.jsin the top-level directory
Extending the Webpack Config
To extend the provided webpack config, you can create your ownwebpack.config.js file: