Skip to content

Conversation

@florentdestremau
Copy link

@florentdestremau florentdestremau commented Oct 26, 2025

This is useful for plug-and-play production mode for asset-mapper

@maxhelias maxhelias requested a review from dunglas October 26, 2025 18:03
Copy link
Contributor

@bkosun bkosun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that according to the AssetMapper documentation, asset compilation should be performed in the production environment, otherwise Symfony will stop serving assets dynamically:

If you run the asset-map:compile command on your development machine, you won't see any changes made to your assets when reloading the page. To resolve this, delete the contents of the public/assets/ directory. This will allow your Symfony application to serve those assets dynamically again.

https://symfony.com/doc/7.3/frontend/asset_mapper.html#serving-assets-in-dev-vs-prod

It is also necessary to provide for the possibility of installing dependencies, as they may not be installed:

All packages in importmap.php are downloaded into an assets/vendor/ directory, which should be ignored by git (the Flex recipe adds it to .gitignore for you). You'll need to run the following command to download the files on other computers if some are missing:
php bin/console importmap:install

https://symfony.com/doc/7.3/frontend/asset_mapper.html#importing-3rd-party-javascript-packages

In addition, the current solution will run dependency installation and asset compilation every time the container is started, and even if various checks are added, this is not the best solution, since building the project at this stage takes time (the result is not cached) and can lead to various errors (e.g., due to network issues). That is why the installation of Composer dependencies for the production environment is performed during image build:

COPY --link composer.* symfony.* ./
RUN set -eux; \
composer install --no-cache --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress
# copy sources
COPY --link . ./
RUN rm -Rf frankenphp/
RUN set -eux; \
mkdir -p var/cache var/log; \
composer dump-autoload --classmap-authoritative --no-dev; \
composer dump-env prod; \
composer run-script --no-dev post-install-cmd; \
chmod +x bin/console; sync;

@florentdestremau
Copy link
Author

florentdestremau commented Oct 27, 2025

Please note that according to the AssetMapper documentation, asset compilation should be performed in the production environment, otherwise Symfony will stop serving assets dynamically:

I added a check on APP_ENV so that this is only run in prod mode, you are right :)

As for using this in build phase instead of run phase, I tried adding this to the dockerfile in the frankenphp_prod build phase, at the very end of the file:

RUN if [ -f importmap.php ]; then \
    php bin/console importmap:install;\
    php bin/console asset-map:compile;\
fi

This works, and seems better placed then ?

@florentdestremau florentdestremau changed the title Add asset-map compilation to entrypoint script Add asset-map compilation if importmap is present Oct 27, 2025
@florentdestremau florentdestremau changed the title Add asset-map compilation if importmap is present Add asset-map compilation if importmap is defined Oct 27, 2025
Copy link
Contributor

@bkosun bkosun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use the set -eux command to enable tracing and stop script execution in case of an error, as well as sync to synchronize file system changes. Additionally, you may want to combine all commands into a single layer.

This is usefull for plug-and-play production mode for asset-mapper
@7-zete-7
Copy link
Contributor

The path to importmap.php can be configured differently using asset_mapper.importmap_path.

Does it make sense to replace the check for the existents of the importmap.php file with a command like php bin/console list --quiet --no-interaction 'asset-map' 2>/dev/null?

@florentdestremau
Copy link
Author

florentdestremau commented Oct 27, 2025

Launching a full command would be slower, another way would be to check composer.json graph ?
Like this:

    if composer show symfony/asset-mapper >/dev/null 2>&1; then \
        php bin/console asset-map:compile; \
    fi; \

Or even simpler with a grep ?

    if grep -q '"symfony/asset-mapper"' composer.json; then \
        php bin/console asset-map:compile; \
    fi; \

To be honest, I'm not sure a template should handle the weird use case where importmap is elsewhere. Looking at the conditions in the docker-entrypoint.sh, if grep -q ^DATABASE_URL= .env; then is already opiniated on the fact that you use full URL env var for your database, and not the DATABASE_PORT, DATABASE_PASSWORD, etc.

Same for migrations: if [ "$(find ./migrations -iname '*.php' -print -quit)" ]; then

So if you stray from the default config you are already accepting that this template's defaults might not work out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants