From b1f2064d64df4db70a379c690ee1e28ebef8b86d Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Thu, 3 Mar 2022 04:47:59 -0500 Subject: [PATCH] Change copying PHP files to dist directory to opt-in via a CLI flag. (#39171) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- packages/scripts/CHANGELOG.md | 4 ++++ packages/scripts/README.md | 20 +++++++++++++------- packages/scripts/config/webpack.config.js | 11 +++++------ packages/scripts/scripts/build.js | 4 ++++ packages/scripts/scripts/start.js | 4 ++++ 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/packages/scripts/CHANGELOG.md b/packages/scripts/CHANGELOG.md index 8e70fd94f7c41..8e39f58c9dd35 100644 --- a/packages/scripts/CHANGELOG.md +++ b/packages/scripts/CHANGELOG.md @@ -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 diff --git a/packages/scripts/README.md b/packages/scripts/README.md index eb00747bbb7bb..8fe5bfefb6fc4 100644 --- a/packages/scripts/README.md +++ b/packages/scripts/README.md @@ -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" } } ``` @@ -87,12 +88,14 @@ _Example:_ 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 @@ -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" } } ``` @@ -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 diff --git a/packages/scripts/config/webpack.config.js b/packages/scripts/config/webpack.config.js index 68dd39c810d41..6661b633de56e 100644 --- a/packages/scripts/config/webpack.config.js +++ b/packages/scripts/config/webpack.config.js @@ -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, @@ -228,12 +232,7 @@ const config = { new CopyWebpackPlugin( { patterns: [ { - from: '**/block.json', - context: 'src', - noErrorOnMissing: true, - }, - { - from: '**/**.php', + from: copyWebPackPattens, context: 'src', noErrorOnMissing: true, }, diff --git a/packages/scripts/scripts/build.js b/packages/scripts/scripts/build.js index 0e3cb887cda79..19a244da48e15 100644 --- a/packages/scripts/scripts/build.js +++ b/packages/scripts/scripts/build.js @@ -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', } ); diff --git a/packages/scripts/scripts/start.js b/packages/scripts/scripts/start.js index 3bd12cc39036e..383634ee8b46b 100644 --- a/packages/scripts/scripts/start.js +++ b/packages/scripts/scripts/start.js @@ -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() ],