Download the PHP package maba/webpack-bundle without Composer
On this page you can find all versions of the php package maba/webpack-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package webpack-bundle
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:
Inside script.js
:
As part of deployment into production environment, after clearing the cache:
In development environment (this must always run in the background, similar to assetic:watch
):
Alternatively, if you are not actively developing your frontend, you can compile once and forget about it, similarly to production environment:
If you are running functional tests on your app, make sure to compile once for test environment to generate manifest file for test environment:
Twig function and tag
You can choose between webpack_asset
function and webpack
tag.
Function:
type
is js
or css
, leave null
to guess the type. For css
this function could return null
if no CSS would
be extracted from provided entry point. If you are sure that there will be some CSS, you could just ignore this.
Otherwise, you could use webpack
tag as it handles this for you (omits the <link/>
tag entirely in that case).
Tag:
As with webpack_asset
function, provide js
, css
or leave it out to guess the type.
See usage with named
and group
in Using commons chunk section.
Keep in mind that you must provide hard-coded asset paths in both tag and function. This is to find all available assets in compile-time.
Stylesheets
By default, ExtractTextPlugin is configured. This means
that if you require
any file that compiles to CSS (.css
, .less
, .scss
) it is removed from compiled JS file
and stored into a separate one. So you have to include it explicitly.
Keep in mind that when you are providing entry point - it's still usually .js
file (see usage example).
If you want to disable this functionality so that CSS would be loaded together with JS in a single request,
disable extract_css
:
This plugin is also needed if you want to require css/less/sass files directly as an entry point.
ES6, Less and Sass support
ES6, Less and Sass works out of the box:
- use
.js
or.jsx
extension to compile from ES6 and ES7 to ES5 using Babel; - use
.less
extension to compile Less files; - use
.scss
extension to compile Sass files.
If you need any custom loaders, feel free to install them via npm
and modify app/config/webpack.config.js
if needed.
Loading images
Images are optimized by default using image-webpack-loader.
You can include images directly into your twig templates by using the same webpack_asset
function.
For this to work correctly, loader for image files must remain file
in your webpack configuration.
Of course, you can use them in your CSS, too:
If you are providing webpack-compatible asset path in CSS, prefix it with ~
. Use relative paths as usual.
See css-loader for more information.
Aliases
Aliases by default are prefixed with @
and point to some specific path.
You can change this prefix by configuring aliases.prefix
parameter.
Aliases work the same in both twig templates (parameter to webpack_asset
function) and Javascript files
(parameter to require
or similar Webpack provided function).
By default, these aliases are registered:
@app
, which points to%kernel.root_dir%/Resources/assets
@root
, which points to%kernel.project_dir%
(usually the root of your repository)@templates
, which points to%kernel.project_dir%/templates
@assets
, which points to%kernel.project_dir%/assets
@AcmeHelloBundle
or similar for each of your bundles. This points to root of the bundle (whereBundle
class is), same as when locating resource in Symfony itself@acme_hello
or similar for each of your bundles. This points to@AcmeHelloBundle/Resources/assets
by default.
You can also register your own aliases, for example @bower
or @npm
would be great candidates if you use any of those package managers. Or something like @vendor
if you use composer to install your frontend assets:
Inside your JavaScript files:
In case you want to use different prefix:
Now inside your JavaScript files:
Be sure to install dependencies (either npm, bower or any other) on path not directly accessible from web. This is not needed by webpack (it compiles them - they can be anywhere on the system) and could cause a security flaw (some assets contain backend examples, which could be potentially used in your production environment).
Configuration
See example with explanations.
Configuring dev-server
app/console maba:webpack:dev-server
runs webpack-dev-server as a separate process,
it listens on localhost:8080
. By default, assets in development
environment are pointed to //localhost:8080/compiled/*
.
If you run this command inside VM, docker container etc., configure
maba_webpack.config.parameters.dev_server_public_path
to use correct host. Also, as
dev-server listens only to localhost connections by default, add this to configuration:
If you need to provide different port, be sure to put --port
and the port itself into separate lines.
When compiling assets with webpack-dev-server
, webpack-dashboard
is used for more user-friendly experience. You can disable it by setting tty_prefix
option to []
.
You can also remove DashboardPlugin
in such case from webpack.config.js
.
Configuring memory for Node.js
If you are experiencing "heap out of memory" error when running maba:webpack:compile
and/or maba:webpack:dev-server
, try to give more memory for Node.js process:
Using commons chunk
This bundle supports both single and several commons chunks, but you have to configure this explicitly.
In your webpack.config.js
:
In your base template:
You can also use webpack_named_asset
twig function instead of webpack
tags.
Grouped commons chunks
webpack
tag supports group
option:
Name of assets are given in options.groups
, which is passed to your webpack.config.js
.
Assets without group set are assigned to default
group. Configuration example:
Keep in mind that currently the same entry point cannot belong to several groups, even if it's used in different places. This means that you must provide the same group for JavaScript and CSS versions of the same asset.
Note on bundle inheritance
If you use bundles with inheritance, keep in mind, that the assets themselves are usually resolved in different context (in webpack itself), assets pointing to parent bundle (by it's aliases) will actually point to child bundle. This allows you to override any of the assets in the parent bundle, but requires to copy all of them, as they will not be looked in parent bundle's directory at all.
Semantic versioning
This bundle follows semantic versioning.
Public API of this bundle (in other words, you should only use these features if you want to easily update to new versions):
- only services that are not marked as
public="false"
- only classes, interfaces and class methods that are marked with
@api
- twig functions and tags
- console commands
- supported DIC tags
For example, if only class method is marked with @api
, you should not extend that class, as constructor
could change in any release.
See Symfony BC rules for basic information
about what can be changed and what not in the API. Keep in mind, that in this bundle everything is
@internal
by default.
After updating this bundle, you should re-run maba:webpack:setup
command and review changes in files,
merge them with any of your own. This bundle might make an assumption that all the needed dependencies
are installed. As compiling is made beforehand as a deployment step, you should notice any errors in
your staging environment if there would be any.
Alternatives?
There are a few alternatives out there for integrating webpack.
Plain simple webpack commands
I would really recommend this approach - just split your frontend code (HTML, CSS, JS) from backend code (PHP + some HTTP API).
Especially if you have single-page application, there is really not much point in integrating your webpack workflow with Symfony.
In this case you could use html-webpack-plugin to generate HTML with correct URL to your bundled javascript file.
ju1ius/WebpackAssetsBundle
This is minimal bundle to provide compiled file names into your twig templates. It also uses assets-webpack-plugin.
What it's missing when compared to this bundle - gathering all entry points. You must manually set them inside your webpack config. If there are only few of them, this is usually not that hard to maintain.
Webpack config is completely manual and does not integrate with your Symfony application.
hostnet/webpack-bundle
This bundle gathers all entry points, but does not provide generated URLs to use inside your twig templates. It does solve this problem by appending modification timestamp for cache busting.
Webpack config is completely integrated into your Symfony application and requires custom PHP code for any custom configuration changes.
In development environment, it generates assets on request. This can be more convenient than having to run specific command in the background, but is usually slower. Also at this point it's kind of hard to integrate it with webpack-dev-server.
It has more tags for twig, like inline scripts. One example is inline splitpoint generation which allows you to generate simple splitpoints per file.
Running tests
All versions of webpack-bundle with dependencies
symfony/framework-bundle Version ^2.7|^3.0|^4.0
symfony/templating Version ^2.1|^3.0|^4.0
symfony/console Version ^2.5|^3.0|^4.0
symfony/routing Version ^2.7|^3.0|^4.0
symfony/process Version ^2.5|^3.0|^4.0
symfony/dependency-injection Version ^2.6|^3.0|^4.0
symfony/twig-bundle Version ^2.3|^3.0|^4.0
symfony/monolog-bundle Version ^2.3|^3.0|^4.0
twig/twig Version ^1.27|^2.0
maba/dependency-injection-extra Version ^0.1.1|^1.0