Create React App (TypeScript) 環境構築
#Atomic_Design #React #TypeScript
#フロントエンド開発環境
セットアップ
code:setup.sh
yarn add react-router-dom lodash validator
# typescript
yarn add @types/react-router-dom
#storybook
npx sb init
yarn add @storybook/addon-storyshots --dev
yarn add react-test-renderer --dev
yarn add @storybook/addon-jest --dev
@babel/plugin-transform-react-jsx
Post-Processing CSS
https://github.com/postcss/postcss
autoprefixerがもともとセットアップされている。browserslistに追記するだけ。
CSS Modules
Running Tests
jestももともとセットアップされている
TypeScript設定
tsconfig
typescript設定
tsconfig.json
ソースディレクトリの設定
paths
モジュールインポート方法解決
https://www.typescriptlang.org/tsconfig#paths
Path aliases with TypeScript in Node.js
https://dev.to/larswaechter/path-aliases-with-typescript-in-nodejs-4353
@types
https://typescript-jp.gitbook.io/deep-dive/type-system/types
自前の型ファイル用に、@typesフォルダを作成しておく。
tsconfigの設定で自動的に読み込んでくれるみたい。
https://www.typescriptlang.org/tsconfig#typeRoots
パッケージ用?
---
flow
https://flow.org/en/docs/config/
型チェックツール
typescriptの場合は不要なので入れない。
---
Create React App 設定
環境変数
https://create-react-app.dev/docs/adding-custom-environment-variables/
変数にはREACT_APP_を接頭辞につける
環境変数ファイルは以下、←の順で、変数が優先されるので注意。
npm start:.env.development.local, .env.local, .env.development, .env
npm run build: .env.production.local, .env.local, .env.production, .env
npm test: .env.test.local, .env.test, .env (note .env.local is missing)
Publicフォルダ
imagesやcssなどwebpackで管理しない外部モジュール
再利用するならwebpackを使用する。
-----
config/paths.js
appBuild: resolveApp('')
ビルド先
public/index.html
ページ名設定
Deployment | Create React App
"homepage": "http://mywebsite.com/relativepath",
ビルドされたhtmlのパスを解決できるようにする。
CSS Modules
Adding a CSS Modules Stylesheet
ファイル名は[name].module.cssにする。
import styles from './Button.module.css'とcssをimportする
code:css
import React, { Component } from 'react';
import styles from './Button.module.css'; // Import css modules stylesheet as styles
import './another-stylesheet.css'; // Import regular stylesheet
class Button extends Component {
render() {
// reference as a js object
return <button className={styles.error}>Error Button</button>;
}
-------------------------------------------------------------
https://github.com/validatorjs/validator.js
validator
https://github.com/google/gts
TypeScript style guide, formatter, and linter
gtsを入れると競合するのかエラー発生する。
Browserslist
https://github.com/browserslist/browserslist
----
以下で、コンパイルエラー発生
./node_modules/@react-leaflet/core/esm/path.js 10:41
const options = props.pathOptions ?? {};
以下の対応をした
https://github.com/facebook/create-react-app/issues/9468#issuecomment-845884956
code:json
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
を以下に変更
code:json
"browserslist": [
">0.2%",
"not dead",
"not op_mini all"
],
------
create-react-appは
babel-loader 8.1.0に依存しているようなので、バージョンを明示的に指定した。
"babel-loader": "8.1.0",