Asset Building using Webpack & Babel Modern WordPress development often separates source code (ES6+, SCSS, modules) from production-ready assets (minified JS/CSS). Tools like Webpack and Babel enable this workflow.
Key Concepts Module Bundling: Combines multiple JavaScript files into a single optimized bundle.
Transpilation: Converts modern JavaScript (ES6+) into backward-compatible versions.
Tree Shaking: Removes unused code from final bundles.
Loaders & Plugins: Extend Webpack functionality (e.g., handling CSS, images).
Typical Project Structure theme/ ├── src/ │ ├── js/ │ │ └── index.js │ ├── scss/ │ │ └── style.scss ├── dist/ │ ├── bundle.js │ └── style.css ├── webpack.config.js ├── package.json Webpack Configuration Example const path = require('path'); module.exports = { entry: './src/js/index.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js', }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', }, }, { test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'], }, ], }, mode: 'production', }; Babel Configuration (.babelrc) { "presets": ["@babel/preset-env"] } Enqueuing Compiled Assets in WordPress function theme_enqueue_assets() { wp_enqueue_script( 'theme-bundle', get_template_directory_uri() . '/dist/bundle.js', array(), '1.0', true ); wp_enqueue_style( 'theme-style', get_template_directory_uri() . '/dist/style.css', array(), '1.0' ); } add_action('wp_enqueue_scripts', 'theme_enqueue_assets'); Internal Mechanics Understanding WordPress internals is essential for extending functionality and debugging.
...