Skip to content

Commit 1d347dd

Browse files
rowanparkermaxhelias
authored andcommitted
docs: How to add npm
Create build-css-js.md Update build-css-js.md Update README.md Update .dockerignore Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <[email protected]> Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <[email protected]> Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <[email protected]> Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <[email protected]> Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <[email protected]> Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <[email protected]> Update build-css-js.md Update build-css-js.md Update build-css-js.md Update build-css-js.md Rework
1 parent 4eebb98 commit 1d347dd

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ bin/*
2323
!bin/console
2424
docker/db/data/
2525
helm/
26+
node_modules/
2627
public/bundles/
2728
var/
2829
vendor/

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ A [Docker](https://www.docker.com/)-based installer and runtime for the [Symfony
2929
1. [Build options](docs/build.md)
3030
2. [Using Symfony Docker with an existing project](docs/existing-project.md)
3131
3. [Support for extra services](docs/extra-services.md)
32-
4. [Deploying in production](docs/production.md)
33-
5. [Installing Xdebug](docs/xdebug.md)
34-
6. [Using a Makefile](docs/makefile.md)
35-
7. [Troubleshooting](docs/troubleshooting.md)
32+
4. [Building CSS and JS](docs/build-css-js.md)
33+
5. [Deploying in production](docs/production.md)
34+
6. [Installing Xdebug](docs/xdebug.md)
35+
7. [Using a Makefile](docs/makefile.md)
36+
8. [Troubleshooting](docs/troubleshooting.md)
3637

3738
## Credits
3839

docs/build-css-js.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Building CSS and JS
2+
3+
For security reason and limit the size of the container, the default stack doesn't include any development "artifacts".
4+
To add it to the project and your development workflow follow this guide.
5+
6+
*This example uses `npm` and [Webpack Encore](https://symfony.com/doc/current/frontend.html).*
7+
8+
Modify `Dockerfile` to build your assets :
9+
10+
- At the top of the file before anything else
11+
```diff
12+
ARG PHP_VERSION=8.1
13+
ARG CADDY_VERSION=2
14+
+ARG NODE_VERSION=18
15+
+
16+
+# node "stage"
17+
+FROM node:${NODE_VERSION}-alpine AS symfony_node
18+
+
19+
+WORKDIR /srv/app
20+
+
21+
+COPY package*.json ./
22+
+
23+
+RUN npm install
24+
+## If you are building your code for production
25+
+# RUN npm ci --only=production
26+
+
27+
+## You need to copy everything to use PostCSS, Tailwinds, ...
28+
+COPY . .
29+
+
30+
+RUN npm run build
31+
32+
# "php" stage
33+
```
34+
35+
- Then copy the built output in `public/build` (the configuration default) to the `php` container (which will then be copied later to `caddy`).
36+
37+
```diff
38+
VOLUME /srv/app/var
39+
40+
+COPY --from=symfony_node /srv/app/public/build public/build
41+
42+
ENTRYPOINT ["docker-entrypoint"]
43+
CMD ["php-fpm"]
44+
45+
FROM caddy:${CADDY_VERSION}-builder-alpine AS symfony_caddy_builder
46+
```
47+
48+
Modify `docker-compose.override.yml` to add a `node` container in your development environment.
49+
This will provide you with hot module reloading.
50+
51+
```diff
52+
caddy:
53+
volumes:
54+
- ./docker/caddy/Caddyfile:/etc/caddy/Caddyfile:ro
55+
- ./public:/srv/app/public:ro
56+
57+
+ node:
58+
+ build:
59+
+ context: .
60+
+ target: symfony_node
61+
+ volumes:
62+
+ - ./:/srv/app
63+
+ ports:
64+
+ - target: 8080
65+
+ published: 8080
66+
+ protocol: tcp
67+
+ command: 'sh -c "npm install; npm run dev-server -- --server-type https --client-web-socket-url https://localhost:8080 --host 0.0.0.0"'
68+
```
69+
70+
If file changes are not picked up, refer to this page:
71+
https://symfony.com/doc/current/frontend/encore/virtual-machine.html#file-watching-issues

0 commit comments

Comments
 (0)