78
loading...
This website collects cookies to deliver better user experience
npm install @wordpress/scripts –save-dev
package.json
file. At minimum, you’re going to need build
and start
. Your JSON would look something like this:{
"name": "plugin_name_replace_me",
"version": "1.0.0",
"description": "Plugin Description Replace Me",
"scripts": {
"build": "wp-scripts build",
"start": "wp-scripts start"
},
"devDependencies": {
"@wordpress/scripts": "15.0"
},
"author": "",
"license": "ISC"
}
package.json
used in the Underpin plugin boilerplate, but it would work in just about any plugin or theme. The key part is the scripts
object:"scripts": {
"build": "wp-scripts build",
"start": "wp-scripts start"
}
scripts
object may have additional scripts, and that’s fine. All this does is register command line scripts that can be ran inside the directory that contains this package.json
file.package.json
is all-set, it’s time to create your Javascript file. This file is going to be what @wordpress/scripts uses to create the actual Javascript file that the site will be used by your browser. By default,./src/index.js
, but this can be customized to be something else if-needed. More on that later.npm run start
it will actually run the start
command inside the @wordpress/scripts package. Conversely, if you run npm run build
, it will run the build
command inside the @wordpress/scripts package. There are a handful of other useful commands, such as linters and translation compilation built into this command, but we’re not going to cover those in this post. You can see them in the @wordpress/scripts documentation.build
and start
will compile your Javascript and your CSS/SCSS into something all web browsers can read, but each one does this a little differently.npm run start
will create an un-minified version of your script, and include a map file so that you can easily debug your scripts. Without this map file, you’d get vague errors that point to the wrong file because the browser wouldn’t otherwise know where these errors are.start
is ran, it will continue to run in the background, and automatically regenerate your scripts and styles when the files are changed. This is perfect for when you’re still building your scripts and styles, since it runs quietly in the background, and automatically regenerates everything for you.start
‘s priority is to help you while you’re developing, but because of this your script files will be way bigger than you’d want them to be on your live site (we’re talking megabytes people_, megabytes!_). This is where build
comes in.npm run build
will create the absolute smallest file sizes it can generate by minifying scripts, and optimizing their contents. This is ideal for when you’re ready to use these themes on a live site (production). Unlike start
, this command will generate your scripts and styles one time, instead of running in the background. The scripts and styles generated by build will look garbled. With variable names minified, and everything compressed to a single line of code, so you don’t want to use this when developing. Instead, you’ll want to run this as a step in your deployment process.build
or watch
, a compiled version of your script will be located inside the build directory.head
tag. In WordPress this is done byplugin_name()->scripts()->add( 'test', [
'handle' => 'test',
'src' => plugin_name()->js_url() . 'index.js',
'name' => 'test',
'description' => 'The description',
'middlewares' => [
'Underpin_Scripts\Factories\Enqueue_Script'
]
] );
test
, where the JS URL is build/index.js
. Underpin’s js_url()
method defaults to the build
in your plugin or theme.Enqueue_Script
middleware. You can learn more about how script middleware works here.wp_register_script( 'test', plugin_dir_url( __FILE__ ) . 'build/index.js' );
add_action( 'wp_enqueue_scripts', function(){
wp_enqueue_script( 'test' );
} );
build
or watch
, and when it’s all set-up it works seamlessly. You won’t even notice that these scripts are not included in your file.plugin_name()->scripts()->add( 'test', [
'handle' => 'test',
'src' => plugin_name()->js_url() . 'index.js',
'name' => 'test',
'description' => 'The description',
'deps' => plugin_name()->dir() . 'build/index.asset.php', // path to dependency file generated by webpack
'middlewares' => [
'Underpin_Scripts\Factories\Enqueue_Script' // Enqueue the script on the front-end
]
] );
deps
argument. deps
can be an array of registered script handles, or a path fo a PHP file. If the path to the asset file exists, it will automatically set the dependencies from the file.// Check to see if the file exists.
$deps_file = plugin_dir_path(__FILE__) . 'build/index.asset.php';
// Set default fallback to dependencies array
$deps = [];
// If the file can be found, use it to set the dependencies array.
if ( file_exists( $deps_file ) ) {
$deps_file = require( $deps_file );
$deps = $file['dependencies'];
}
// Register script
wp_register_script( 'test', plugin_dir_path( __FILE__ ) . 'build/index.js', $deps );
// Enqueue the script later-on
add_action( 'wp_enqueue_scripts', function(){
wp_enqueue_script( 'test' );
} );
webpack.config.js
file in the root directory of your plugin or theme. When this is added, @wordpress/scripts will automatically use your Webpack config instead of the one that comes with @wordpress/scripts./**
* WordPress Dependencies
*/
const defaultConfig = require( '@wordpress/scripts/config/webpack.config.js' );
module.exports = {
...defaultConfig,
...{
// Add any overrides to the default here.
}
}
src/index.js
, but what happens if you want to create multiple Javascript files? Maybe you need to have one script for an admin interface, and another for the front-end of the site. Using the method above, you can override the entry
configuration of your Webpack config, and instruct @wordpress/scripts to create two files, instead./**
* External Dependencies
*/
const path = require( 'path' );
/**
* WordPress Dependencies
*/
const defaultConfig = require( '@wordpress/scripts/config/webpack.config.js' );
module.exports = {
...defaultConfig,
...{
entry: {
admin: path.resolve( process.cwd(), 'src', 'admin.js' ),
"beer-admin": path.resolve( process.cwd(), 'src', 'beer-admin.js' ),
"beer-list": path.resolve( process.cwd(), 'src', 'beer-list.css' ),
},
}
}
entry
configuration. Instead of creating the default index.js file, this instructs Webpack to create three files: