Skip to content

Commit

Permalink
Change copying PHP files to dist directory to opt-in via a CLI flag. (#…
Browse files Browse the repository at this point in the history
…39171)

* Enable accepting --webpack-copy-php flag from the CLI commands

* Update the glob pattern based on existence of --webpack-copy-php flag.

* Update README and changelog files.

* Add PR to the changelog.

* Update CHANGELOG.md

* Update README.md

Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>
  • Loading branch information
ryanwelcher and gziolo committed Mar 3, 2022
1 parent a1b11c5 commit b1f2064
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/scripts/CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New Feature

- Add `--webpack-copy-php` CLI flag to opt-in to copying php files from `src` and its subfolders to the output directory (`build` by default) ([#39171](https://github.com/WordPress/gutenberg/pull/39171)).

## 22.0.0 (2022-02-22)

### Breaking Changes
Expand Down
20 changes: 13 additions & 7 deletions packages/scripts/README.md
Expand Up @@ -79,20 +79,23 @@ _Example:_
{
"scripts": {
"build": "wp-scripts build",
"build:custom": "wp-scripts build entry-one.js entry-two.js --output-path=custom"
"build:custom": "wp-scripts build entry-one.js entry-two.js --output-path=custom",
"build:copy-php": "wp-scripts build --webpack-copy-php"
}
}
```

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.
- `npm run build:custom` - builds the code for production with two entry points and a custom output directory. Paths for custom entry points are relative to the project root.
- `npm run build:copy-php` - builds the code for production and opts into copying PHP files from the `src` directory and its subfolders to the output directory.

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-copy-php` – enables copying PHP files from the `src` directory and its subfolders to the output directory.
- `--webpack-no-externals` – disables scripts' assets generation, and omits the list of default externals.

#### Advanced information

Expand Down Expand Up @@ -367,7 +370,8 @@ _Example:_
"scripts": {
"start": "wp-scripts start",
"start:hot": "wp-scripts start --hot",
"start:custom": "wp-scripts start entry-one.js entry-two.js --output-path=custom"
"start:custom": "wp-scripts start entry-one.js entry-two.js --output-path=custom",
"start:copy-php": "wp-scripts start"
}
}
```
Expand All @@ -376,14 +380,16 @@ This is how you execute the script with presented setup:

- `npm start` - starts the build for development.
- `npm run start:hot` - starts the build for development with "Fast Refresh". The page will automatically reload if you make changes to the files.
- `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.
- `npm run start:custom` - starts the build for development which contains two entry points and a custom output directory. Paths for custom entry points are relative to the project root.
- `npm run start:copy-php` - starts the build for development and opts into copying PHP files from the `src` directory and its subfolders to the output directory.

This script automatically use the optimized config but sometimes you may want to specify some custom options:

- `--hot` – enables "Fast Refresh". The page will automatically reload if you make changes to the code. _For now, it requires that WordPress has the [`SCRIPT_DEBUG`](https://wordpress.org/support/article/debugging-in-wordpress/#script_debug) flag enabled and the [Gutenberg](https://wordpress.org/plugins/gutenberg/) plugin installed._
- `--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.
- `--webpack-copy-php` – enables copying PHP files from the `src` directory and its subfolders to the output directory.
- `--webpack-devtool` – controls how source maps are generated. See options at https://webpack.js.org/configuration/devtool/#devtool.
- `--webpack-no-externals` – disables scripts' assets generation, and omits the list of default externals.

#### Advanced information

Expand Down
11 changes: 5 additions & 6 deletions packages/scripts/config/webpack.config.js
Expand Up @@ -36,6 +36,10 @@ if ( ! browserslist.findConfig( '.' ) ) {
}
const hasReactFastRefresh = hasArgInCLI( '--hot' ) && ! isProduction;

const copyWebPackPattens = process.env.WP_COPY_PHP_FILES_TO_DIST
? '**/{block.json,*.php}'
: '**/block.json';

const cssLoaders = [
{
loader: MiniCSSExtractPlugin.loader,
Expand Down Expand Up @@ -228,12 +232,7 @@ const config = {
new CopyWebpackPlugin( {
patterns: [
{
from: '**/block.json',
context: 'src',
noErrorOnMissing: true,
},
{
from: '**/**.php',
from: copyWebPackPattens,
context: 'src',
noErrorOnMissing: true,
},
Expand Down
4 changes: 4 additions & 0 deletions packages/scripts/scripts/build.js
Expand Up @@ -19,6 +19,10 @@ if ( hasArgInCLI( '--webpack-bundle-analyzer' ) ) {
process.env.WP_BUNDLE_ANALYZER = true;
}

if ( hasArgInCLI( '--webpack-copy-php' ) ) {
process.env.WP_COPY_PHP_FILES_TO_DIST = true;
}

const { status } = spawn( resolveBin( 'webpack' ), getWebpackArgs(), {
stdio: 'inherit',
} );
Expand Down
4 changes: 4 additions & 0 deletions packages/scripts/scripts/start.js
Expand Up @@ -21,6 +21,10 @@ if ( hasArgInCLI( '--webpack--devtool' ) ) {
process.env.WP_DEVTOOL = getArgFromCLI( '--webpack--devtool' );
}

if ( hasArgInCLI( '--webpack-copy-php' ) ) {
process.env.WP_COPY_PHP_FILES_TO_DIST = true;
}

const { status } = spawn(
resolveBin( 'webpack' ),
[ hasArgInCLI( '--hot' ) ? 'serve' : 'watch', ...getWebpackArgs() ],
Expand Down

0 comments on commit b1f2064

Please sign in to comment.