Skip to content

Commit bcef071

Browse files
committed
Update react-hooks template
1 parent 07e6357 commit bcef071

20 files changed

+851
-251
lines changed

jscomp/bsb/bsb_templates.ml

Lines changed: 438 additions & 130 deletions
Large diffs are not rendered by default.

jscomp/bsb/templates/react-hooks/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
npm-debug.log
55
/lib/bs/
66
/node_modules/
7+
/bundleOutput/

jscomp/bsb/templates/react-hooks/README.md

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,55 @@
1-
# ${bsb:name}
1+
# ReasonReact Template & Examples
22

3-
## Run Project
3+
This is:
4+
- A template for your new ReasonReact project.
5+
- A collection of thin examples illustrating ReasonReact usage.
6+
- Extra helper documentation for ReasonReact (full ReasonReact docs [here](https://reasonml.github.io/reason-react/)).
7+
8+
`src` contains 4 sub-folders, each an independent, self-contained ReasonReact example. Feel free to delete any of them and shape this into your project! This template's more malleable than you might be used to =).
9+
10+
The point of this template and examples is to let you understand and personally tweak the entirely of it. We **don't** give you an opaque, elaborate mega build setup just to put some boxes on the screen. It strikes to stay transparent, learnable, and simple. You're encouraged to read every file; it's a great feeling, having the full picture of what you're using and being able to touch any part.
11+
12+
## Run
413

514
```sh
615
npm install
16+
npm run server
17+
# in a new tab
718
npm start
8-
# in another tab
9-
npm run webpack
1019
```
1120

12-
After you see the webpack compilation succeed (the `npm run webpack` step), open up `build/index.html` (**no server needed!**). Then modify whichever `.re` file in `src` and refresh the page to see the changes.
21+
Open a new web page to `http://localhost:8000/`. Change any `.re` file in `src` to see the page auto-reload. **You don't need any bundler when you're developing**!
1322

14-
**For more elaborate ReasonReact examples**, please see https://github.com/reasonml-community/reason-react-example
23+
**How come we don't need any bundler during development**? We highly encourage you to open up `index.html` to check for yourself!
1524

16-
## Run Project with Server
25+
# Features Used
1726

18-
To run with the webpack development server run `npm run server` and view in the browser at http://localhost:8000. Running in this environment provides hot reloading and support for routing; just edit and save the file and the browser will automatically refresh.
27+
| | Blinking Greeting | Reducer from ReactJS Docs | Fetch Dog Pictures | Reason Using JS Using Reason |
28+
|---------------------------|------------------|----------------------------|--------------------|------------------------------|
29+
| No props | || | |
30+
| Has props | | | ||
31+
| Children props || | | |
32+
| No state | | | ||
33+
| Has state || || |
34+
| Has state with useReducer | || | |
35+
| ReasonReact using ReactJS | | | ||
36+
| ReactJS using ReasonReact | | | ||
37+
| useEffect || || |
38+
| Dom attribute ||| ||
39+
| Styling |||||
40+
| React.array | | || |
1941

20-
Note that any hot reload on a route will fall back to the root (`/`), so `ReasonReact.Router.dangerouslyGetInitialUrl` will likely be needed alongside the `ReasonReact.Router.watchUrl` logic to handle routing correctly on hot reload refreshes or simply opening the app at a URL that is not the root.
42+
# Bundle for Production
2143

22-
To use a port other than 8000 set the `PORT` environment variable (`PORT=8080 npm run server`).
44+
We've included a convenience `UNUSED_webpack.config.js`, in case you want to ship your project to production. You can rename and/or remove that in favor of other bundlers, e.g. Rollup.
2345

24-
## Build for Production
46+
We've also provided a barebone `indexProduction.html`, to serve your bundle.
2547

2648
```sh
27-
npm run clean
28-
npm run build
29-
npm run webpack:production
49+
npm install webpack webpack-cli
50+
# rename file
51+
mv UNUSED_webpack.config.js webpack.config.js
52+
# call webpack to bundle for production
53+
./node_modules/.bin/webpack
54+
open indexProduction.html
3055
```
31-
32-
This will replace the development artifact `build/Index.js` for an optimized version as well as copy `src/index.html` into `build/`. You can then deploy the contents of the `build` directory (`index.html` and `Index.js`).
33-
34-
If you make use of routing (via `ReasonReact.Router` or similar logic) ensure that server-side routing handles your routes or that 404's are directed back to `index.html` (which is how the dev server is set up).
35-
36-
**To enable dead code elimination**, change `bsconfig.json`'s `package-specs` `module` from `"commonjs"` to `"es6"`. Then re-run the above 2 commands. This will allow Webpack to remove unused code.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
entry: './src/Index.bs.js',
5+
// If you ever want to use webpack during development, change 'production'
6+
// to 'development' as per webpack documentation. Again, you don't have to
7+
// use webpack or any other bundler during development! Recheck README if
8+
// you didn't know this
9+
mode: 'production',
10+
output: {
11+
path: path.join(__dirname, "bundleOutput"),
12+
filename: 'index.js',
13+
},
14+
};

jscomp/bsb/templates/react-hooks/bsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
21
{
3-
"name": "react-hooks-template",
2+
"name": "reason-react-examples",
43
"reason": {
5-
"react-jsx": 3
4+
"react-jsx": 3,
65
},
76
"sources": {
87
"dir" : "src",
98
"subdirs" : true
109
},
10+
"bsc-flags": ["-bs-super-errors", "-bs-no-version-header"],
1111
"package-specs": [{
1212
"module": "commonjs",
1313
"in-source": true
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>ReasonReact Examples</title>
6+
</head>
7+
<body>
8+
<script>
9+
// stub a variable ReactJS checks. ReactJS assumes you're using a bundler, NodeJS or similar system that provides it the `process.env.NODE_ENV` variable.
10+
window.process = {
11+
env: {
12+
NODE_ENV: 'development'
13+
}
14+
};
15+
</script>
16+
17+
<!-- This is https://github.com/marijnh/moduleserve, the secret sauce that allows us not need to bundle things during development, and have instantaneous iteration feedback, without any hot-reloading or extra build pipeline needed. -->
18+
<script src="/moduleserve/load.js" data-module="./src/Index.bs.js"></script>
19+
<!-- Our little watcher. Super clean. Check it out! -->
20+
<script src="./watcher.js"></script>
21+
</body>
22+
</html>

jscomp/bsb/templates/react-hooks/src/index.html renamed to jscomp/bsb/templates/react-hooks/indexProduction.html

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
<title>ReasonReact Examples</title>
66
</head>
77
<body>
8-
Component 1:
9-
<div id="index1"></div>
10-
11-
Component 2:
12-
<div id="index2"></div>
13-
14-
<script src="/Index.js"></script>
8+
<script src="./bundleOutput/index.js"></script>
159
</body>
1610
</html>

jscomp/bsb/templates/react-hooks/package.json

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,25 @@
33
"version": "${bsb:proj-version}",
44
"scripts": {
55
"build": "bsb -make-world",
6-
"start": "bsb -make-world -w",
6+
"start": "bsb -make-world -w -ws _ ",
77
"clean": "bsb -clean-world",
8-
"test": "echo \"Error: no test specified\" && exit 1",
9-
"webpack": "webpack -w",
10-
"webpack:production": "NODE_ENV=production webpack",
11-
"server": "webpack-dev-server"
8+
"server": "moduleserve ./ --port 8000",
9+
"test": "echo \"Error: no test specified\" && exit 1"
1210
},
1311
"keywords": [
14-
"BuckleScript"
12+
"BuckleScript",
13+
"ReasonReact",
14+
"reason-react"
1515
],
1616
"author": "",
1717
"license": "MIT",
1818
"dependencies": {
19-
"react": "^16.8.1",
20-
"react-dom": "^16.8.1",
21-
"reason-react": ">=0.7.0"
19+
"react": "^16.2.0",
20+
"react-dom": "^16.2.0",
21+
"reason-react": ">=0.4.0"
2222
},
2323
"devDependencies": {
2424
"bs-platform": "^${bsb:bs-version}",
25-
"html-webpack-plugin": "^3.2.0",
26-
"webpack": "^4.0.1",
27-
"webpack-cli": "^3.1.1",
28-
"webpack-dev-server": "^3.1.8"
25+
"moduleserve": "^0.8.4"
2926
}
3027
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[@react.component]
2+
let make = (~children) => {
3+
let (show, setShow) = React.useState(() => true);
4+
5+
// Notice that instead of `useEffect`, we have `useEffect0`. See
6+
// reasonml.github.io/reason-react/docs/en/components#hooks for more info
7+
React.useEffect0(() => {
8+
let id =
9+
Js.Global.setInterval(
10+
() => setShow(previousShow => !previousShow),
11+
1000,
12+
);
13+
14+
Some(() => Js.Global.clearInterval(id));
15+
});
16+
17+
let style =
18+
if (show) {
19+
ReactDOMRe.Style.make(~opacity="1", ~transition="opacity 1s", ());
20+
} else {
21+
ReactDOMRe.Style.make(~opacity="0", ~transition="opacity 1s", ());
22+
};
23+
24+
<div style> children </div>;
25+
};

jscomp/bsb/templates/react-hooks/src/Component1.re

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)