Open swashata opened 6 years ago
Also the following code block represents the logic for the browser-sync, webpack-dev-middleware and webpack-hot-middleware implementation.
I wanted to update on the status. Currently I am spending time at WordCamp Ahmedabad. I will get back to regular work on 4th and hopefully by that time @ahmadawais can make a decision on how to proceed with this.
Probably worth checking out this implementation. https://github.com/zackify/gutenblock I believe it's using webpack-dev-server calling it from WP like:
wp_enqueue_script(
'crossfield_blocksjs',
//IN PRODUCTION put plugins_url( '/build/main.js', dirname( __FILE__ ) )
'http://localhost:8080/main.js',
array( 'wp-blocks', 'wp-i18n', 'wp-element' )
);
In my testing it works great.
Hi @ahmadawais if you can comment on the configuration decision, we can surely make some progress with this.
I was thinking we can pass in the options from CLI parameters
cgb-scripts start --proxy http://wpdev.test --disturl "/wp-content/plugins/superawesome/dist"
If the parameters are present, then invoke browser-sync (+ webpack dev middleware) with hot reload, otherwise fallback to current behavior.
Feature Request
Per our discussion #104 let's take one step at a time. So here are the things we would need to implement HMR.
Development Server
We have option for
webpack-dev-server
andbrowser-sync
along withwebpack-dev-middleware
andwebpack-hot-middleware
.While it could be tempting to use
webpack-dev-server
, I would suggest against it.mu-plugins
snippet.I do not know of any reverse proxy development server that works conflict free for WordPress. I think browser-sync along with
webpack-(dev|hot)-middleware
is the way to go forward.Configuration
At the minimum, we would need to ask the following from the end user
proxy
- The URL of the local WordPress development server.distURLPath
- The public URL path of thedist
directory (/wp-content/plugins/my-awesome-plugin/dist
). This is where our hot server will live andwebpack-dev-server
will serve from. In my experience, during development, if we just serve from memory and not write to disk, it speeds things up. Otherwise for hot reload, you just get a lot of.json
file (to start with).So the working will be like this
webpack-dev-middleware
'spublicPath
would bedistURLPath
. Given WordPress enqueues.../dist/build.js
,output.path
is..../dist
and theoutput.filename
isbuild.js
, the dev middleware will properly serve the build file from that particular URL (without writing to the disk).webpack-hot-middleware
'sclient.path
would be/__cgb_hmr
andserver.path
would be/__cgb_hmr
(could be anything for that matter).webpack-hot-middleware
will connect to the hmr path.But this configuration directly conflicts with the philosophy of your project
And I don't see a way past it. There are just too many good setup of local WordPress development server, so I don't think we can just pick one and go.
What will work
hot
component withreact-hot-loader
withinedit
function. Since it is injected somewhere within the component tree, it doesn't always work. Also it completely messes up gutenberg save feature. It never gets the proper attribute and component from theedit
tree (because it is a proxy fromreact-hot-loader
) and always ends up saving an empty HTML structure.Some more thought
This approach will only provide HMR for CSS and live reload for JavaScript at the cost of asking some configuration value from user.
Also please note that the
distURLPath
I ask, would be used in development mode only, without changing webpack'soutput.publicPath
. If we were to touchoutput.publicPath
, we would have to do it dynamically. But that's a discussion for another feature.So that's about what I think we need to implement HMR. Let's have some discussion and agree on a upgrade path before we start trying ✌️.