-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: Change webpack configuration to include render
files in the build folder
#43917
Changes from 5 commits
f20c355
c0992c0
291d942
8f7fd61
9e4dc01
71df6dd
d532e2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,19 +62,23 @@ We commit to keeping the breaking changes minimal so you can upgrade `@wordpress | |
|
||
### `build` | ||
|
||
Transforms your code according the configuration provided so it’s ready for production and optimized for the best performance. The entry points for your project get detected by scanning all script fields in `block.json` files located in the `src` directory. The script fields in `block.json` should pass relative paths to `block.json` in the same folder. | ||
Transforms your code according the configuration provided so it’s ready for production and optimized for the best performance. | ||
|
||
_This script exits after producing a single build. For incremental builds, better suited for development, see the [start](#start) script._ | ||
|
||
The entry points for your project get detected by scanning all script fields in `block.json` files located in the `src` directory. The script fields in `block.json` should pass relative paths to `block.json` in the same folder. | ||
|
||
_Example:_ | ||
|
||
```json | ||
{ | ||
"editorScript": "file:index.js", | ||
"editorStyle": "file:editor.css", | ||
"style": "file:style.css" | ||
"script": "file:script.js", | ||
"viewScript": "file:view.js" | ||
} | ||
``` | ||
|
||
The fallback entry point is `src/index.js` (other supported extensions: `.jsx`, `.ts`, and `.tsx`) in case there is no `block.json` file found. The output generated will be written to `build/index.js`. This script exits after producing a single build. For incremental builds, better suited for development, see the [start](#start) script. | ||
The fallback entry point is `src/index.js` (other supported extensions: `.jsx`, `.ts`, and `.tsx`) in case there is no `block.json` file found. In that scenario, the output generated will be written to `build/index.js`. | ||
|
||
_Example:_ | ||
|
||
|
@@ -93,13 +97,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 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. | ||
- `npm run build:copy-php` - builds the code for production and opts into copying all PHP files from the `src` directory and its subfolders to the output directory. By default, only PHP files listed in the `render` field in the detected `block.json` files get copied. | ||
- `build:custom-directory` - builds the code for production using the `custom-directory` as the source code directory. | ||
|
||
This script automatically use the optimized config but sometimes you may want to specify some custom options: | ||
|
||
- `--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 source directory ( default is `src` ) and its subfolders to the output directory. | ||
- `--webpack-copy-php` – enables copying all PHP files from the source directory ( default is `src` ) and its subfolders to the output directory. | ||
- `--webpack-no-externals` – disables scripts' assets generation, and omits the list of default externals. | ||
- `--webpack-src-dir` – Allows customization of the source code directory. Default is `src`. | ||
|
||
|
@@ -335,19 +339,23 @@ It reuses the same logic as `npm pack` command to create an npm package tarball. | |
|
||
### `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. The entry points for your project get detected by scanning all script fields in `block.json` files located in the `src` directory. The script fields in `block.json` should pass relative paths to `block.json` in the same folder. | ||
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. | ||
|
||
_For single builds, better suited for production, see the [build](#build) script._ | ||
|
||
The entry points for your project get detected by scanning all script fields in `block.json` files located in the `src` directory. The script fields in `block.json` should pass relative paths to `block.json` in the same folder. | ||
|
||
_Example:_ | ||
|
||
```json | ||
{ | ||
"editorScript": "file:index.js", | ||
"editorStyle": "file:editor.css", | ||
"style": "file:style.css" | ||
"script": "file:script.js", | ||
"viewScript": "file:view.js" | ||
} | ||
``` | ||
|
||
The fallback entry point is `src/index.js` (other supported extensions: `.jsx`, `.ts`, and `.tsx`) in case there is no `block.json` file found. The output generated will be written to `build/index.js`. For single builds, better suited for production, see the [build](#build) script. | ||
The fallback entry point is `src/index.js` (other supported extensions: `.jsx`, `.ts`, and `.tsx`) in case there is no `block.json` file found. In that scenario, the output generated will be written to `build/index.js`. | ||
|
||
_Example:_ | ||
|
||
|
@@ -368,14 +376,14 @@ 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 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. | ||
- `npm run start:copy-php` - starts the build for development and opts into copying all PHP files from the `src` directory and its subfolders to the output directory. By default, only PHP files listed in the `render` field in the detected `block.json` files get copied. | ||
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.
As I understand from #42430 the new Shouldn't this say something like...
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. Yes, I see what you mean. It's hard to explain as it's one file per 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. Same as #43917 (comment) |
||
- `npm run start:custom-directory` - builds the code for production using the `custom-directory` as the source code 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-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 source directory ( default is `src` ) and its subfolders to the output directory. | ||
- `--webpack-copy-php` – enables copying all PHP files from the source directory ( default is `src` ) 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. | ||
- `--webpack-src-dir` – Allows customization of the source code directory. Default is `src`. | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -26,6 +26,7 @@ const { | |||
hasCssnanoConfig, | ||||
hasPostCSSConfig, | ||||
getWebpackEntryPoints, | ||||
getRenderPropPaths, | ||||
} = require( '../utils' ); | ||||
|
||||
const isProduction = process.env.NODE_ENV === 'production'; | ||||
|
@@ -36,9 +37,8 @@ if ( ! browserslist.findConfig( '.' ) ) { | |||
} | ||||
const hasReactFastRefresh = hasArgInCLI( '--hot' ) && ! isProduction; | ||||
|
||||
const copyWebpackPatterns = process.env.WP_COPY_PHP_FILES_TO_DIST | ||||
? '**/{block.json,*.php}' | ||||
: '**/block.json'; | ||||
// Get paths of the `render` props included in `block.json` files | ||||
const renderPaths = getRenderPropPaths(); | ||||
luisherranz marked this conversation as resolved.
Show resolved
Hide resolved
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. I use wordpress-scripts in our build system, and it doesn't work anymore. The whole thing is more complex, but basically, there is what it does: https://github.com/loxK/wordpress-scripts-test import wpWebpackConfig from '@wordpress/scripts/config/webpack.config.js'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import {resolve} from "path";
export default {
...wpWebpackConfig,
entry: './dir/file.js',
output: {
filename: 'file.js',
path: resolve( process.cwd(), 'assets/js' ),
},
plugins: [
...(( wpWebpackConfig?.plugins || [] ).filter(plugin => ! (plugin instanceof CopyWebpackPlugin))),
]
} » npm run build
> build
> webpack
[webpack-cli] Failed to load '/home/www/test/webpack.config.js' config
[webpack-cli] TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
at new NodeError (node:internal/errors:387:5)
at validateString (node:internal/validators:121:11)
at Object.join (node:path:1172:7)
at fromProjectRoot (/home/www/test/node_modules/@wordpress/scripts/utils/file.js:13:7)
at hasProjectFile (/home/www/test/node_modules/@wordpress/scripts/utils/file.js:16:14)
at getRenderPropPaths (/home/www/test/node_modules/@wordpress/scripts/utils/config.js:312:9)
at Object.<anonymous> (/home/www/test/node_modules/@wordpress/scripts/config/webpack.config.js:41:21)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32) {
code: 'ERR_INVALID_ARG_TYPE'
} Any clue on what to do to make it work again ? 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. Hey @loxK. I've just cloned your repository, done a I'm not sure what may be going on. Could you please try removing the 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. You need to upgrade @wordpress/scripts to 24.1.0 by editing packages.json and I upgraded it in the repository, 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. Gotcha. For some reason, It wasn't failing before because you overwrote the entry points (which also access 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. Ok, it's because you're using
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. I'm sorry, but I don't know the answer. You need to find a way to switch back to 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. Damn breaking change in a minor release version number. Hours of work ahead ... 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. Extending webpack is not covered with semver, especially when not used in conjunction with
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. By the way, if you have needs that I also encourage you to open a PR if you think there's something broken or that could be done in a better way inside 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. For reference, @loxK opened a PR to change the way the webpack config access the value of |
||||
|
||||
const cssLoaders = [ | ||||
{ | ||||
|
@@ -232,7 +232,7 @@ const config = { | |||
new CopyWebpackPlugin( { | ||||
patterns: [ | ||||
{ | ||||
from: copyWebpackPatterns, | ||||
from: '**/block.json', | ||||
context: process.env.WP_SRC_DIRECTORY, | ||||
noErrorOnMissing: true, | ||||
transform( content, absoluteFrom ) { | ||||
|
@@ -265,6 +265,17 @@ const config = { | |||
return content; | ||||
}, | ||||
}, | ||||
{ | ||||
from: '**/*.php', | ||||
context: process.env.WP_SRC_DIRECTORY, | ||||
noErrorOnMissing: true, | ||||
filter: ( filepath ) => { | ||||
return ( | ||||
process.env.WP_COPY_PHP_FILES_TO_DIST || | ||||
renderPaths.includes( filepath ) | ||||
gziolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
); | ||||
}, | ||||
}, | ||||
], | ||||
} ), | ||||
// The WP_BUNDLE_ANALYZER global variable enables a utility that represents | ||||
|
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.
As I understand from #42430 the new
render
field inblock.json
only accepts a path for just one file.Shouldn't this say something like...
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.
There might be multiple
block.json
files.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.
In that case, it's perfect in the way it's written 👍