The short answer is, that your bundle should contain a Resources/public/
folder with the built production assets in it. You can use the assets:install
-command to copy or symlink these files into your project's public/
directory where it will automatically be placed in a subdirectory named after your bundle, so for example public/bundles/appbundle/
. In your bundle you can then assume this path exists and load assets from there, e.g. in a twig-template with the asset-helper: {{ asset('bundles/appbundle/images/my_image.jpg') }}
.
If you want to use EasyAdminBundle as a reference, here is an explanation how it works there:
Yes, EasyAdminBundle provides a webpack.config.js
, but it is mainly for development and will never be used by your application. Instead the configuration will write the production assets to the appropriate places inside the bundle (namely src/Resources/public
):
Encore
.setOutputPath('./src/Resources/public/')
.setManifestKeyPrefix('bundles/easyadmin')
.enableSourceMaps(false)
.enableVersioning(false)
.disableSingleRuntimeChunk()
These built assets are part of the bundle (see the link above) and are always distributed with the bundle, so no need to build frontend assets yourself when you use the bundle. Those production assets can then be used directly by your application by running a Symfony command which copies frontend resources into your project's public/
-directory:
php bin/console assets:install
The assets:install command installs bundle assets into a given
directory (e.g. the public directory).
php bin/console assets:install public
A "bundles" directory will be created inside the target directory and the
"Resources/public" directory of each bundle will be copied into it.
In your templates you can then rely on these assets using the bundle-prefix whenever you require these assets, e.g. for app.css
(You can see this being used in EasyAdminBundle's layout.html.twig):
{{ asset('bundles/easyadmin/app.css') }}
If you want to do something similar, you will probably have to ensure that the webpack-settings do not break accessing the css files directly, e.g. runtime chunks or versioning, like EasyAdminBundle does. Alternatively you could ask users of your bundle to also reference the manifest.json in your bundle in their assets-config, but then you probably have to rely on the package naming which might not be what you want.