I decided to give Webpack Encore a whirl while I was building this site, and I thought I would document my experience.
Webpack Encore is a simpler way to integrate Webpack into your application. It wraps Webpack, giving you a clean & powerful API for bundling JavaScript modules, pre-processing CSS & JS and compiling and minifying assets. Encore gives you professional asset system that's a delight to use.
Webpack Encore configures Webpack with sensible defaults, and it also configures Webpack using method calls instead of a JSONish configuration script. You can use it any time you would normally use Webpack, and it also appears to be a replacement for Assetic in Symonfy projects. Which means Symfony developers should get ready to use it.
Configuration
I'm using the following configuration script which I saved in the Symfony project root directory as webpack.config.js.
var Encore = require('@symfony/webpack-encore');
var webpack = require('webpack');
Encore
.setOutputPath('./web/build/')
.setPublicPath('/build')
.cleanupOutputBeforeBuild()
.addEntry('js/app', './web/src/js/index.jsx')
.addStyleEntry('css/app', './web/src/scss/app.scss')
.enableReactPreset()
.enableSassLoader()
.enablePostCssLoader()
.autoProvidejQuery()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
;
let config = Encore.getWebpackConfig();
module.exports = config;
Versioning
Webpack Encore supports versioning assets through the use of a JSON manifest file. Versioning
renames the asset files when they change, which forces browsers to re-download them. Webpack Encore
and the template asset()
function handles everything for you.
The following template:
<!DOCTYPE html>
<html lang="en">
<head>
<title>headzoo.io</title>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link href="" rel="stylesheet" media="screen">
</head>
<body>
<div id="mount"></div>
<script src=""></script>
</body>
</html>
Will render as the following HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>headzoo.io</title>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link href="/build/css/app.0cbc3ec83fb34ac256fba3b70cb78067.css" rel="stylesheet" media="screen">
</head>
<body>
<div id="mount"></div>
<script src="/build/js/app.bb3cb705fc87f2a5570e.js"></script>
</body>
</html>
Versioning needs to be enabled in app/config/config.yml.
framework:
assets:
json_manifest_path: "%kernel.project_dir%/web/build/manifest.json"
Running
During development I run the following command in the PhpStorm terminal to have Webpack Encore to rebuild the js & scss files as they change.
node_modules/.bin/encore dev --watch
To save myself a bit of typing I saved the command in a bash script as bin/encore.
#!/usr/bin/env bash
./node_modules/.bin/encore $@
Which I ran using this command.
bin/encore dev --watch
Conclusion
I give Webpack Encore a solid B+. I found the method calls easier to work with because PhpStorm was able to provide type hints for each possible setting. When using Webpack's own configuration script, I find myself constantly checking the Webpack documentation. Which is a pain.