-
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
Re-use webpack and babel config for build script #12685
Re-use webpack and babel config for build script #12685
Conversation
Pull the Gutenberg webpack config into a package so it can be re-used for block/extension development.
Use code from WP Calypso for handling WP externals so we don't have to have the actual list of packages accessible in our webpack configuration.
Thanks for starting this PR. I missed it and started exploration for something similar but limited in scope in #12837. I will take a closer look at what you proposed next week 👍 |
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.
Nice idea to expose Webpack config. I think we should limit this PR to the config and tackle the script part separately.
{ | ||
test: /\.js$/, | ||
exclude: [ | ||
/block-serialization-spec-parser/, |
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.
We should do something to make it more bullet-proof. I have no idea how it will behave outside of Gutenberg repository. It was a quick workaround to make ES5 modules with Webpack.
const webpack = require( 'webpack' ); | ||
const formatWebpackMessages = require( 'react-dev-utils/formatWebpackMessages' ); | ||
|
||
webpackConfig.module.rules.forEach( |
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.
See my PR: #12837.
I think we should tackle @wordpress/scripts
separately. It would be awesome to expose Webpack config first.
return `wp.${ name.replace( /-([a-z])/g, ( match, letter ) => letter.toUpperCase() ) }`; | ||
}; | ||
|
||
const wordpressExternals = ( context, request, callback ) => |
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.
Does it magically detect all imports starting with @wordpress/*
and converts them into externals?
Would it make sense to apply all improvements to the config as its own PR first so we could test them in isolation?
Do we have developers asking for this? I'm a little concerned that this is introducing more complexity into the build process and may not be used. It's taking our config one step further away from where people expect it. Sorry - this isn't meant to sound critical - I'm just trying to understand the justification for this change. |
@dmsnell Basically, there is no official tooling or guidance regarding the JS build process for block and/or extension development currently. The Gutenberg Handbook offers ESNext examples but doesn’t offer a straightforward option for actually running that code. Extracing the webpack config allows us to leverage what is already in Gutenberg to offer something for those folks in the scripts package. |
I have just landed #12837 which introduces By the way, the topic of shared Webpack config was discussed yesterday during weekly Core JS chat (link requires registration): |
I'm closing this in favor of #13814. I had to merge this code into Gutenberg repository so I could continue all the great work started here. Let's continue on the new PR 🙇 |
Description
This is an in-progress PR that adds a build-config package, which pulls the base webpack config into a package so it can be easily re-used by 3rd-party developers. It then adds a build.js script to
@wordpress/scripts
that leverages the config. Once merged, this would allow developers to either depend on the scripts package or runnpx wp-scripts build
to build blocks and extensions.By having something like this in core, we'd have some simple build tools available out of the box so that block developers don't need to hunt down 3rd-party tools or configure webpack/babel manually. Further, the build-config package may also be useful for 3rd-party tool developers (e.g. create-guten-block, gutenblock, etc) who want to better ensure their compatibility with Gutenberg.
How has this been tested?
The first thing to test with this PR is just a normal Gutenberg build because much of the webpack config has been extracted into a package. From what I can tell, everything still builds correctly, tests pass, and Gutenberg appears to function as expected.
Testing block building using wp-scripts is a bit tricky because you're running scripts from unpublished packages only available locally. To make this work, I do the following:
npm run build
.npm run copy-local-gutenberg-packages && npm i
.npm run build
in the example plugin's folder. The block's code insrc/index.js
will be built with the output being placed inbuild
.These testing instructions copy the built Gutenberg plugin and its packages into the example plugin itself because I've found that using a file ref to traverse back to the parent directory causes NPM to randomly crash and spew non-sense.
Types of changes
New feature
Checklist:
Planned follow-up PRs
Assuming we get this first PR ready and merged, I'll likely add a separate dev script with a
watch
flag and an init script that sets up a basic template/boilerplate for at least block development.