You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -38,50 +38,151 @@ However it is intended to use this module in the `scripts` section in the `packa
38
38
"build": "mp-scripts build",
39
39
"lint:js": "mp-scripts lint-js",
40
40
"lint:style": "mp-scripts lint-style",
41
-
"start": "mp-scripts start",
41
+
"start": "mp-scripts start"
42
42
}
43
43
}
44
44
```
45
45
46
+
### WordPress Dependencies
47
+
48
+
This package uses the [Dependency Extraction Webpack Plugin](https://www.npmjs.com/package/@wordpress/dependency-extraction-webpack-plugin) to extract wordpress dependencies. It does two things:
49
+
* Externalize dependencies that are available as script dependencies on modern WordPress sites.
50
+
* 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.
51
+
52
+
This plugin is enabled by default with default configuration but can be easyli turned off by passing `--no-deps` flag to your build script:
53
+
```json
54
+
{
55
+
"scripts": {
56
+
"build": "mp-scripts build --no-deps",
57
+
}
58
+
}
59
+
```
60
+
61
+
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](https://www.npmjs.com/package/@micropackage/scripts#%EF%B8%8F-advanced-usage) for more information.
62
+
63
+
### Using WordPress Dependencies
64
+
65
+
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`:
While running `mp-scripts build` command the [Dependency Extraction Webpack Plugin](https://www.npmjs.com/package/@wordpress/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`.
Uses [webpack](https://webpack.js.org/) to transform your code. The default [webpack](https://webpack.js.org/) configuration scans entry directory and uses each file as an entry point. It is also possible to use css/scss files as entry points. Using [MiniCssExtractPlugin](https://github.com/[webpack](https://webpack.js.org/)-contrib/mini-css-extract-plugin) and a custom plugin for asset cleanup this will emit only css file for css/scss etry.
50
-
All the params passed to this script will be passed forward to [webpack](https://webpack.js.org/). There are also params specific for this script:
111
+
Uses [webpack](https://webpack.js.org/) to transform your code. By default it will scan the source paths to automatically create an entry point for each file. Subfolders are not scanned, also files which names start with underscore are skipped. Entry points can be js and (s)css files. Using [MiniCssExtractPlugin](https://github.com/webpack-contrib/mini-css-extract-plugin) and a custom plugin for asset cleanup this will emit only css file for (s)css entry.
112
+
113
+
This script can be configured using CLI arguments or by setting up a `mpScriptsConfig` property in the `package.json`.
114
+
115
+
All arguments other than listed below will be directly passed to [webpack](https://webpack.js.org/).
|**--entry-dir**| 'src/assets' | Directory in which to look for entries. |
55
-
|**--output-dir**| 'dist' | Output directory.<br/>_**Note:** this is not the same as [webpack](https://webpack.js.org/)'s `--output-path`. See examples below._|
56
-
|**--js-dir**| 'js' | Scripts dir inside `--entry-dir`|
57
-
|**--style-dir**| 'scss' | Style dir inside `--entry-dir`|
|**urlLoader**| -- | boolean\|number | Whether to use [url-loader](https://github.com/webpack-contrib/url-loader) for images. If number is passed it will be used as '[limit](https://github.com/webpack-contrib/url-loader#limit)' option.<br />**Default: `true`**|
122
+
|**imagemin**| -- | boolean\|object | Whether to use [image-webpack-loader](https://github.com/tcoopman/image-webpack-loader) to optimize images with [imagemin](https://github.com/imagemin/imagemin). Object will be passed as [image-webpack-loader](https://github.com/tcoopman/image-webpack-loader) configuration.<br />**Default: `true`**|
|**paths.scripts**|**--scripts-path**| string | Scripts path relative to `src\|output`. Use `false` to skip this path.<br />**Default: `'js'`**|
126
+
|**paths.styles**|**--styles-path**| string | Styles path relative to `src\|output`. Use `false` to skip this path.<br />**Default: `'scss'`**|
127
+
|**paths.images**|**--images-path**| string | Images path relative to `output`. Images included in scripts and styles will be placed in this location if `urlLoader` is turned off or the image size exceeds `limit`.<br />**Default: `'images'`**|
-`yarn build` - 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` directories
71
165
-`yarn 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.
72
166
-`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`.
73
167
168
+
#### Mode
169
+
170
+
By default `build` script will work in development mode. There are two ways to use another mode:
171
+
* by adding `--mode` argument to your command
172
+
* by setting NODE_ENV variable
173
+
74
174
### `lint-js`
75
175
Lints your code using [eslint](https://eslint.org/).
76
176
Default linting ruleset is [@wordpress/eslint-plugin/recommended](https://www.npmjs.com/package/@wordpress/eslint-plugin). This can be overwriten by placing an [eslint](https://eslint.org/) config file in your project or specifing `eslintConfig` field in a `package.json`.
77
177
78
178
*Example:*
79
179
```json
80
180
{
81
-
"scripts": {
82
-
"lint:js": "mp-scripts lint-js",
83
-
"lint:js:src": "mp-scripts lint-js ./src"
84
-
}
181
+
"scripts": {
182
+
"lint:js": "mp-scripts lint-js",
183
+
"fix:js": "mp-scripts lint-js --fix",
184
+
"lint:js:src": "mp-scripts lint-js ./src"
185
+
}
85
186
}
86
187
```
87
188
@@ -97,10 +198,11 @@ Uses [stylelint](https://stylelint.io/) to lint your style files.
By default, files located in `dist`, `vendor` and `node_modules` folders are ignored.
111
213
112
214
### `start`
113
-
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 as in `build` script.
215
+
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.
@@ -126,13 +228,13 @@ This script works exactly like `build` but configured for development. It will a
126
228
127
229
This package ships with default config files for [eslint](https://eslint.org/), [stylelint](https://stylelint.io/) and [webpack](https://webpack.js.org/). Each config file can be overriden in your project.
To extend default [webpack](https://webpack.js.org/) 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.
132
234
133
235
In the example below a webpack.config.js file is added to the root folder extending the provided [webpack](https://webpack.js.org/) config to include [url-loader](https://github.com/webpack-contrib/url-loader) for images:
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](https://github.com/mysticatea/npm-run-all).
266
+
Using this module ypu can define scripts which will run other scripts in parallel or sequential using `run-p` or `run-s` commands.
267
+
```json
268
+
{
269
+
"scripts": {
270
+
"lint:style": "mp-scripts lint-style",
271
+
"lint:js": "mp-scripts lint-js",
272
+
"fix:style": "mp-scripts lint-style --fix",
273
+
"fix:js": "mp-scripts lint-js --fix",
274
+
"lint": "run-p \"lint:*\"",
275
+
"lint:fix": "run-p \"fix:*\"",
276
+
}
277
+
}
278
+
```
279
+
With the above config in your `package.json` you can run:
280
+
*`yarn lint` - to run both `lint:style` and `lint:js` in parallel
281
+
*`yarn lint:fix` - to run both `fix:style` and `fix:js` in parallel
282
+
161
283
## 📦 About the Micropackage project
162
284
163
285
Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.
0 commit comments