|
| 1 | +Here are the key advantages of this approach: |
| 2 | + |
| 3 | +- Getting started is cca 10 seconds and you are live reloading your changes in browser. No config. |
| 4 | + |
| 5 | +- There is no bundling misdirection, so source maps work. You can [debug scalaJS straight out of VSCode](https://code.visualstudio.com/docs/nodejs/browser-debugging) by following the standard VSSCode debugging instructions. |
| 6 | + |
| 7 | +- Because there's no seperate ecosystem or NPM to configure, configuring build and CI is a lot easier. No `node_modules` to worry about. I found that this simplicity infected everything around it. |
| 8 | + |
| 9 | +- In terms of performance; NPM dependancies are loaded out the CDN. This is slow the first time - but seriously - check the network browser tools when you refresh the page. The second time they are all served out of browser cache - it takes 0ms. Even better, that cache _survives application redeployment!_. If you pre-load the (fat) "internal-" xxx dependancies scalaJS produces, this combination crushes page load times. |
| 10 | + |
| 11 | +- You can use the same build tool for both backend and frontend, and share code between them. |
| 12 | + |
| 13 | +- Unit testing the frontend with the [Playwright](https://playwright.dev/java/) java client suddenly bursts into life... |
| 14 | + - And be because it's all orchestrated in the same process - you can test the _styles_ and the interplay between the application state and styles which is where most of my bugs are. |
| 15 | + |
| 16 | +- Your build becomes very simple. Here is a mill build.sc file that builds an assembly, including your static resources, frontend app and API. [Example project](https://github.com/Quafadas/mill-full-stack) |
| 17 | + |
| 18 | +```scala sc:nocompile |
| 19 | +object backend extends Common with ScalafmtModule with ScalafixModule { |
| 20 | + |
| 21 | + def ivyDeps = |
| 22 | + super.ivyDeps() ++ Config.jvmDependencies ++ Config.sharedDependencies |
| 23 | + |
| 24 | + def moduleDeps = Seq(shared.jvm) |
| 25 | + |
| 26 | + def frontendResources = T{PathRef(frontend.fullLinkJS().dest.path)} |
| 27 | + |
| 28 | + def staticAssets = T.source{PathRef(frontend.millSourcePath / "ui")} |
| 29 | + |
| 30 | + def allClasspath = T{localClasspath() ++ Seq(frontendResources()) ++ Seq(staticAssets()) } |
| 31 | + |
| 32 | + override def assembly: T[PathRef] = T{ |
| 33 | + Assembly.createAssembly( |
| 34 | + Agg.from(allClasspath().map(_.path)), |
| 35 | + manifest(), |
| 36 | + prependShellScript(), |
| 37 | + Some(upstreamAssembly2().pathRef.path), |
| 38 | + assemblyRules |
| 39 | + ) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +``` |
| 44 | +A dockerfile that uses the assembly: |
| 45 | + |
| 46 | +```Dockerfile |
| 47 | +FROM azul/zulu-openjdk-alpine:17 |
| 48 | + |
| 49 | +# See the GHA for building the assembly |
| 50 | +COPY "./out/backend/assembly.dest/out.jar" "/app/app.jar" |
| 51 | + |
| 52 | +EXPOSE 8080 |
| 53 | + |
| 54 | +ENTRYPOINT [ "java", "-jar", "/app/app.jar" ] |
| 55 | +``` |
| 56 | + |
| 57 | +Here is a GHA, that deploys that assembly to [fly.io](https://fly.io). |
| 58 | + |
| 59 | +```yaml |
| 60 | +name: Deploy to fly.io |
| 61 | + |
| 62 | +on: |
| 63 | + push: |
| 64 | + branches: [main] |
| 65 | + |
| 66 | +env: |
| 67 | + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} |
| 68 | +jobs: |
| 69 | + build: |
| 70 | + runs-on: ubuntu-latest |
| 71 | + |
| 72 | + steps: |
| 73 | + - uses: actions/checkout@v2 |
| 74 | + - uses: coursier/setup-action@main |
| 75 | + with: |
| 76 | + jvm: temurin@17 |
| 77 | + apps: mill |
| 78 | + - uses: superfly/flyctl-actions/setup-flyctl@master |
| 79 | + - name: Build application |
| 80 | + run: mill show backend.assembly -j 0 |
| 81 | + - name: Deploy to fly.io |
| 82 | + run: flyctl deploy --remote-only |
| 83 | +``` |
| 84 | +You are live. |
0 commit comments