首页 > 其他分享 >react脚手架搭建的几种方式

react脚手架搭建的几种方式

时间:2022-09-25 23:57:37浏览次数:66  
标签:npm -- app yarn react 脚手架 create 搭建

react脚手架创建几种方式

npm

首先通过 npm 方式全局安装 create-react-app

npm install -g create-react-app

查看是否安装完成

create-react-app --version
5.0.1

查看帮助信息


Usage: create-react-app <project-directory> [options]

Options:
  -V, --version                            output the version number
  --verbose                                print additional logs
  --info                                   print environment debug info
  --scripts-version <alternative-package>  use a non-standard version of react-scripts
  --template <path-to-template>            specify a template for the created project
  --use-pnp
 

js

创建一个 普通 react-npm 项目

create-react-app project-name
cd 项目名
npm start

ts

添加配置命令 --template typescript

create-react-app project-name --template typescript
cd 项目名
npm start

yarn

使用yarn方式安装之前检查yarn是否安装

yarn --verison

如果未安装yarn 使用该命令

npm install yarn -g

通过yarn方式安装 create-react-app

yarn install -g create-react-app

js

create-react-app project-name
cd 项目名
yarn start

ts

create-react-app project-name --template typescript
cd 项目名
yarn start

vite

局部安装vite

npm install vite -D

全局安装vite

npm install vite -g

执行该命令可以选择对应模板

npm init vite@latest

或者使用 yarn 方式安装也可以

js


npm init vite-app 项目名 --template react
cd 项目名
npm install
npm run dev
npm run buil

ts

安装完毕之后执行该命令

cd 项目名称
npm install
npm run dev

结果如下

less-loader

添加less-loader 可以参考

如果配置不生效请执行


npm run eject 

如果执行报错,请先提交本地git缓存内容。

执行成功之后请在config目录中在webpack.config.js文件中修改配置

/*该内容大概在 70多行,找不到请搜索 cssRegex 或者 cssModuleRegex */
const lessRegex = /\.(less)$/;
const lessModuleRegex = /\.module\.(less)$/;


/*添加loader配置  还是 搜索 cssRegex 或者 cssModuleRegex 找到配置,复制
css loader 的配置 ,然后将 匹配模块修改成 上面 lessRegex 和 lessMoudleRegex


{
              test: cssRegex,
              exclude: cssModuleRegex,
              use: getStyleLoaders({
                importLoaders: 1,
                sourceMap: isEnvProduction
                  ? shouldUseSourceMap
                  : isEnvDevelopment,
                modules: {
                  mode: 'icss',
                },
              }),
              // Don't consider CSS imports dead code even if the
              // containing package claims to have no side effects.
              // Remove this when webpack adds a warning or an error for this.
              // See https://github.com/webpack/webpack/issues/6571
              sideEffects: true,
            },
            // Adds support for CSS Modules (https://github.com/css-modules/css-modules)
            // using the extension .module.css
            {
              test: cssModuleRegex,
              use: getStyleLoaders({
                importLoaders: 1,
                sourceMap: isEnvProduction
                  ? shouldUseSourceMap
                  : isEnvDevelopment,
                modules: {
                  mode: 'local',
                  getLocalIdent: getCSSModuleLocalIdent,
                },
              }),
            },

*/

{
              test: lessRegex,
              exclude: cssModuleRegex,
              use: getStyleLoaders({
                importLoaders: 1,
                sourceMap: isEnvProduction
                  ? shouldUseSourceMap
                  : isEnvDevelopment,
                modules: {
                  mode: 'icss',
                },
              }),
              // Don't consider CSS imports dead code even if the
              // containing package claims to have no side effects.
              // Remove this when webpack adds a warning or an error for this.
              // See https://github.com/webpack/webpack/issues/6571
              sideEffects: true,
            },
            // Adds support for CSS Modules (https://github.com/css-modules/css-modules)
            // using the extension .module.css
            {
              test: lessModuleRegex,
              use: getStyleLoaders({
                importLoaders: 1,
                sourceMap: isEnvProduction
                  ? shouldUseSourceMap
                  : isEnvDevelopment,
                modules: {
                  mode: 'local',
                  getLocalIdent: getCSSModuleLocalIdent,
                },
              }),
            },





其他的配置不要动

标签:npm,--,app,yarn,react,脚手架,create,搭建
From: https://www.cnblogs.com/wuxin001/p/16729441.html

相关文章

  • React 入门学习笔记
    npxcreate-react-appdemo创建reactdemo,此命令行不需要提前安装create-react-app脚手架,创建demo后就会删除了vue微信小程序都是MVVM框架react是MVC框架jsx......
  • pipenv_使用pipenv搭建python项目环境
    使用pipenv搭建python项目环境进入pipenv的虚拟环境,然后运行python项目使用pipenv在当前文件夹创建虚拟环境......
  • 一文读透react精髓
    学和使用react有一年多了,最近想在梳理一下react基础知识,夯实基础,激流勇进~关于reacr-router,redux,redux-saga后续都会慢慢输出,希望各位看官老爷持续关注~~要是能给个赞......
  • React 函数式组件怎样进行优化
    前言目的本文只介绍函数式组件特有的性能优化方式,类组件和函数式组件都有的不介绍,比如key的使用。另外本文不详细的介绍API的使用,后面也许会写,其实想用好hooks还是......
  • 详细解读 React useCallback & useMemo
    前言阅读本文章需要对Reacthooks中useState和useEffect有基础的了解。我的这篇文章内有大致介绍在React项目中全量使用Hooks。useCallbackuseCallback的作......
  • Linux学习(一)从搭建自己的code-server开始
    前言某天碰巧看到了网页版的VSCode,发现居然有这种好东西,浏览器访问https://vscode.dev/,ipad终于能当生产力工具而不是爱奇艺播放器了o.O其实这个东西已经够用了,代码......
  • 1-VSCode搭建GD32开发环境
    一、使用VSCode开发GD32的原因1-单片机开发用的最多的IDE为Keil,而Keil为商用软件,并非开源,而且只支持windows环境,介于当前关系,有断供的风险在。2-其他IDE类似第1条。3-V......
  • Visual Studio 2022 开发 STM32 单片机 - 环境搭建点亮LED灯
    安装VS2022社区版软件选择基础的功能就好  安装VisualGDB软件(CSDN资源) 按照提示一步一步安装就好 VisualGDB激活软件(CSDN资源)将如下软件放在VisualGDB的安......
  • react-native 判断传入的日期是否是昨天
    constisYesterday=(theDate='2022-1-110:12:00')=>{theDate=newDate(theDate)vardate=(newDate());//当前时间vartoday=newDate(date.ge......
  • 带有服务器端渲染 (SSR) React 前端的 Umbraco v10
    带有服务器端渲染(SSR)React前端的Umbracov10CreatedwithDALL-E(OpenAI)介绍最近我想尝试UmbracoCMS是否可以与React前端集成,同时保持对SEO友好。如果......