Skip to content

Commit a8ff872

Browse files
Alex Eaglealexeagle
authored andcommitted
feat(examples): convert create-react-app example to bazel run
1 parent 146e522 commit a8ff872

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

examples/create-react-app/BUILD.bazel

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ copy_to_bin(
2929
],
3030
)
3131

32-
copy_to_bin(
33-
name = "copy_test_files",
34-
srcs = glob(_TESTS),
35-
)
36-
3732
# react-scripts can only work if the working directory is the root of the application.
3833
# So we'll need to chdir before it runs.
3934
write_file(
@@ -42,23 +37,31 @@ write_file(
4237
content = ["process.chdir(__dirname)"],
4338
)
4439

40+
_RUNTIME_DEPS = [
41+
"chdir.js",
42+
"copy_static_files",
43+
"@npm//react",
44+
"@npm//react-dom",
45+
]
46+
4547
react_scripts(
4648
# Note: this must be named "build" since react-scripts hard-codes that as the output dir
4749
name = "build",
4850
args = [
4951
"--node_options=--require=./$(execpath chdir.js)",
5052
"build",
5153
],
52-
data = [
53-
"chdir.js",
54-
"copy_static_files",
54+
data = _RUNTIME_DEPS + [
5555
"@npm//@types",
56-
"@npm//react",
57-
"@npm//react-dom",
5856
],
5957
output_dir = True,
6058
)
6159

60+
copy_to_bin(
61+
name = "copy_test_files",
62+
srcs = glob(_TESTS),
63+
)
64+
6265
react_scripts_test(
6366
name = "test",
6467
args = [
@@ -71,15 +74,20 @@ react_scripts_test(
7174
"--no-watchman",
7275
"--ci",
7376
],
74-
data = [
75-
"chdir.js",
76-
"copy_static_files",
77+
data = _RUNTIME_DEPS + [
7778
"copy_test_files",
7879
"@npm//@testing-library/dom",
7980
"@npm//@testing-library/jest-dom",
8081
"@npm//@testing-library/react",
8182
"@npm//@testing-library/user-event",
82-
"@npm//react",
83-
"@npm//react-dom",
8483
],
8584
)
85+
86+
react_scripts(
87+
name = "start",
88+
args = [
89+
"--node_options=--require=$(rootpath chdir.js)",
90+
"start",
91+
],
92+
data = _RUNTIME_DEPS,
93+
)

examples/create-react-app/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ approaches you can take to build and test your React app with Bazel.
66

77
## Available Scripts
88

9-
In the project directory, you can run:
9+
Just like with stock create-react-app, we have the same developer workflow. In the project directory, you can run:
1010

1111
### `yarn start`
1212

@@ -19,7 +19,9 @@ You will also see any lint errors in the console.
1919
### `yarn test`
2020

2121
Launches the test runner in the interactive watch mode.<br />
22-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
22+
Note that it restarts Jest on each change, so there's a large time penalty on each re-run.
23+
This can be solved by making Jest ibazel-aware as we did with Karma.
24+
See https://github.com/bazelbuild/rules_nodejs/issues/2028
2325

2426
### `yarn build`
2527

examples/create-react-app/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function App() {
88
<header className="App-header">
99
<img src={logo} className="App-logo" alt="logo" />
1010
<p>
11-
Edit <code>src/App.tsx</code> and save to reload.
11+
Edit <code>src/App.tsx</code> and saverate to reload.
1212
</p>
1313
<a
1414
className="App-link"

0 commit comments

Comments
 (0)