-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: Polish @wordpress/scripts README and related tutorial #14484
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f5ce8a6
Clarify that JSX is not part of ESNext in JS build setup tutorial
gziolo 016f0cf
Docs: Polish @wordpress/scripts README and related tutorial
gziolo 9a920b2
Emphasize that build and start scripts should be used with the defaul…
gziolo 4b4209e
Add Advanced information subsection for all scripts
gziolo 61a5be7
Link the existing tutorial for build and start scripts
gziolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Scripts | ||
|
||
Collection of reusable scripts for WordPress development. | ||
Collection of reusable scripts for WordPress development. This package also comes with a recommended configuration for every tool integrated to make the process seamless. | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Command-line interfaces help to turn working with an app into a pleasant experience, but it is still not enough to keep it easy to maintain in the long run. Developers are left on their own to keep all configurations and dependent tools up to date. This problem multiplies when they own more than one project which shares the same setup. Fortunately, there is a pattern that can simplify maintainers life – reusable scripts. This idea boils down to moving all the necessary configurations and scripts to one single tool dependency. In most cases, it should be possible to accomplish all tasks using the default settings, but some customization is allowed, too. With all that in place updating all projects should become a very straightforward task. | ||
|
||
|
@@ -23,22 +23,26 @@ _Example:_ | |
```json | ||
{ | ||
"scripts": { | ||
"build": "wp-scripts run build", | ||
"check-engines": "wp-scripts check-engines", | ||
"check-licenses": "wp-scripts check-licenses --production", | ||
"lint:css": "wp-scripts lint-style '**/*.css'", | ||
"lint:js": "wp-scripts lint-js .", | ||
"lint:pkg-json": "wp-scripts lint-pkg-json .", | ||
"start": "wp-scripts start", | ||
"test:e2e": "wp-scripts test-e2e", | ||
"test:unit": "wp-scripts test-unit-js" | ||
} | ||
} | ||
``` | ||
|
||
It might be also a good idea to get familiar with the [JavaScript Build Setup tutorial](/docs/designers-developers/developers/tutorials/javascript/js-build-setup.md) for setting up a development environment to use ESNext syntax. It gives a very in-depth explanation how to use [build](#build) and [start](#start) scripts. | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Available Scripts | ||
|
||
### `build` | ||
|
||
Transforms your code according the configuration provided so it's ready for production and optimized for the best performance. It uses [webpack](https://webpack.js.org/) behind the scenes. It'll lookup for a webpack config in the top-level directory of your package and will use it if it finds one. If none is found, it'll use the default config bundled within `@wordpress/scripts` packages. Learn more in the [webpack config](#webpack-config) section. | ||
Transforms your code according the configuration provided so it's ready for production and optimized for the best performance. The entry point for your project's code should be located inside `src/index.js` file. The output generated will be written to `build/index.js` file. It's similar to [start](#start) script which is better suited for development phase. | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
_Example:_ | ||
|
||
|
@@ -54,9 +58,13 @@ This is how you execute the script with presented setup: | |
|
||
* `npm run build` - builds the code for production. | ||
|
||
#### Advanced information | ||
|
||
This script uses [webpack](https://webpack.js.org/) behind the scenes. It'll lookup for a webpack config in the top-level directory of your package and will use it if it finds one. If none is found, it'll use the default config bundled within `@wordpress/scripts` packages. Learn more in the [Advanced Usage](#advanced-usage) section. | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### `check-engines` | ||
|
||
Checks if the current `node`, `npm` (or `yarn`) versions match the given [semantic version](https://semver.org/) ranges. If the given version is not satisfied, information about installing the needed version is printed and the program exits with an error code. It uses [check-node-version](https://www.npmjs.com/package/check-node-version) behind the scenes with the recommended configuration provided. You can specify your own ranges as described in [check-node-version docs](https://www.npmjs.com/package/check-node-version). | ||
Checks if the current `node`, `npm` (or `yarn`) versions match the given [semantic version](https://semver.org/) ranges. If the given version is not satisfied, information about installing the needed version is printed and the program exits with an error code. | ||
|
||
_Example:_ | ||
|
||
|
@@ -72,6 +80,10 @@ This is how you execute the script with presented setup: | |
|
||
* `npm run check-engines` - checks installed version of `node` and `npm`. | ||
|
||
#### Advanced information | ||
|
||
It uses [check-node-version](https://www.npmjs.com/package/check-node-version) behind the scenes with the recommended configuration provided. You can specify your own ranges as described in [check-node-version docs](https://www.npmjs.com/package/check-node-version). Learn more in the [Advanced Usage](#advanced-usage) section. | ||
|
||
### `check-licenses` | ||
|
||
Validates that all dependencies of a project are compatible with the project's own license. | ||
|
@@ -95,7 +107,7 @@ _Flags_: | |
|
||
### `lint-js` | ||
|
||
Helps enforce coding style guidelines for your JavaScript files. It uses [eslint](https://eslint.org/) with the set of recommended rules defined in [@wordpress/eslint-plugin](https://www.npmjs.com/package/@wordpress/eslint-plugin) npm package. You can override default rules with your own as described in [eslint docs](https://eslint.org/docs/rules/). | ||
Helps enforce coding style guidelines for your JavaScript files. | ||
|
||
_Example:_ | ||
|
||
|
@@ -111,9 +123,13 @@ This is how you execute the script with presented setup: | |
|
||
* `npm run lint:js` - lints JavaScript files in the entire project's directories. | ||
|
||
#### Advanced information | ||
|
||
It uses [eslint](https://eslint.org/) with the set of recommended rules defined in [@wordpress/eslint-plugin](https://www.npmjs.com/package/@wordpress/eslint-plugin) npm package. You can override default rules with your own as described in [eslint docs](https://eslint.org/docs/rules/). Learn more in the [Advanced Usage](#advanced-usage) section. | ||
|
||
### `lint-pkg-json` | ||
|
||
Helps enforce standards for your package.json files. It uses [npm-package-json-lint](https://www.npmjs.com/package/npm-package-json-lint) with the set of recommended rules defined in [@wordpress/npm-package-json-lint-config](https://www.npmjs.com/package/@wordpress/npm-package-json-lint-config) npm package. You can override default rules with your own as described in [npm-package-json-lint wiki](https://github.com/tclindner/npm-package-json-lint/wiki). | ||
Helps enforce standards for your `package.json` files. | ||
|
||
_Example:_ | ||
|
||
|
@@ -129,9 +145,13 @@ This is how you execute those scripts using the presented setup: | |
|
||
* `npm run lint:pkg-json` - lints `package.json` file in the project's root folder. | ||
|
||
#### Advanced information | ||
|
||
It uses [npm-package-json-lint](https://www.npmjs.com/package/npm-package-json-lint) with the set of recommended rules defined in [@wordpress/npm-package-json-lint-config](https://www.npmjs.com/package/@wordpress/npm-package-json-lint-config) npm package. You can override default rules with your own as described in [npm-package-json-lint wiki](https://github.com/tclindner/npm-package-json-lint/wiki). Learn more in the [Advanced Usage](#advanced-usage) section. | ||
|
||
### `lint-style` | ||
|
||
Helps enforce coding style guidelines for your style files. It uses [stylelint](https://github.com/stylelint/stylelint) with the [stylelint-config-wordpress](https://github.com/WordPress-Coding-Standards/stylelint-config-wordpress) configuration per the [WordPress CSS Coding Standards](https://make.wordpress.org/core/handbook/best-practices/coding-standards/css/). You can override them with your own rules as described in [stylelint user guide](https://github.com/stylelint/stylelint/docs/user-guide.md). | ||
Helps enforce coding style guidelines for your style files. | ||
|
||
_Example:_ | ||
|
||
|
@@ -147,9 +167,13 @@ This is how you execute the script with presented setup: | |
|
||
* `npm run lint:css` - lints CSS files in the whole project's directory. | ||
|
||
#### Advanced information | ||
|
||
It uses [stylelint](https://github.com/stylelint/stylelint) with the [stylelint-config-wordpress](https://github.com/WordPress-Coding-Standards/stylelint-config-wordpress) configuration per the [WordPress CSS Coding Standards](https://make.wordpress.org/core/handbook/best-practices/coding-standards/css/). You can override them with your own rules as described in [stylelint user guide](https://github.com/stylelint/stylelint/docs/user-guide.md). Learn more in the [Advanced Usage](#advanced-usage) section. | ||
|
||
### `start` | ||
|
||
Transforms your code according the configuration provided so it's ready for development. The script will automatically rebuild if you make changes to the code, and you will see the build errors in the console. It uses [webpack](https://webpack.js.org/) behind the scenes. It'll lookup for a webpack config in the top-level directory of your package and will use it if it finds one. If none is found, it'll use the default config bundled within `@wordpress/scripts` packages. Learn more in the [webpack config](#webpack-config) section. | ||
Transforms your code according the configuration provided so it's ready for development. The script will automatically rebuild if you make changes to the code, and you will see the build errors in the console. The entry point for your project's code should be located inside `src/index.js` file. The output generated will be written to `build/index.js` file. It's similar to [build](#build) script which is better suited for production usage purpose. | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
_Example:_ | ||
|
||
|
@@ -165,11 +189,15 @@ This is how you execute the script with presented setup: | |
|
||
* `npm start` - starts the build for development. | ||
|
||
#### Advanced information | ||
|
||
It uses [webpack](https://webpack.js.org/) behind the scenes. It'll lookup for a webpack config in the top-level directory of your package and will use it if it finds one. If none is found, it'll use the default config bundled within `@wordpress/scripts` packages. Learn more in the [Advanced Usage](#advanced-usage) section. | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### `test-e2e` | ||
|
||
Launches the End-To-End (E2E) test runner. It uses [Jest](https://jestjs.io/) behind the scenes and you are able to utilize all of its [CLI options](https://jestjs.io/docs/en/cli.html). You can also run `./node_modules/.bin/wp-scripts test:e2e --help` or `npm run test:e2e:help` (as presented below) to view all of the available options. | ||
Launches the End-To-End (E2E) test runner. Writing tests can be done using [Jest API](https://jestjs.io/docs/en/api) in combination with [Puppeteer API](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md): | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Writing tests can be done using Puppeteer API: | ||
> [Jest](https://jestjs.io/) is a delightful JavaScript Testing Framework with a focus on simplicity. | ||
|
||
> [Puppeteer](https://pptr.dev/) is a Node library which provides a high-level API to control Chrome or Chromium over the [DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/). Puppeteer runs [headless](https://developers.google.com/web/updates/2017/04/headless-chrome) by default, but can be configured to run full (non-headless) Chrome or Chromium. | ||
|
||
|
@@ -194,11 +222,15 @@ This script automatically detects the best config to start Puppeteer but sometim | |
|
||
We enforce that all tests run serially in the current process using [--runInBand](https://jestjs.io/docs/en/cli#runinband) Jest CLI option to avoid conflicts between tests caused by the fact that they share the same WordPress instance. | ||
|
||
#### Advanced information | ||
|
||
It uses [Jest](https://jestjs.io/) behind the scenes and you are able to utilize all of its [CLI options](https://jestjs.io/docs/en/cli.html). You can also run `./node_modules/.bin/wp-scripts test:e2e --help` or `npm run test:e2e:help` (as presented earlier) to view all of the available options. Learn more in the [Advanced Usage](#advanced-usage) section. | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### `test-unit-js` | ||
|
||
_Alias_: `test-unit-jest` | ||
|
||
Launches the unit test runner. It uses [Jest](https://jestjs.io/) behind the scenes and you are able to utilize all of its [CLI options](https://jestjs.io/docs/en/cli.html). You can also run `./node_modules/.bin/wp-scripts test-unit-js --help` or `npm run test:unit:help` (as presented below) to view all of the available options. By default, it uses the set of recommended options defined in [@wordpress/jest-preset-default](https://www.npmjs.com/package/@wordpress/jest-preset-default) npm package. You can override them with your own options as described in [Jest documentation](https://jestjs.io/docs/en/configuration). | ||
Launches the unit test runner. Writing tests can be done using [Jest API](https://jestjs.io/docs/en/api). | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
_Example:_ | ||
|
||
|
@@ -218,11 +250,19 @@ This is how you execute those scripts using the presented setup: | |
* `npm run test:unit:help` - prints all available options to configure unit tests runner. | ||
* `npm run test:unit:watch` - runs all unit tests in the watch mode. | ||
|
||
## webpack config | ||
#### Advanced information | ||
|
||
It uses [Jest](https://jestjs.io/) behind the scenes and you are able to utilize all of its [CLI options](https://jestjs.io/docs/en/cli.html). You can also run `./node_modules/.bin/wp-scripts test-unit-js --help` or `npm run test:unit:help` (as presented earlier) to view all of the available options. By default, it uses the set of recommended options defined in [@wordpress/jest-preset-default](https://www.npmjs.com/package/@wordpress/jest-preset-default) npm package. You can override them with your own options as described in [Jest documentation](https://jestjs.io/docs/en/configuration). Learn more in the [Advanced Usage](#advanced-usage) section. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See suggestions for |
||
|
||
## Advanced Usage | ||
|
||
In general, this package should be used with the set of recommended config files. While it is possible to override every single config file provided, if you have to do it, it means that your use case is more complex than anticipated. If that happens, it would be better to avoid using the whole abstraction layer and setup your project with full control over tooling used. | ||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Webpack config | ||
|
||
The `build` and `start` commands use [webpack](https://webpack.js.org/) behind the scenes. webpack is a tool that helps you transform your code into something else. For example: it can take code written in ESNext and output ES5 compatible code that is minified for production. | ||
|
||
### Default webpack config | ||
#### Default webpack config | ||
|
||
`@wordpress/scripts` bundles the default webpack config used as a base by the WordPress editor. These are the defaults: | ||
|
||
|
@@ -240,7 +280,7 @@ lodash | `import x from lodash;` | `var x = window.lodash.x;` | |
lodash-es | `import x from lodash-es;` | `var x = window.lodash.x;` | ||
WordPress packages | `import x from '@wordpress/package-name` | `var x = window.wp.packageName.x` | ||
|
||
### Provide your own webpack config | ||
#### Provide your own webpack config | ||
|
||
Should there be any situation where you want to provide your own webpack config, you can do so. The `build` and `start` commands will use your provided file when: | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkaz - I found this statement confusing and updated it to make it explicit that JSX is not a subset of ESNext as one could understand before.