Bundle to Integrate Webpack into Symfony
Symfony bundle to help integrating webpack into Symfony project.
What is webpack?
Module bundler and CommonJS / AMD dependency manager.
For me, it replaces both grunt/gulp and RequireJS.
What does this bundle do?
- Finds javascript entry points inside your twig templates.
- Runs webpack with assets-webpack-plugin.
- Saves generated file names, so that twig function returns correct URL to generated asset.
Additionally, for development environment:
- Runs webpack-dev-server, which serves and regenerates assets if they are changed.
- Watches twig templates for changes, updates entry points and restarts webpack-dev-server if webpack configuration changes.
More goodies:
- Lets you configure webpack config as you want, while still providing needed parameters from Symfony, like entry points, aliases, environment and additional parameters.
- Lets you define custom entry point providers if you don't use twig or include scripts in any other way.
- Works with images and css/less/sass files out-of-the-box, if needed.
- Supports both Webpack 2 (by default) and Webpack 1.
Look at Symfony, Webpack and AngularJS Single Page Application Demo for usage examples.
Also look at MabaWebpackMigrationBundle for easier migration from AsseticBundle to webpack.
How does this compare to assetic?
Webpack lets you create components, which know their own dependencies.
With assetic, you must explicitly provide all needed javascript and stylesheet files in your templates.
If you split one of your javascript files into two files, you need to update all templates where that new dependency
is required. With webpack, you could just require('./newFile.js'); inside the javascript file and you're done.
Moreover, from javascript your can require CSS files as easily as other javascripts - require('./styles.css');
and you're set to go.
If your application is frontend-based, sooner or later you're gonna need to load your assets asynchronously. This comes by default in webpack. Assetic just bundles the assets, you need to use library like RequireJS to do that (for example, you can look at HearsayRequireJSBundle as an alternative).
webpack-dev-server supports hot-reload of your files, sometimes without page refresh (perfect for styling and some JS frameworks, like React).
Installation
Inside AppKernel:
Run command:
It copies default webpack.config.js and package.json files and runs npm install.
If any of the files already exists, you'll be asked if you'd like to overwrite them.
webpack.config.js must export a function that takes options as an argument and returns webpack config.
Feel free to modify this configuration file as you'd like - bundle just provides default one as a starting point.
You should add webpack.config.js and package.json into your repository. You should also add node_modules into
.gitignore file and run npm install similarly to composer install (after cloning repository, after package.json
is updated and as a task in your deployment). Of course, you could just add it to your repository, too.
If you want to use Webpack 1 for some reason, pass --useWebpack1 as a command line option to setup command.
Usage
Inside twig templates:
{% end_webpack %}