rules_js
rules_js copied to clipboard
react-cra example as mono repo
I'm able to recreate the example of react-cra when the WORKSPACE and BUILD.bazel file are in the same directory. But when I try and move the source files, along with the BUILD file into a sub directory and update the targets in the WORKSPACE, I'm not able to get it working. Looks like all the examples at https://github.com/aspect-build/bazel-examples have these files in the same root. Do you have an example of what the react-scripts in a subdirectory might look like?
@marsh73 What issue were you running into? I got stuck here:
bazel build //examples/cra/...
INFO: Analyzed 12 targets (1 packages loaded, 4682 targets configured).
INFO: Found 12 targets...
ERROR: /Users/ewhauser/working/monorepo/examples/cra/BUILD.bazel:46:22: ReactScripts examples/cra//build failed: (Exit 1): build__js_binary.sh failed: error executing command bazel-out/darwin_arm64-opt-exec-2B5CBBC6/bin/examples/cra/build__js_binary.sh build
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
Could not find a required file.
Name: index.html
Searched in: /private/var/tmp/_bazel_ewhauser/c1fee43d9f70be2c9377b78115996411/sandbox/darwin-sandbox/3485/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/public
INFO: Elapsed time: 12.358s, Critical Path: 9.02s
INFO: 6 processes: 6 internal.
FAILED: Build did NOT complete successfully
Because it is searching for public
in the root of the execution tree.

I was struggling to get this to work in our production build so i cloned the examples to see if i could make my own mono repo and getting it to work... it feels close but I get the above whether i build or test. cc: @alexeagle
https://github.com/marsh73/rules_js
It's looking for package.json in the working directory, so I think you just need to chdir into the package for that relative filesystem lookup to work.
https://docs.aspect.build/aspect-build/rules_js/v1.0.0-beta.0/docs/js_binary-docgen.html#js_binary-chdir
adding chdir = package_name()
to your //cra:build
target seems to fix this for me.
(Bazel always runs programs with the monorepo root as the working directory, so rules_js follows that convention as our default for consistency)
@alexeagle you are so quick... thank you... worked perf... going to work on converting our production web build file now.... 🤞
@marsh73 Are you all set with this question? If so, can we close it?
@marsh73 I gave the CRA example an upgrade recently: https://github.com/aspect-build/bazel-examples/tree/main/react-cra. A added jest test targets, eslint targets, separate ts_project transpilation targets for application code and specs. The react-scripts "build" target no longer does any transpilation since that is done in fine grained ts_project targets.
I'll make another PR to re-organize the code so it looks more like it would in a monorepo with the react-scripts build target not in the root BUILD file