|
11 | 11 | * **Eslint** helps practice standard coding styles
|
12 | 12 |
|
13 | 13 | ## Versions
|
14 |
| -* NextJs v9.3.6 |
15 |
| -* Redux-Toolkit v1.3.5 |
| 14 | +* NextJs v9.4.2 |
| 15 | +* Redux-Toolkit v1.3.6 |
16 | 16 | * Emotion v10
|
17 |
| -* Typescript v3.8.3 |
| 17 | +* Typescript v3.9.2 |
18 | 18 |
|
19 | 19 | ## Other Project Setup
|
20 | 20 | * **[[Nextjs_Ts_Eslint]](https://github.com/Project-Setup/Nextjs_Ts_Eslint)** NextJs, EmotionJs, Typescript
|
|
29 | 29 | npm install
|
30 | 30 | ```
|
31 | 31 | 1. remove unwanted files in `public/`, `src/`
|
32 |
| -2. modify `configs/` and add `.env/` |
| 32 | +2. add `.env` and other .env files |
33 | 33 | 3. preview dev progress on `http://localhost:3000/`
|
34 | 34 | ```sh
|
35 | 35 | npm run dev
|
36 | 36 | ```
|
37 |
| -4. export to `docs/` for GIthub Page deploy |
| 37 | +4. export to `docs/` for Github Page deploy |
38 | 38 | ```sh
|
39 | 39 | npm run export
|
40 | 40 | ```
|
|
108 | 108 | "skipLibCheck": true,
|
109 | 109 | "strict": true,
|
110 | 110 | "target": "esnext",
|
111 |
| - "forceConsistentCasingInFileNames": true |
| 111 | + "forceConsistentCasingInFileNames": true, |
| 112 | + "baseUrl": "./src" |
112 | 113 | },
|
113 | 114 | "exclude": [
|
114 | 115 | "node_modules",
|
|
129 | 130 | ### [Eslint and Prettier](https://dev.to/robertcoopercode/using-eslint-and-prettier-in-a-typescript-project-53jb)
|
130 | 131 | 1.
|
131 | 132 | ```sh
|
132 |
| - npm i -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react |
| 133 | + npm i -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react eslint-import-resolver-typescript |
133 | 134 | npm i -D eslint-config-airbnb eslint-plugin-jsx-a11y eslint-plugin-import eslint-plugin-react-hooks
|
134 | 135 | npm i -D prettier eslint-config-prettier eslint-plugin-prettier
|
135 | 136 | ```
|
|
205 | 206 | node: {
|
206 | 207 | extensions: ['.js', '.jsx', '.ts', '.tsx'],
|
207 | 208 | },
|
| 209 | + typescript: {}, |
208 | 210 | },
|
209 | 211 | react: {
|
210 | 212 | version: 'detect', // Tells eslint-plugin-react to automatically detect the version of React to use
|
|
268 | 270 | '<rootDir>/__mocks__/mocks.js',
|
269 | 271 | '\\.(css|less|scss)$': '<rootDir>/__mocks__/mocks.js',
|
270 | 272 | },
|
| 273 | + moduleDirectories: ['node_modules', 'src'], |
271 | 274 | };
|
272 | 275 | ```
|
273 | 276 | 5. create `babel.config.js`
|
|
280 | 283 | ```js
|
281 | 284 | import Enzyme from 'enzyme';
|
282 | 285 | import Adapter from 'enzyme-adapter-react-16';
|
| 286 | + import { join } from 'path'; |
| 287 | + import { loadEnvConfig } from 'next/dist/lib/load-env-config'; |
| 288 | +
|
| 289 | + // to load '.env' files in test |
| 290 | + loadEnvConfig(join(__dirname, '.../')); |
283 | 291 |
|
284 | 292 | Enzyme.configure({ adapter: new Adapter() });
|
285 | 293 | ```
|
|
349 | 357 | ### [Deploy to Github Pages](https://github.com/zeit/next.js/issues/3335#issuecomment-489354854)
|
350 | 358 | (deploy to /docs intead of using gh-pages branch; replace `{folder}` with the project name in github repo)
|
351 | 359 |
|
352 |
| -1. create `LINK_PREFIX` in `next.config.js` |
| 360 | +1. add `.env.production` |
| 361 | + ```sh |
| 362 | + NEXT_PUBLIC_LINK_PREFIX=/{folder} |
| 363 | + ``` |
| 364 | +2. create `LINK_PREFIX` in `next.config.js` |
353 | 365 | ```js
|
354 |
| - const isProd = process.env.NODE_ENV === 'production'; |
355 |
| - const prodAssetPrefix = '/{folder}'; |
| 366 | + const LINK_PREFIX = process.env.NEXT_PUBLIC_LINK_PREFIX || ''; |
356 | 367 | module.exports = () => ({
|
357 |
| - env: { |
358 |
| - LINK_PREFIX: isProd ? prodAssetPrefix : ''; |
359 |
| - }, |
360 |
| - assetPrefix: isProd ? prodAssetPrefix : '';, |
| 368 | + assetPrefix: LINK_PREFIX, |
361 | 369 | });
|
362 | 370 | ```
|
363 |
| -2. change `as` prop in `next/Link` to add `linkPrefix`, similar to `src/features/link/Link.tsx` in the example setup |
364 |
| -3. change `scripts` in `package.json` |
| 371 | +3. change `as` prop in `next/Link` to add `linkPrefix`, similar to `src/features/link/Link.tsx` in the example setup |
| 372 | +4. change `scripts` in `package.json` |
365 | 373 | ```json
|
366 | 374 | {
|
367 | 375 | "scripts": {
|
|
391 | 399 | };
|
392 | 400 | ```
|
393 | 401 |
|
394 |
| -### Dotenv |
395 |
| -1. |
396 |
| - ```sh |
397 |
| - npm i -S dotenv |
398 |
| - ``` |
399 |
| -2. add `.env/development.env` and `.env/production.env` |
400 |
| -3. add `config/env.mapping.js` |
401 |
| - ```js |
402 |
| - /* eslint-disable @typescript-eslint/no-var-requires */ |
403 |
| - const path = require('path'); |
404 |
| - require('dotenv').config({ |
405 |
| - path: path.join(__dirname, `../.env/${process.env.NODE_ENV}.env`), |
406 |
| - }); |
407 |
| -
|
408 |
| - // env mapping for exposure to client |
409 |
| - module.exports = {/* EXAMPLE_VAR: process.env.EXAMPLE */}; |
410 |
| - ``` |
411 |
| -4. add to `jest/jest.setup.js` |
412 |
| - ```js |
413 |
| - // ... |
414 |
| - import envMapping from '../configs/env.mapping'; |
415 |
| - Object.assign(process.env, envMapping); |
416 |
| - // ... |
417 |
| - ``` |
418 |
| -
|
419 | 402 | <br/>
|
420 | 403 |
|
421 | 404 | ### [Redux-Toolkit](https://redux-toolkit.js.org/)
|
|
439 | 422 | 2. change `next.config.js`
|
440 | 423 | ```js
|
441 | 424 | const isProd = process.env.NODE_ENV === 'production';
|
| 425 | + const FOLDER = LINK_PREFIX && LINK_PREFIX.substring(1); |
442 | 426 |
|
443 | 427 | // tranfrom precache url for browsers that encode dynamic routes
|
444 | 428 | // i.e. "[id].js" => "%5Bid%5D.js"
|
|
458 | 442 | // service worker
|
459 | 443 | pwa: {
|
460 | 444 | disable: !isProd,
|
461 |
| - subdomainPrefix: proces.env.LINK_PREFIX, |
| 445 | + subdomainPrefix: LINK_PREFIX, |
462 | 446 | dest: 'public',
|
463 | 447 | navigationPreload: true,
|
464 | 448 | },
|
|
0 commit comments