Skip to content
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

Scripts: Add official flags for build and start scripts #22310

Merged
merged 6 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
"worker-farm": "1.7.0"
},
"scripts": {
"analyze-bundles": "cross-env WP_BUNDLE_ANALYZER=1 npm run build",
"analyze-bundles": "npm run build -- --webpack-bundle-analyzer",
"prebuild": "npm run check-engines",
"clean:packages": "rimraf \"./packages/*/@(build|build-module|build-style)\"",
"clean:package-types": "tsc --build --clean",
Expand Down
6 changes: 6 additions & 0 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

### New Feature

- New `--webpack-no-externals` flag added to `build` and `start` scripts. It disables scripts' assets generation, and omits the list of default externals ([#22310](https://github.com/WordPress/gutenberg/pull/22310)).
- New `--webpack-bundle-analyzer` flag added to `build` and `start` scripts. It enables visualization for the size of webpack output files with an interactive zoomable treemap ([#22310](https://github.com/WordPress/gutenberg/pull/22310)).
- New `--webpack--devtool` flag added to `start` script. It controls how source maps are generated. See options at https://webpack.js.org/configuration/devtool/#devtool ([#22310](https://github.com/WordPress/gutenberg/pull/22310)).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need wepack in the name or is it just an implementation detail?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a way to filtering those flags. We did something similar for puppeteer. We could whitelist them to not pass to webpack (jest fails when you pass it unknown flag), too. I’m fine with renaming


## 9.1.0 (2020-05-14)

### Enhancements
Expand Down
9 changes: 9 additions & 0 deletions packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ This is how you execute the script with presented setup:
* `npm run build` - builds the code for production.
* `npm run 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.

This script automatically use the optimized config but sometimes you may want to specify some custom options:
* `--webpack-no-externals` – disables scripts' assets generation, and omits the list of default externals.
* `--webpack-bundle-analyzer` – enables visualization for the size of webpack output files with an interactive zoomable treemap.

#### Advanced information

This script uses [webpack](https://webpack.js.org/) behind the scenes. It’ll look 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 provided by `@wordpress/scripts` packages. Learn more in the [Advanced Usage](#advanced-usage) section.
Expand Down Expand Up @@ -369,6 +373,11 @@ This is how you execute the script with presented setup:
* `npm start` - starts the build for development.
* `npm run start:custom` - starts the build for development which contains two entry points and a custom output folder. Paths for custom entry points are relative to the project root.

This script automatically use the optimized config but sometimes you may want to specify some custom options:
* `--webpack-no-externals` – disables scripts' assets generation, and omits the list of default externals.
* `--webpack-bundle-analyzer` – enables visualization for the size of webpack output files with an interactive zoomable treemap.
* `--webpack--devtool` – controls how source maps are generated. See options at https://webpack.js.org/configuration/devtool/#devtool.

#### Advanced information

This script uses [webpack](https://webpack.js.org/) behind the scenes. It’ll look 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 provided by `@wordpress/scripts` packages. Learn more in the [Advanced Usage](#advanced-usage) section.
Expand Down
5 changes: 4 additions & 1 deletion packages/scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ const config = {
new LiveReloadPlugin( {
port: process.env.WP_LIVE_RELOAD_PORT || 35729,
} ),
new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ),
// WP_NO_EXTERNALS global variable controls whether scripts' assets get
// generated, and the default externals set.
! process.env.WP_NO_EXTERNALS &&
new DependencyExtractionWebpackPlugin( { injectPolyfill: true } ),
].filter( Boolean ),
stats: {
children: false,
Expand Down
11 changes: 10 additions & 1 deletion packages/scripts/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ const { sync: resolveBin } = require( 'resolve-bin' );
/**
* Internal dependencies
*/
const { getWebpackArgs } = require( '../utils' );
const { getWebpackArgs, hasArgInCLI } = require( '../utils' );

process.env.NODE_ENV = process.env.NODE_ENV || 'production';

if ( hasArgInCLI( '--webpack-no-externals' ) ) {
process.env.WP_NO_EXTERNALS = true;
}

if ( hasArgInCLI( '--webpack-bundle-analyzer' ) ) {
process.env.WP_BUNDLE_ANALYZER = true;
}

const { status } = spawn( resolveBin( 'webpack' ), getWebpackArgs(), {
stdio: 'inherit',
} );
Expand Down
16 changes: 14 additions & 2 deletions packages/scripts/scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ const { sync: resolveBin } = require( 'resolve-bin' );
/**
* Internal dependencies
*/
const { getWebpackArgs } = require( '../utils' );
const { getArgFromCLI, getWebpackArgs, hasArgInCLI } = require( '../utils' );

if ( hasArgInCLI( '--webpack-no-externals' ) ) {
process.env.WP_NO_EXTERNALS = true;
}

if ( hasArgInCLI( '--webpack-bundle-analyzer' ) ) {
process.env.WP_BUNDLE_ANALYZER = true;
}

if ( hasArgInCLI( '--webpack--devtool' ) ) {
process.env.WP_DEVTOOL = getArgFromCLI( '--webpack--devtool' );
}

const { status } = spawn(
resolveBin( 'webpack' ),
getWebpackArgs( [ '--watch' ] ),
getWebpackArgs().push( '--watch' ),
{
stdio: 'inherit',
}
Expand Down
10 changes: 3 additions & 7 deletions packages/scripts/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ const hasWebpackConfig = () =>

/**
* Converts CLI arguments to the format which webpack understands.
* It allows to optionally pass some additional webpack CLI arguments.
*
* @see https://webpack.js.org/api/cli/#usage-with-config-file
*
* @param {?Array} additionalArgs The list of additional CLI arguments.
*
* @return {Array} The list of CLI arguments to pass to webpack CLI.
*/
const getWebpackArgs = ( additionalArgs = [] ) => {
let webpackArgs = getArgsFromCLI();
const getWebpackArgs = () => {
// Gets all args from CLI without those prefixed with `--webpack`.
let webpackArgs = getArgsFromCLI( [ '--webpack' ] );

const hasWebpackOutputOption =
hasArgInCLI( '-o' ) || hasArgInCLI( '--output' );
Expand Down Expand Up @@ -99,8 +97,6 @@ const getWebpackArgs = ( additionalArgs = [] ) => {
webpackArgs.push( '--config', fromConfigRoot( 'webpack.config.js' ) );
}

webpackArgs.push( ...additionalArgs );

return webpackArgs;
};

Expand Down