Closed gergelyfabian closed 2 years ago
Any hints what I could be doing wrong?
Hey @gergelyfabian, what errors are you seeing? Do you have a sample repo I could pull to repro your error? I guess I could try recreating it with what you've shared here, but if you have a repo on hand that'd be easier.
In addition I'm wondering what about static files that Play framework is adding magically (like jquery min js).
Apologies, I'm not sure I understand what you're asking here. _ Sidenote: Static Asset support hasn't been explicitly added to this rule set, but it may (should?) work. If you can get me a small repro case I may be able to help you troubleshoot.
The magical static files appearing are because of the following dependency in my project, I just realized (works with sbt):
"org.webjars" % "bootstrap" % "3.4.1"
Then the question is, how to get these files also to appear with Bazel, along with the custom ones included as static assets (in a public folder).
I'll try to provide a repro.
Here is the reproduction: https://github.com/gergelyfabian/bazel-play-example
Sbt:
sbt run
Bazel:
bazel run //app:service
The difference is that with sbt these things work:
These are missing with Bazel.
At least the two first issues may be caused by static routes not working correctly. I tried providing the public files as data
to the service, but it did not help. I also tried debugging, and I could have access to the public files in the controllers (as they are in runfiles). I guess the best would be to debug the generated routers, where they are searching for the static files (and where they are in reality), but I did not manage to do that.
I fixed both issues.
For the general /public
access I needed to move //public
to become service
's resources
:
resources = [
"//public",
],
Then, for webjars, it's a bit tricky, as Play documentation says:
WebJars are automatically extracted into a lib folder relative to your public assets for convenience. For example, if you declared a dependency on RequireJs then you can reference it from a view using a line like:
<script` data-main="@routes.Assets.at("javascripts/main.js")" type="text/javascript" src="@routes.Assets.at("lib/requirejs/require.js")"></script>
Note the lib/requirejs/require.js path. The lib folder denotes the extracted WebJar assets, the requirejs folder corresponds to the WebJar artifactId, and the require.js refers to the required asset at the root of the WebJar.
Quote from: https://www.playframework.com/documentation/2.8.1/AssetsOverview#WebJars
Bazel does not do this magic automatically for Play, so once we have the webjars dependency, we need to unpack it manually. This can be done with a genrule. I've demonstrated that in my example project:
Finally, the unpacked files need to become a resource dependency of the service:
Maybe such a genrule for webjars could become a functionality of this plugin? (with proper parameters I guess) A new rule maybe?
I changed my solution for a macro for webjars. Maybe this could be something to be moved into rules_play_routes?
Macro definition: https://github.com/gergelyfabian/bazel-play-example/blob/012379905a571e243019f01b5ab1f120ec594d81/tools/webjars.bzl
How it's used: https://github.com/gergelyfabian/bazel-play-example/blob/012379905a571e243019f01b5ab1f120ec594d81/public/BUILD
Reopening to discuss what to do with the support for webjars in rules_play_routes
, as it's officially supported by Play Framework Assets (by automatically unpacking the jars by Play):
https://www.playframework.com/documentation/2.8.1/AssetsOverview#WebJars
After considering what is the purpose of rules_play_routes I came to a conclusion that the functionality for webjars is most probably not well fitting into rules_play_routes. I moved this unzipping functionality for Webjars into a new project:
https://github.com/gergelyfabian/rules_play_utils
You can simply use this along with rules_play_routes, the two should nicely play together.
I use this from my bazel-play-example
project.
I'd propose https://github.com/lucidsoftware/rules_play_routes/pull/45 to improve documentation, and if it's merged, I'd close this issue, if you agree, @SrodriguezO.
LGTM; approved/merged that. Thanks!
I have a project that I've set up with rules_play_routes (using Play 2.8). Everything works nicely (including dynamic routes), but static files from /public do not seem to be accessible, even though I made them accessible:
I can see the static files in
bazel-bin/app/service.runfiles/my-app/public/
. In addition I'm wondering what about static files that Play framework is adding magically (like jquery min js).