Set Up Webpack And Babel For Your WordPress Theme
https://medium.com/@imranhsayed/set-up-webpack-and-babel-for-your-wordpress-theme-4ab56a00c873
Set Up Webpack And Babel For Your WordPress Theme
Imran SayedFollowAug 20 · 4 min read
In this blog, I will explain to you how to set up Webpack for your project. If you want to learn the basics of Webpack and why do we use it, you can watch the video below and then come back.
You may already know that Webpack bundles all of your modules and dependencies into one file which you can then include in your project for many reasons explained in the above video.
So once we install webpack, we need to tell webpack which are the modules/files it needs to bundle and the path where it needs to output a single bundled file.![]()

So let’s begin
Prerequisites
What do you need to get started with Webpack?
Well, all you need is node js to be installed on your system. And node js comes preinstalled with npm
Create package.json file and install webpack
cd projectname
npm init --yes // creates package.jsonThen update your package.json with following details
{
"name": "aquila",
"version": "1.0.0",
"description": "Aquila theme packages",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [ "wordpress", "themes" ],
"author": "Imran Sayed",
"license": "MIT",
"private": true,
"browserslist": [
"defaults"
]
}Now lets install webpack, webpack cli , babel and some other loaders and plugins as dev dependencies
Create a directory so that we have the following structure your-theme/assets/src/js .
Then create files main.js and single.js inside of src/js directory. Also add an img directory( inside src ) and add an image cat.jpg inside of that
Now add the below script to your package.json
The Webpack Config file
You can create a config file called webpack.config.js that stores the configuration for your webpack like the path for your entry point and output file, loaders etc. If you don’t create this file then webpack uses its default configuration file.
So let’s create a file called webpack.config.js in the root of your project and add the following configurations.
Babel Configurations
Create a file called .babelrc inside assets directory and add the below configurations.
Lets create a directory called clock inside of src directory and add a file called index.js and just write some JavaScript lets say console.log(‘hello’);
Then inside of main.js import the clock/index.js and the image like so :
Running dev server
Now run npm run dev and it will run the webpack dev server in watch mode and bundle the files for you . Babel is going to convert the modern JS into JS that most browsers can understand. Notice the build folder.
You check out:
Last updated
Was this helpful?