Reusable scripts for (not only) WordPress development.
npm install @micropackage/scripts



This is a collection of scripts useful for (not only) WordPress development. Inspired by @wordpress/scripts, this package contianis essential tools with default configuration.
You only need to install one module:
`` bash`
npm install @micropackage/scripts --save-dev`
or bash`
yarn add -D @micropackage/scripts
This package exposes a binary called mp-scripts which can be called directly with npx or yarn.` bash`
npx mp-scripts build
yarn mp-scripts buildscripts
However it is intended to use this module in the section in the package.json file of our project.
` json`
{
"scripts": {
"build": "mp-scripts build",
"lint:js": "mp-scripts lint-js",
"lint:style": "mp-scripts lint-style",
"start": "mp-scripts start"
}
}
This package uses the Dependency Extraction Webpack Plugin to extract wordpress dependencies. It does two things:
* Externalize dependencies that are available as script dependencies on modern WordPress sites.
* Add an asset file for each entry point that declares an object with the list of WordPress script dependencies for the entry point. The asset file also contains the current version calculated for the current source code.
This plugin is enabled by default with default configuration but can be easyli turned off by passing --no-deps flag to your build script:` json`
{
"scripts": {
"build": "mp-scripts build --no-deps",
}
}
If you need to use this plugin with other configuration (for example you want it to generate json files instead php - see the plugin's documentation) you can extend webpack config. See Advanced Usage for more information.
Let's assume we are creating a Gutenberg block. We want to import Component class and some Gutenberg components to use in our block. Entry file would be custom-block.js:`javascript
import { Component } from '@wordpress/element';
import { Button, CheckboxControl } from '@wordpress/components';
...
`
While running mp-scripts build command the Dependency Extraction Webpack Plugin will create additional php file containing an object with dependencies list and asset version. Note, that there is also no need to add imported packages to your package.json - since they are being externalized you don't need them in node_modules.custom-block.js
The output files for this scenario are:
* custom-block.asset.php
*
Both files are located in the output directory.
The php file will have the following content:
`php`
array('wp-components', 'wp-element', 'wp-polyfill'), 'version' => 'e7e3b282b35389ecd440edc71e073e5d'); ?>version
Note that will change if your source changes.
Here is a simple example of usage:
`php
add_action( 'enqueue_block_editor_assets', function() {
$dir = plugin_dir_path( __FILE__ );
$src = "{$dir}/dist/custom-block.js";
$asset_file = "{$dir}/dist/custom-block.asset.php";
if ( file_exists( $src ) && file_exists( $asset_file ) ) {
$asset_info = require $asset_file;
wp_enqueue_script(
'custom-block',
$src,
$asset_info['dependencies'],
$asset_info['version'],
true
);
}
} );
?>
`
This script can be configured using CLI arguments or by setting up a mpScriptsConfig property in the package.json.
All arguments other than listed below will be directly passed to webpack.
#### Configuration
| Name | Argument | Type | Description |
|-------------------|--------------------|--------------|-----------------------------------------------------------------------------------------------------------------|
| inlineAssets | true | boolean\|number | Whether to use url-loader for images. If number is passed it will be used as 'limit' option.
Default: true |
| imagemin | -- | boolean\|object | Whether to use image-webpack-loader to optimize images with imagemin. Object will be passed as image-webpack-loader configuration.
Default: true |
| paths.src | --src-path | string | Source path relative to project root.
Default: 'src/assets' |
| paths.output | --output-path | string | Output path relative to project root.
Default: 'dist' |
| paths.scripts | --scripts-path | string | Scripts path relative to src\|output. Use false to skip this path.'js'
Default: |src\|output
| paths.styles | --styles-path | string | Styles path relative to . Use false to skip this path.'scss'
Default: |output
| paths.images | --images-path | string | Images path relative to . Images included in scripts and styles will be placed in this location if inlineAssets is turned off or the image size exceeds limit.'images'
Default: |output
| paths.fonts | --fonts-path | Fonts | Fonts path relative to . Font files included in scripts and styles will be placed in this location.'fonts'
Default: |
Example:
`json`
{
"mpScriptsConfig": {
"inlineAssets": 8192,
"imagemin": {
"svgo": {
"plugins": [
{ "removeDoctype": false }
]
}
},
"paths": {
"src": "src/assets",
"output": "dist",
"scripts": "js",
"styles": "scss",
}
}
}
#### Usage
Example:
`json`
{
"scripts": {
"build": "mp-scripts build",
"build:dev": "mp-scripts build --mode=development",
"build:custom": "mp-scripts build entry-one.js entry-two.js --output-path=custom",
"build:other": "mp-scripts build --entry-path=other/src --output-path=other/dist --scripts-path=scripts --styles-path=styles"
}
}yarn build
How to use it:
- - builds the code for production using entries from src/assets/js and src/assets/scss. Only files located directly in this folders will be used. All file names starting with _ (underscore) are skipped. Output files will be placed inside dist/js and dist/css directoriesyarn build:custom
- - builds the code for production with two entry points and a custom output folder. Paths for custom entry points are relative to the project root.yarn build:other
- - this will work like the default build, but will look for entries inside other/src/scripts and other/src/styles directories. Output files will be placed inside other/dist/scripts and other/dist/styles.
#### Mode
By default build script will work in development mode. There are two ways to use another mode:--mode
* by adding argument to your command
* by setting NODE_ENV variable
field in a package.json.Example:
`json
{
"scripts": {
"lint:js": "mp-scripts lint-js",
"fix:js": "mp-scripts lint-js --fix",
"lint:js:src": "mp-scripts lint-js ./src"
}
}
`How to use it:
-
yarn lint:js - lints JavaScript files in the entire project’s directories.
- yarn lint:js:src - lints JavaScript files in the project’s src subfolder’s directories.By default, files located in
dist, vendor and node_modules folders are ignored.$3
Uses stylelint to lint your style files.Example:
`json
{
"scripts": {
"lint:style": "mp-scripts lint-style",
"fix:style": "mp-scripts lint-style --fix",
"lint:css:src": "mp-scripts lint-style 'src/*/.css'"
}
}
`
How to use it:
- yarn lint:style - lints CSS and SCSS files in the entire project’s directories.
- yarn lint:css:src - lints only CSS files in the project’s src subfolder’s directories.By default, files located in
dist, vendor and node_modules folders are ignored.$3
This script works exactly like build but configured for development. It will also automatically rebuild if the code will change. All the params work the same way as in build script.Example:
`json
{
"scripts": {
"start": "mp-scripts start",
"start:custom": "mp-scripts start --entry-path=custom/src --output-path=custom/build"
}
}
`🕵️ Advanced Usage
This package ships with default config files for eslint, stylelint and webpack. Each config file can be overriden in your project.
$3
To extend default webpack config you can provide your own
webpack.config.js file and require the provided webpack.config.js file. You can use spread operator to import parts of the config.In the example below a webpack.config.js file is added to the root folder extending the provided webpack config to include url-loader for images:
`javascript
const defaultConfig = require( "@micropackage/scripts/config/webpack.config" );module.exports = {
...defaultConfig,
module: {
...defaultConfig.module,
rules: [
...defaultConfig.module.rules,
{
test: /\.(png|jpg|gif)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
name: '[name].[ext]',
outputPath: 'images',
},
},
],
},
],
},
};
`$3
There are separate scripts to lint js and (s)css files, but it would be nice to have a single task to lint both. It can be accomplished using external modules, e.g. npm-run-all.
Using this module ypu can define scripts which will run other scripts in parallel or sequential using
run-p or run-s commands.
`json
{
"scripts": {
"lint:style": "mp-scripts lint-style",
"lint:js": "mp-scripts lint-js",
"fix:style": "mp-scripts lint-style --fix",
"fix:js": "mp-scripts lint-js --fix",
"lint": "run-p \"lint:*\"",
"lint:fix": "run-p \"fix:*\"",
}
}
`
With the above config in your package.json you can run:
* yarn lint - to run both lint:style and lint:js in parallel
* yarn lint:fix - to run both fix:style and fix:js` in parallelMicropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.
The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.
Micropackages are maintained by BracketSpace.
GNU General Public License (GPL) v3.0. See the LICENSE file for more information.