Skip to content

Commit 5425ea4

Browse files
Update bsb_templates, add description in readme
1 parent 90bcbe9 commit 5425ea4

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

jscomp/bsb/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,13 @@ Bsb is BuckleScript's build system. User-facing documentations are [here](https:
55
This directory hosts its implementation. It reads into `bsconfig.json`, uses some BS/OCaml/Reason-specific logic, and generates a [ninja](https://ninja-build.org) build file then calls `ninja` on it. So much of the incremental build and perf work is delegated to Ninja.
66

77
There's a `templates/` subdirectory. It's the thing shown when you do `bsb -themes`. To generate a template for the user, it basically picks the chosen template from `templates/` and copy pastes it into the destined user directory while substituting some strings in those templates, like `${bsb:proj-version}` in the `package.json`s. The copy-pasting of folders isn't actually done naively through a call to unix `cp`. It's cleverly achieved through something called ocamlres. Please see more descriptions in `pack-templates.sh`.
8+
9+
## Add/edit a template
10+
11+
The content of `templates` is packed into `bsb_templates.ml` automatically when running `pack-templates.sh`, located in this directory.
12+
13+
When adding/editing a template the script needs to be rerun to update the relevant parts in `bsb_templates.ml`. The script uses [`ocamlres`](https://github.com/OCamlPro/ocp-ocamlres), a tool for embedding files inside ocaml executables. You will need to install it with [`opam`](https://opam.ocaml.org/doc/Install.html), ocaml package manager. Follow the [instructions](https://opam.ocaml.org/doc/Install.html) to install `opam`, if you haven't installed it yet. To install `ocp-ocamlres` run:
14+
15+
```sh
16+
opam install ocp-ocamlres
17+
```

jscomp/bsb/bsb_templates.ml

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,206 @@ let root = OCamlRes.Res.([
995995
</body>\n\
996996
</html>\n\
997997
")]) ;
998+
Dir ("react-starter", [
999+
Dir ("src", [
1000+
File ("Index.re",
1001+
"[%bs.raw {|require(\"./index.css\")|}];\n\
1002+
\n\
1003+
ReactDOMRe.renderToElementWithId(<App />, \"root\");\n\
1004+
") ;
1005+
File ("index.css",
1006+
"body {\n\
1007+
\ margin: 0;\n\
1008+
\ font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", \"Roboto\", \"Oxygen\",\n\
1009+
\ \"Ubuntu\", \"Cantarell\", \"Fira Sans\", \"Droid Sans\", \"Helvetica Neue\",\n\
1010+
\ sans-serif;\n\
1011+
\ -webkit-font-smoothing: antialiased;\n\
1012+
\ -moz-osx-font-smoothing: grayscale;\n\
1013+
}\n\
1014+
\n\
1015+
main {\n\
1016+
\ padding: 20px;\n\
1017+
}\n\
1018+
") ;
1019+
File ("App.re",
1020+
"type state = {count: int};\n\
1021+
\n\
1022+
type action =\n\
1023+
\ | Increment\n\
1024+
\ | Decrement;\n\
1025+
\n\
1026+
let initialState = {count: 0};\n\
1027+
\n\
1028+
let reducer = (state, action) =>\n\
1029+
\ switch (action) {\n\
1030+
\ | Increment => {count: state.count + 1}\n\
1031+
\ | Decrement => {count: state.count - 1}\n\
1032+
\ };\n\
1033+
\n\
1034+
[@react.component]\n\
1035+
let make = () => {\n\
1036+
\ let (state, dispatch) = React.useReducer(reducer, initialState);\n\
1037+
\n\
1038+
\ <main>\n\
1039+
\ {React.string(\"Simple counter with reducer\")}\n\
1040+
\ <div>\n\
1041+
\ <button onClick={_ => dispatch(Decrement)}>\n\
1042+
\ {React.string(\"Decrement\")}\n\
1043+
\ </button>\n\
1044+
\ <span> {state.count |> string_of_int |> React.string} </span>\n\
1045+
\ <button onClick={_ => dispatch(Increment)}>\n\
1046+
\ {React.string(\"Increment\")}\n\
1047+
\ </button>\n\
1048+
\ </div>\n\
1049+
\ </main>;\n\
1050+
};\n\
1051+
") ;
1052+
File ("index.html",
1053+
"<!DOCTYPE html>\n\
1054+
<html lang=\"en\">\n\
1055+
\ <head>\n\
1056+
\ <meta charset=\"UTF-8\" />\n\
1057+
\ <title>Reason react starter</title>\n\
1058+
\ </head>\n\
1059+
\ <body>\n\
1060+
\ <div id=\"root\"></div>\n\
1061+
\ <script src=\"/Index.js\"></script>\n\
1062+
\ </body>\n\
1063+
</html>\n\
1064+
")]) ;
1065+
File ("bsconfig.json",
1066+
"{\n\
1067+
\ \"name\": \"reason-react-starter\",\n\
1068+
\ \"reason\": {\n\
1069+
\ \"react-jsx\": 3\n\
1070+
\ },\n\
1071+
\ \"sources\": {\n\
1072+
\ \"dir\": \"src\",\n\
1073+
\ \"subdirs\": true\n\
1074+
\ },\n\
1075+
\ \"bsc-flags\": [\"-bs-super-errors\", \"-bs-no-version-header\"],\n\
1076+
\ \"package-specs\": [\n\
1077+
\ {\n\
1078+
\ \"module\": \"commonjs\",\n\
1079+
\ \"in-source\": true\n\
1080+
\ }\n\
1081+
\ ],\n\
1082+
\ \"suffix\": \".bs.js\",\n\
1083+
\ \"namespace\": true,\n\
1084+
\ \"bs-dependencies\": [\"reason-react\"],\n\
1085+
\ \"refmt\": 3\n\
1086+
}\n\
1087+
") ;
1088+
File ("package.json",
1089+
"{\n\
1090+
\ \"name\": \"${bsb:name}\",\n\
1091+
\ \"version\": \"${bsb:proj-version}\",\n\
1092+
\ \"scripts\": {\n\
1093+
\ \"build\": \"bsb -make-world\",\n\
1094+
\ \"start\": \"bsb -make-world -w -ws _ \",\n\
1095+
\ \"clean\": \"bsb -clean-world\",\n\
1096+
\ \"webpack\": \"webpack -w\",\n\
1097+
\ \"webpack:production\": \"NODE_ENV=production webpack\",\n\
1098+
\ \"server\": \"webpack-dev-server\",\n\
1099+
\ \"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"\n\
1100+
\ },\n\
1101+
\ \"keywords\": [\n\
1102+
\ \"BuckleScript\",\n\
1103+
\ \"ReasonReact\",\n\
1104+
\ \"reason-react\"\n\
1105+
\ ],\n\
1106+
\ \"author\": \"\",\n\
1107+
\ \"license\": \"MIT\",\n\
1108+
\ \"dependencies\": {\n\
1109+
\ \"react\": \"^16.8.1\",\n\
1110+
\ \"react-dom\": \"^16.8.1\",\n\
1111+
\ \"reason-react\": \">=0.7.0\"\n\
1112+
\ },\n\
1113+
\ \"devDependencies\": {\n\
1114+
\ \"bs-platform\": \"^${bsb:bs-version}\",\n\
1115+
\ \"webpack\": \"^4.0.1\",\n\
1116+
\ \"webpack-cli\": \"^3.1.1\",\n\
1117+
\ \"webpack-dev-server\": \"^3.1.8\",\n\
1118+
\ \"html-webpack-plugin\": \"^3.2.0\",\n\
1119+
\ \"style-loader\": \"^1.0.0\",\n\
1120+
\ \"css-loader\": \"^3.2.0\"\n\
1121+
\ }\n\
1122+
}\n\
1123+
") ;
1124+
File (".gitignore",
1125+
".DS_Store\n\
1126+
.merlin\n\
1127+
.bsb.lock\n\
1128+
npm-debug.log\n\
1129+
/lib/bs/\n\
1130+
/node_modules/\n\
1131+
*.bs.js\n\
1132+
") ;
1133+
File ("README.md",
1134+
"# Reason react starter\n\
1135+
\n\
1136+
## Run Project\n\
1137+
\n\
1138+
```sh\n\
1139+
npm install\n\
1140+
npm start\n\
1141+
# in another tab\n\
1142+
npm run server\n\
1143+
```\n\
1144+
\n\
1145+
View the app 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.\n\
1146+
\n\
1147+
To use a port other than 8000 set the `PORT` environment variable (`PORT=8080 npm run server`).\n\
1148+
\n\
1149+
## Build for Production\n\
1150+
\n\
1151+
```sh\n\
1152+
npm run clean\n\
1153+
npm run build\n\
1154+
npm run webpack:production\n\
1155+
```\n\
1156+
\n\
1157+
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`).\n\
1158+
\n\
1159+
**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.\n\
1160+
") ;
1161+
File ("webpack.config.js",
1162+
"const path = require(\"path\")\n\
1163+
const HtmlWebpackPlugin = require(\"html-webpack-plugin\")\n\
1164+
const outputDir = path.join(__dirname, \"build/\")\n\
1165+
\n\
1166+
const isProd = process.env.NODE_ENV === \"production\"\n\
1167+
\n\
1168+
module.exports = {\n\
1169+
\ entry: \"./src/Index.bs.js\",\n\
1170+
\ mode: isProd ? \"production\" : \"development\",\n\
1171+
\ devtool: \"source-map\",\n\
1172+
\ output: {\n\
1173+
\ path: outputDir,\n\
1174+
\ filename: \"Index.js\"\n\
1175+
\ },\n\
1176+
\ plugins: [\n\
1177+
\ new HtmlWebpackPlugin({\n\
1178+
\ template: \"src/index.html\",\n\
1179+
\ inject: false\n\
1180+
\ })\n\
1181+
\ ],\n\
1182+
\ devServer: {\n\
1183+
\ compress: true,\n\
1184+
\ contentBase: outputDir,\n\
1185+
\ port: process.env.PORT || 8000,\n\
1186+
\ historyApiFallback: true\n\
1187+
\ },\n\
1188+
\ module: {\n\
1189+
\ rules: [\n\
1190+
\ {\n\
1191+
\ test: /\\.css$/,\n\
1192+
\ use: [\"style-loader\", \"css-loader\"]\n\
1193+
\ }\n\
1194+
\ ]\n\
1195+
\ }\n\
1196+
}\n\
1197+
")]) ;
9981198
Dir ("tea", [
9991199
Dir ("src", [
10001200
File ("main.ml",

0 commit comments

Comments
 (0)