FriendsOfSymfony / FOSJsRoutingBundle

A pretty nice way to expose your Symfony routing to client applications.
1.48k stars 261 forks source link

Skip registering compile hooks #462

Closed Basster closed 8 months ago

Basster commented 1 year ago

I'm deploying my application via Docker and do Docker multi-stage build (builder-pattern) to build my frontend stuff. This looks a little bit like this:

Excerpt:

FROM composer AS composer

# install composer packages

FROM php AS php

COPY --from=composer --chown=root:www-data /app/vendor /app/vendor
COPY --chown=root:www-data . /app

RUN set -eux; \
    php bin/console assets:install; \
    php bin/console fos:js-routing:dump --format json --target assets/fos_js_routes.json;

# [...] configure php base layer

FROM node:16-alpine AS node

COPY package.json yarn.lock webpack.config.js postcss.config.js .babelrc /build/
COPY assets /build/assets
COPY public /build/public
COPY --from=php /app/public/bundles /build/public/bundles
COPY --from=php /app/assets/fos_js_routes.json /build/assets/fos_js_routes.json
COPY --from=php /app/vendor/friendsofsymfony/jsrouting-bundle /build/vendor/friendsofsymfony/jsrouting-bundle

RUN 
    set -eux; \
    yarn --pure-lockfile;  \
    yarn build;

# [...] more frontend stuff...

FROM php AS prod

# Copy frontend files.
COPY --from=node --chown=root:www-data /build/public/build /app/public/build

# [...] finalize the image

This way I avoid having composer and node in my runtime image.

Today I wanted to try the FosRouting webpack plugin and noticed, that tries to build the fos_js_routes.json during webpack build/watch, wich is pretty convenient, but didn't fit into my build process, since I don't have PHP available during yarn build.

So I decided to add an optional parameter to the FoRouting plugin, to disable registering the compile hooks, so the plugin uses the existing, statically dumped fos_js_routes.json during runtime.

// webpack.config.js

Encore
        .addPlugin(new FosRouting(
            { target: './assets/fos_js_routes.json' }, // <- path to dumped routes.json 
            false // <- set false to suppress automatic recompilation of the file
            )
        )