diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..a8ccaa1 --- /dev/null +++ b/.npmignore @@ -0,0 +1,26 @@ + +### Vim ### +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +### rescript +*.bs.js +*.gen.tsx +*.gen.js diff --git a/README.md b/README.md index 399db6f..c3c8692 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,51 @@ -# cra-template -create-react-app template with Reason/ReScript. +

+
+ +

+

+ Usage • + Project Structure • + Alternative +

+
+
+ +## Usage + +First run the below commands to start create new project: +```bash +npx create-react-app --template @reason-id/cra-template +``` + +## Project Structure + +``` +. +├── bsconfig.json +├── .gitignore +├── public +│   ├── favicon.ico +│   ├── index.html +│   ├── logo192.png +│   ├── logo512.png +│   ├── manifest.json +│   └── robots.txt +├── README.md +└── src + ├── App.css + ├── App.re + ├── index.css + ├── index.js + ├── index.re + ├── logo.svg + ├── reportWebVitals.js + └── Router + ├── Route.re + ├── Route.rei + └── Router.re +``` + +## Alternative +* [**Spin**](https://github.com/tmattio/spin)(we use [Router](https://github.com/reason-id/cra-template/tree/feat/v0.0.1/template/src/Router) from [Spin](https://github.com/tmattio/spin)) +* [**app-template-reason-react**](https://github.com/jihchi/app-template-reason-react) for [Snowpack](snowpack.dev) +* [**vite-reason-react-template**](https://github.com/tatchi/vite-reason-react-template) for [Vite](https://github.com/vitejs/vite) diff --git a/banner.svg b/banner.svg new file mode 100644 index 0000000..98ee321 --- /dev/null +++ b/banner.svg @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..e3c8e33 --- /dev/null +++ b/package.json @@ -0,0 +1,36 @@ +{ + "name": "@reason-id/cra-template", + "version": "0.0.1", + "keywords": [ + "react", + "create-react-app", + "reason-react", + "reasonml", + "rescript" + ], + "author": { + "name": "ri7nz", + "email": "ri7nz.labs@gmail.com", + "url": "https://rin.rocks/" + }, + "description": "Create-React-App Template for Reason/ReScript", + "license": "MIT", + "engines": { + "node": ">=10" + }, + "bugs": { + "url": "https://github.com/reason-id/cra-template/issues" + }, + "files": [ + "template", + "template.json" + ], + "repository": { + "type": "git", + "url": "https://github.com/reason-id/cra-template" + }, + "main": "template.json", + "publishConfig": { + "registry": "https://npm.pkg.github.com/" + } +} diff --git a/template.json b/template.json new file mode 100644 index 0000000..1c2b39e --- /dev/null +++ b/template.json @@ -0,0 +1,22 @@ +{ + "package": { + "scripts": { + "start:re": "bsb -make-world -w", + "start:cra": "react-scripts start", + "start": "run-p -ln start:*", + "prebuild": "bsb -clean-world -make-world", + "build": "react-scripts build", + "test": "react-scripts test", + "eject": "react-scripts eject" + }, + "dependencies": { + "@testing-library/jest-dom": "^5.11.0", + "@testing-library/react": "^10.4.3", + "@testing-library/user-event": "^12.0.11", + "web-vitals": "^0.2.2", + "bs-platform": "^8.2.0", + "reason-react": "^0.9.0", + "npm-run-all": "^4.1.5" + } + } +} diff --git a/template/.env b/template/.env new file mode 100644 index 0000000..cd6c868 --- /dev/null +++ b/template/.env @@ -0,0 +1 @@ +EXTEND_ESLINT=true diff --git a/template/.eslintignore b/template/.eslintignore new file mode 100644 index 0000000..811ae48 --- /dev/null +++ b/template/.eslintignore @@ -0,0 +1,3 @@ +*.bs.js +*.gen.tsx +*.gen.js diff --git a/template/README.md b/template/README.md new file mode 100644 index 0000000..d42d52e --- /dev/null +++ b/template/README.md @@ -0,0 +1,46 @@ +# Getting Started with Create React App with ReasonReact. + +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app) and `ReasonReact` template from [@reason-id/cra-template](https://github.com/reason-id/cra-template). + +## Available Scripts + +In the project directory, you can run: + +### `npm start` + +Runs the app in the development mode.\ +Open [http://localhost:3000](http://localhost:3000) to view it in the browser. + +The page will reload if you make edits.\ +You will also see any lint errors from `react-scripts` and `bs-platform` in the console. + +### `npm test` + +Launches the test runner in the interactive watch mode.\ +See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. + +### `npm run build` + +Builds the app for production to the `build` folder.\ +It correctly bundles React in production mode and optimizes the build for the best performance. + +The build is minified and the filenames include the hashes.\ +Your app is ready to be deployed! + +See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. + +### `npm run eject` + +**Note: this is a one-way operation. Once you `eject`, you can’t go back!** + +If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. + +Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. + +You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. + +## Learn More + +You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). + +To learn React, check out the [React documentation](https://reactjs.org/). diff --git a/template/bsconfig.json b/template/bsconfig.json new file mode 100644 index 0000000..f6be573 --- /dev/null +++ b/template/bsconfig.json @@ -0,0 +1,28 @@ +{ + "name": "your-awesom-cra-reason-project", + "reason": { + "react-jsx": 3 + }, + "sources": [ + { + "dir": "src", + "subdirs": true + } + ], + "package-specs": [ + { + "module": "commonjs", + "in-source": true + } + ], + "suffix": ".bs.js", + "namespace": true, + "ppx-flags": [], + "bs-dependencies": ["reason-react"], + "bs-dev-dependencies": [], + "refmt": 3, + "warnings": { + "error": "+5" + }, + "bsc-flags": ["-bs-super-errors", "-bs-no-version-header"] +} diff --git a/template/gitignore b/template/gitignore new file mode 100644 index 0000000..ee4873f --- /dev/null +++ b/template/gitignore @@ -0,0 +1,27 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* +### Reasonml ### +/lib + diff --git a/template/public/favicon.ico b/template/public/favicon.ico new file mode 100644 index 0000000..a11777c Binary files /dev/null and b/template/public/favicon.ico differ diff --git a/template/public/index.html b/template/public/index.html new file mode 100644 index 0000000..8b8e844 --- /dev/null +++ b/template/public/index.html @@ -0,0 +1,39 @@ + + + + + + + + + + + + + Your Application Title | @reason-id + + +
+ + + diff --git a/template/public/logo192.png b/template/public/logo192.png new file mode 100644 index 0000000..fc44b0a Binary files /dev/null and b/template/public/logo192.png differ diff --git a/template/public/logo512.png b/template/public/logo512.png new file mode 100644 index 0000000..a4e47a6 Binary files /dev/null and b/template/public/logo512.png differ diff --git a/template/public/manifest.json b/template/public/manifest.json new file mode 100644 index 0000000..de1bfb3 --- /dev/null +++ b/template/public/manifest.json @@ -0,0 +1,25 @@ +{ + "short_name": "your-app-name", + "name": "your-app-name", + "icons": [ + { + "src": "favicon.ico", + "sizes": "64x64 32x32 24x24 16x16", + "type": "image/x-icon" + }, + { + "src": "logo192.png", + "type": "image/png", + "sizes": "192x192" + }, + { + "src": "logo512.png", + "type": "image/png", + "sizes": "512x512" + } + ], + "start_url": ".", + "display": "standalone", + "theme_color": "#000000", + "background_color": "#fecf28" +} diff --git a/template/public/robots.txt b/template/public/robots.txt new file mode 100644 index 0000000..e9e57dc --- /dev/null +++ b/template/public/robots.txt @@ -0,0 +1,3 @@ +# https://www.robotstxt.org/robotstxt.html +User-agent: * +Disallow: diff --git a/template/src/App.css b/template/src/App.css new file mode 100644 index 0000000..a829dc9 --- /dev/null +++ b/template/src/App.css @@ -0,0 +1,42 @@ +.App { + min-height:100vh; + text-align: center; + background-color: #282c34; +} + +.App-logo { + height: 40vmin; + pointer-events: none; +} + +@media (prefers-reduced-motion: no-preference) { + .App-logo { + animation: App-logo-spin infinite 20s linear; + } +} + +.App-header { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + font-size: calc(10px + 2vmin); + color: white; +} + +.App-link { + color: #61dafb; +} + +code { + color: white; +} + +@keyframes App-logo-spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} diff --git a/template/src/App.re b/template/src/App.re new file mode 100644 index 0000000..584163b --- /dev/null +++ b/template/src/App.re @@ -0,0 +1,85 @@ +/* import module from external (javascript module_*/ +%raw +{j|require("./App.css")|j}; + +let logo = [%raw {j|require("./logo.svg")|j}]; + +/* an example implementation of router */ +module AppRouter = { + open Router; + [@react.component] + let make = () => { + let route = Router.useRouter(); + let currentRoute = + switch (route) { + | Some(Home) => "Home" + | Some(Greeting(greet)) => "Greeting: " ++ greet + | None => "Handle Notfound" + }; + <> + + {("Current Route: " ++ currentRoute)->React.string} + ; + }; +}; + +[@react.component] +let make = () => { + let (showRouter, setShowRouter) = React.useState(_ => false); +
+
+ logo + setShowRouter(current => !current)}> + "Click Me: for Show `AppRouter`"->React.string + +
+
+ {showRouter + ? + : <> +

+ "Edit "->React.string + "src/App.re"->React.string + " and save to reload."->React.string +

+ + "Learn React"->React.string + + + "fork me on github"->React.string + + } +
+
; +}; diff --git a/template/src/Router/Route.re b/template/src/Router/Route.re new file mode 100644 index 0000000..f978293 --- /dev/null +++ b/template/src/Router/Route.re @@ -0,0 +1,19 @@ +type t = + | Home + | Greeting(string); + +let fromUrl = (url: ReasonReactRouter.url) => + switch (url.path) { + | [] => Some(Home) + | ["greeting", greet] => Some(Greeting(greet)) + | _ => None + }; + +type t'; + +external make: string => t' = "%identity"; +external toString: t' => string = "%identity"; + +let home = "/"->make; +let greeting = greet => ("/greeting/" ++ greet)->make; +// nothing definition for notfound cause all of undefined route is `Notfound` diff --git a/template/src/Router/Route.rei b/template/src/Router/Route.rei new file mode 100644 index 0000000..c04d02d --- /dev/null +++ b/template/src/Router/Route.rei @@ -0,0 +1,13 @@ +type t = + | Home + | Greeting(string); + +let fromUrl: ReasonReactRouter.url => option(t); + +type t'; + +external make: string => t' = "%identity"; +external toString: t' => string = "%identity"; + +let home: t'; +let greeting: string => t'; diff --git a/template/src/Router/Router.re b/template/src/Router/Router.re new file mode 100644 index 0000000..aef024c --- /dev/null +++ b/template/src/Router/Router.re @@ -0,0 +1,37 @@ +let useRouter = () => ReasonReactRouter.useUrl()->Route.fromUrl; + +let push = route => route->Route.toString->ReasonReactRouter.push; +let replace = route => route->Route.toString->ReasonReactRouter.replace; + +// Component like `Link` from `react-router-dom` or similar. +module Link = { + [@react.component] + let make = (~route: Route.t', ~children, ~className=?) => { + let location = route->Route.toString; + + + if (!event->ReactEvent.Mouse.defaultPrevented + && event->ReactEvent.Mouse.button == 0 + && !event->ReactEvent.Mouse.altKey + && !event->ReactEvent.Mouse.ctrlKey + && !event->ReactEvent.Mouse.metaKey + && !event->ReactEvent.Mouse.shiftKey) { + event->ReactEvent.Mouse.preventDefault; + location->ReasonReactRouter.push; + } + }> + children + ; + }; +}; + +open Route; +// this is an extra/bonus cause `reason-react` isn't have `goBack` API in ReasonReactRouter. +let goBack = route => + switch (route) { + | Some(Home) => "im-from-home"->greeting->replace + | _ => home->replace + }; diff --git a/template/src/index.css b/template/src/index.css new file mode 100644 index 0000000..ec2585e --- /dev/null +++ b/template/src/index.css @@ -0,0 +1,13 @@ +body { + margin: 0; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', + 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', + sans-serif; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + +code { + font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', + monospace; +} diff --git a/template/src/index.js b/template/src/index.js new file mode 100644 index 0000000..afd7280 --- /dev/null +++ b/template/src/index.js @@ -0,0 +1,9 @@ +// this is a (proxy) from `index.re` (index.bs.js) cause `react-scripts` entry point is `index.js` +import "./index.bs.js"; +// we can move `reportWebVitals` to `index.re` but need to bind/interop it +import reportWebVitals from "./reportWebVitals"; + +// If you want to start measuring performance in your app, pass a function +// to log results (for example: reportWebVitals(console.log)) +// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals +reportWebVitals(); diff --git a/template/src/index.re b/template/src/index.re new file mode 100644 index 0000000..1e2c81b --- /dev/null +++ b/template/src/index.re @@ -0,0 +1,7 @@ +%raw +{j|require("./index.css")|j}; + +ReactDOMRe.renderToElementWithId( + , + "root", +); diff --git a/template/src/logo.svg b/template/src/logo.svg new file mode 100644 index 0000000..6b60c10 --- /dev/null +++ b/template/src/logo.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/template/src/reportWebVitals.js b/template/src/reportWebVitals.js new file mode 100644 index 0000000..5253d3a --- /dev/null +++ b/template/src/reportWebVitals.js @@ -0,0 +1,13 @@ +const reportWebVitals = onPerfEntry => { + if (onPerfEntry && onPerfEntry instanceof Function) { + import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { + getCLS(onPerfEntry); + getFID(onPerfEntry); + getFCP(onPerfEntry); + getLCP(onPerfEntry); + getTTFB(onPerfEntry); + }); + } +}; + +export default reportWebVitals;