Create React App v3 学习笔记 #87
zhangyu1818
announced in
zh-cn
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
不知不觉中,
Create React App
已经从v1到了v3,今天又再一次阅读了文档,将一些常用的配置记录下来创建App
npx
npm
npm
版本大于6可用yarn
template
新版
Create React App
提供了--template [template-name]
的命令行选项如果没有选择
template
,会使用默认template
创建template
会被命名为cra-template-[template-name]
,但是你只需要在命令行输入[template-name]
比如创建
Typescript
的应用,只需要在命令行添加--template typescript
选择包管理器
CLI
默认会选择yarn
作为包管理工具,如果你想使用npm
,你可以在命令行添加--use-npm
,如:自动格式化代码
添加
git
钩子来使用prettier
格式化代码也可以使用
yarn
husky
可以让npm script
使用githookslint-staged
可以在暂存文件的时候运行脚本prettier
在提交前格式化代码添加下列字段到
package.json
接下来添加
lint-staged
字段到package.json
现在每当你提交代码时,
prettier
会自动格式化代码添加自定义环境变量
添加
.env
文件,在这个文件里定义的变量是自定义变量自定义变量必须以
REACT_APP_
开头,如在代码中可以使用
process.env.REACT_APP_SITE_NAME
来使用在
public/index.html
文件中使用自定义变量更多参考
dotenv
文档添加Sass的支持
npm install node-sass --save # or yarn add node-sass
引入
sass
文件的时候可以通过添加SASS_PATH
来使用绝对路径引入如果你设置了
SASS_PATH=node_modules:src
,可以像一下方式import
CSS Modules
约定名字为
[name].module.css
或者使用sass
后名为[name].module.scss
or[name].module.sass
的文件为CSS Modules
文件在开发环境使用
HTTPS
如果接口是
https
的,代理是http
就发不过去,好在Create React App
提供了这个功能(虽然chrome会提示不安全)修改
package.json
中的npm start
脚本或者添加
HTTPS=true
到.env
文件绝对路径的组件引入
因为没有
webpack
的alias
的配置,所以引入很麻烦,比如../../../Components/
现在可以在
jsconfig.json
或者tsconfig.json
中添加配置来解决这个问题jsconfig.json
如果使用
Typescript
,就在tsconfig.json
文件中配置现在,就可以使用绝对路径引入了
Beta Was this translation helpful? Give feedback.
All reactions