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

Change copying PHP files to dist directory to opt-in via a CLI flag. #39171

Merged
merged 6 commits into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/scripts/CHANGELOG.md
@@ -1,6 +1,7 @@
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->

## Unreleased
- 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)

Expand Down
10 changes: 8 additions & 2 deletions packages/scripts/README.md
Expand Up @@ -79,7 +79,8 @@ _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"
}
}
```
Expand All @@ -88,11 +89,13 @@ 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:copy-php` - builds the code for production and opts into copying PHP files from src src and its subfolders to the dist 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 src and its subfolders to the dist directory.

#### 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 @@ -377,13 +381,15 @@ 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:copy-php` - starts the build for development and opts into copying PHP files from src src and its subfolders to the dist 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 src and its subfolders to the dist directory.

#### 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