This is an example plugin to showcase working of @wpackio/scripts. It has
the following features.
- Custom implement HMR with webpack.
- Using Sass on react components.
-
Clone this repository.
-
Make sure you have docker and composer installed on your machine.
-
Install all node dependencies.
npm i
-
Install all composer dependency (just
wpackio/enqueuein our case).composer install
-
Run the following command from this directory
examples/plugin.npm run docker:up
This basically runs the following command.
docker-compose up -d && docker-compose logs -f wordpressWait until the build is complete. This will give you a WordPress development server at
http://localhost:8080. Then press ctrl + c.npm start
This will open up the development server within your network LAN Ip address.
Note that due to how
file:dependency works withnpm, it might throw some error. We recommend usingyarn, until the issue is resolved. This is just for checking out this demo. In your own project, bothnpmandyarnwill work fine. -
Now log into your WordPress dashboard with the credentials given below.
-
Activate
WPack.io Sample Plugin Developmentplugin. -
Check the homepage and check your browser's console.
It is not a requirement to use docker for
@wpackio/scripts. It is just required for this example. You can very well spin up any local server you are comfortable with.
Docker WP Dev Server
- URL: http://localhost:8080
- Username:
root - Password:
root
Now go ahead and edit the contents in file src/reactapp/components/*. Check it
load live in your console.
If you edit any of the entrypoints like src/reactapp/index.jsx, then you will see
full page reload.
With the help of browsersync, we also watch for files inside inc/**/*.php. So
go ahead and edit them. The page will reload.
We have created a simple shortcode [wpackio-reactapp]. Publish it in any page
and this will create a todo app.
Edit files under src/reactapp/ and see them refresh live (without page reload).
This is done with the help this code within the entry-point.
// Render our application
document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(<TodoApp />, document.querySelector('#wpackio-reactapp'));
// Let's Hot Module Replace the main TodoApp component
// module.hot is provided by WebPack
if (module.hot) {
module.hot.accept('./components/TodoApp', () => {
// eslint-disable-next-line global-require
const NewTodoApp = require('./components/TodoApp').default;
ReactDOM.render(
<NewTodoApp />,
document.querySelector('#wpackio-reactapp')
);
});
}
});Run
npm run buildRun
npm run archive