├── .eslintrc ├── .gitignore ├── .nowignore ├── .storybook ├── addons.js ├── config.js ├── preview-head.html └── webpack.config.js ├── .vscode └── settings.json ├── .yarnrc ├── README.md ├── api └── time.ts ├── config-overrides.js ├── jest.config.js ├── jest.cra.config.js ├── jest.node.config.js ├── lib ├── app-name.ts ├── current-time.test.ts └── current-time.ts ├── package.json ├── public ├── favicon.ico ├── index.html └── manifest.json ├── src ├── App.css ├── App.test.tsx ├── App.tsx ├── index.css ├── index.tsx ├── react-app-env.d.ts └── serviceWorker.ts ├── tsconfig.json └── yarn.lock /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "parser": "@typescript-eslint/parser", 4 | "extends": [ 5 | "react-app", 6 | "plugin:@typescript-eslint/recommended", 7 | "prettier", 8 | "prettier/@typescript-eslint" 9 | ], 10 | "plugins": ["@typescript-eslint"], 11 | "parserOptions": { 12 | "ecmaFeatures": { 13 | "jsx": true 14 | }, 15 | "useJSXTextNode": true 16 | }, 17 | "env": { 18 | "browser": true, 19 | "commonjs": true, 20 | "es6": true, 21 | "node": true, 22 | "jest": true 23 | }, 24 | "settings": { 25 | "react": { 26 | "version": "16.8" 27 | } 28 | }, 29 | "rules": { 30 | "@typescript-eslint/explicit-function-return-type": 0 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | node_modules/ 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | coverage/ 10 | 11 | # production 12 | build/ 13 | 14 | # misc 15 | .DS_Store 16 | .env 17 | storybook-static 18 | 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | -------------------------------------------------------------------------------- /.nowignore: -------------------------------------------------------------------------------- 1 | **/*.test.ts 2 | **/*.spec.ts 3 | .vscode 4 | .storybook -------------------------------------------------------------------------------- /.storybook/addons.js: -------------------------------------------------------------------------------- 1 | import '@storybook/addon-storysource/register'; 2 | import '@storybook/addon-knobs/register'; 3 | import '@storybook/addon-actions/register'; 4 | import '@storybook/addon-links/register'; 5 | import '@storybook/addon-a11y/register'; 6 | -------------------------------------------------------------------------------- /.storybook/config.js: -------------------------------------------------------------------------------- 1 | import { configure } from '@storybook/react'; 2 | 3 | const req = require.context('../src', true, /\.stories\.tsx$/); 4 | 5 | function loadStories() { 6 | req.keys().forEach(filename => req(filename)); 7 | } 8 | 9 | configure(loadStories, module); 10 | -------------------------------------------------------------------------------- /.storybook/preview-head.html: -------------------------------------------------------------------------------- 1 | 5 | -------------------------------------------------------------------------------- /.storybook/webpack.config.js: -------------------------------------------------------------------------------- 1 | module.exports = ({ config, mode }) => { 2 | // remove the linting config 3 | config.module.rules = config.module.rules.filter(x => x.enforce !== 'pre'); 4 | config.module.rules.push({ 5 | test: /\.(ts|tsx)$/, 6 | use: [ 7 | { 8 | loader: require.resolve('awesome-typescript-loader'), 9 | }, 10 | { 11 | loader: require.resolve('react-docgen-typescript-loader'), 12 | }, 13 | ], 14 | }); 15 | config.resolve.extensions.push('.ts', '.tsx'); 16 | return config; 17 | }; 18 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "eslint.validate": [ 3 | "javascript", 4 | "javascriptreact", 5 | "typescript", 6 | "typescriptreact" 7 | ], 8 | "eslint.enable": true, 9 | "editor.tabSize": 2, 10 | "search.exclude": { 11 | "**/node_modules": true, 12 | "**/bower_components": true, 13 | "**/packages/*/lib/**": true, 14 | "**/temp/**": true 15 | } 16 | } -------------------------------------------------------------------------------- /.yarnrc: -------------------------------------------------------------------------------- 1 | --add.ignore-engines true 2 | --install.ignore-engines true 3 | --upgrade-interactive.ignore-engines true 4 | --upgrade-interactive.latest true -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cra-monorepo 2 | 3 | This is a starter for a Zeit [Now 2.0](https://zeit.co/now) based monorepo containing a Create React App frontend + Node lambda based backend. 4 | 5 | Example deployment: https://cra-monorepo.ctrlplusb.now.sh/ 6 | 7 | ## Features 8 | 9 | - Frontend: Create React App with React Fast Refresh and Storybook 10 | - Backend: Node lambdas 11 | - Shared code between frontend and backend 12 | - Fully TypeScript'ed with same TSConfig across entire codebase 13 | - Consistent ESLint with Typescript parser across entire codebase 14 | - Run Jest from the root 15 | - Prettier configuration 16 | - Yarn 17 | 18 | ## Recommendations 19 | 20 | Highly recommend using VSCode with the Prettier plugin. 🥰 21 | 22 | ## Getting started 23 | 24 | ```bash 25 | git clone https://github.com/ctrlplusb/cra-monorepo 26 | cd cra-monorepo 27 | yarn install 28 | ``` 29 | 30 | ## Commands 31 | 32 | Development: 33 | 34 | ```bash 35 | yarn start 36 | ``` 37 | 38 | Deployment: 39 | 40 | ```bash 41 | yarn deploy 42 | ``` 43 | 44 | Lint: 45 | 46 | ```bash 47 | yarn lint 48 | ``` 49 | 50 | Storybook: 51 | 52 | ```bash 53 | yarn storybook 54 | ``` 55 | 56 | Test: 57 | 58 | ```bash 59 | yarn test 60 | ``` 61 | -------------------------------------------------------------------------------- /api/time.ts: -------------------------------------------------------------------------------- 1 | import { IncomingMessage, ServerResponse } from 'http'; 2 | import currentTime from '../lib/current-time'; 3 | 4 | export default (req: IncomingMessage, res: ServerResponse) => { 5 | res.end(currentTime()); 6 | }; 7 | -------------------------------------------------------------------------------- /config-overrides.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable @typescript-eslint/no-var-requires */ 2 | 3 | const path = require('path'); 4 | const { 5 | addBabelPlugin, 6 | addWebpackPlugin, 7 | babelInclude, 8 | disableEsLint, 9 | override, 10 | removeModuleScopePlugin, 11 | } = require('customize-cra'); 12 | // TODO: Replace with official plugin when it is supported 13 | const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin'); 14 | 15 | module.exports = override( 16 | // Add support for React Fast Refresh 17 | process.env.NODE_ENV === 'development' 18 | ? addBabelPlugin('react-refresh/babel') 19 | : undefined, 20 | process.env.NODE_ENV === 'development' 21 | ? addWebpackPlugin(new ReactRefreshPlugin()) 22 | : undefined, 23 | // Add support for transpiling local package imports 24 | babelInclude([path.resolve('src'), path.resolve('lib')]), 25 | // Disable CRA lint in favour of project lint 26 | disableEsLint(), 27 | // Allow relative imports outside of src 28 | removeModuleScopePlugin(), 29 | ); 30 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | projects: ['jest.node.config.js', 'jest.cra.config.js'], 3 | }; 4 | -------------------------------------------------------------------------------- /jest.cra.config.js: -------------------------------------------------------------------------------- 1 | /** 2 | * I have done this "re-export" of the react-scripts jest configuration so that 3 | * we are able to leverage Jest projects based execution from the root of the 4 | * repo. 5 | */ 6 | 7 | /* eslint-disable @typescript-eslint/no-var-requires */ 8 | 9 | const createJestConfig = require('react-scripts/scripts/utils/createJestConfig'); 10 | 11 | const rootDir = __dirname; 12 | const resolveReactScriptsModule = reactScriptsPath => 13 | require.resolve(`react-scripts/${reactScriptsPath}`); 14 | const isEjecting = false; 15 | 16 | module.exports = { 17 | ...createJestConfig(resolveReactScriptsModule, rootDir, isEjecting), 18 | displayName: 'cra', 19 | }; 20 | -------------------------------------------------------------------------------- /jest.node.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | displayName: 'node', 3 | preset: 'ts-jest', 4 | testEnvironment: 'node', 5 | testPathIgnorePatterns: ['/node_modules/', '/src/'], 6 | }; 7 | -------------------------------------------------------------------------------- /lib/app-name.ts: -------------------------------------------------------------------------------- 1 | export default 'cra-monorepo'; 2 | -------------------------------------------------------------------------------- /lib/current-time.test.ts: -------------------------------------------------------------------------------- 1 | import currentTime from './current-time'; 2 | 3 | test('returns in the HH:MM format', () => { 4 | const actual = currentTime(); 5 | 6 | expect(actual).toMatch(/^[\d]{2}:[\d]{2}$/); 7 | }); 8 | -------------------------------------------------------------------------------- /lib/current-time.ts: -------------------------------------------------------------------------------- 1 | export default function currentTime(): string { 2 | const now = new Date(); 3 | return `${now 4 | .getHours() 5 | .toString() 6 | .padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`; 7 | } 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "name": "cra-monorepo", 4 | "engines": { 5 | "node": "12" 6 | }, 7 | "scripts": { 8 | "build-storybook": "build-storybook -s public", 9 | "build": "NODE_ENV=development SKIP_PREFLIGHT_CHECK=true react-app-rewired build", 10 | "deploy": "now", 11 | "dev": "FORCE_COLOR=true BROWSER=none SKIP_PREFLIGHT_CHECK=true react-app-rewired start | cat", 12 | "lint": "eslint --ext .ts --ext .tsx api src lib", 13 | "start": "now dev", 14 | "storybook": "start-storybook -p 9009 -s public", 15 | "test": "jest" 16 | }, 17 | "dependencies": { 18 | "react": "^16.13.1", 19 | "react-dom": "^16.13.1", 20 | "react-scripts": "^3.4.1" 21 | }, 22 | "devDependencies": { 23 | "@babel/core": "^7.9.6", 24 | "@babel/plugin-syntax-flow": "^7.8.3", 25 | "@hot-loader/react-dom": "^16.13.0", 26 | "@pmmmwh/react-refresh-webpack-plugin": "^0.2.0", 27 | "@storybook/addon-a11y": "^5.3.18", 28 | "@storybook/addon-actions": "^5.3.18", 29 | "@storybook/addon-info": "^5.3.18", 30 | "@storybook/addon-knobs": "^5.3.18", 31 | "@storybook/addon-links": "^5.3.18", 32 | "@storybook/addon-storysource": "^5.3.18", 33 | "@storybook/addons": "^5.3.18", 34 | "@storybook/react": "^5.3.18", 35 | "@testing-library/jest-dom": "^5.5.0", 36 | "@testing-library/react": "^10.0.4", 37 | "@types/jest": "^25.2.1", 38 | "@types/node": "^13.13.5", 39 | "@types/react": "^16.9.34", 40 | "@types/react-dom": "^16.9.7", 41 | "@types/storybook__addon-info": "^5.2.1", 42 | "@typescript-eslint/eslint-plugin": "^2.31.0", 43 | "@typescript-eslint/parser": "^2.31.0", 44 | "awesome-typescript-loader": "^5.2.1", 45 | "babel-core": "^7.0.0-bridge.0", 46 | "babel-eslint": "^10.1.0", 47 | "babel-jest": "^26.0.1", 48 | "babel-loader": "^8.1.0", 49 | "customize-cra": "^0.9.1", 50 | "eslint": "^6.8.0", 51 | "eslint-config-prettier": "^6.11.0", 52 | "eslint-config-react-app": "^5.2.1", 53 | "eslint-plugin-flowtype": "^4.7.0", 54 | "eslint-plugin-import": "^2.20.2", 55 | "eslint-plugin-jsx-a11y": "^6.2.3", 56 | "eslint-plugin-prettier": "^3.1.3", 57 | "eslint-plugin-react": "^7.19.0", 58 | "eslint-plugin-react-hooks": "^4.0.0", 59 | "jest": "^26.0.1", 60 | "jest-environment-jsdom": "^26.0.1", 61 | "prettier": "^2.0.5", 62 | "react-app-rewired": "^2.1.6", 63 | "react-docgen-typescript-loader": "^3.7.2", 64 | "react-docgen-typescript-webpack-plugin": "^1.1.0", 65 | "react-refresh": "^0.8.2", 66 | "ts-jest": "^25.5.0", 67 | "typescript": "3.8.3" 68 | }, 69 | "prettier": { 70 | "semi": true, 71 | "singleQuote": true, 72 | "trailingComma": "all" 73 | }, 74 | "browserslist": { 75 | "production": [ 76 | ">0.2%", 77 | "not dead", 78 | "not op_mini all" 79 | ], 80 | "development": [ 81 | "last 1 chrome version", 82 | "last 1 firefox version", 83 | "last 1 safari version" 84 | ] 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ctrlplusb/cra-monorepo/37c971efc73b1f3581d2fcba429121808b9eb4ae/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 12 | 13 | 22 |The time on the server is {serverDate}
19 |The time on client is {currentTime()}
20 |