- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在将 create-react-app (CRA - v4) monorepo Webpack 设置迁移到由 vite 提供支持的 NX Monorepo。
我目前正试图弄清楚如何解决典型的
Uncaught ReferenceError: React is not defined
import { memo } from 'react';
我已经运行了删除所有
import React
的 linter声明,我会害怕通过成百上千的文件再次添加它。
vite.config.ts
import path from 'path';
import { defineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
import eslintPlugin from 'vite-plugin-eslint';
import react from '@vitejs/plugin-react';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [tsconfigPaths(), eslintPlugin(), react()],
resolve: {
alias: {
stream: 'stream-browserify',
'~': path.resolve(__dirname, 'src'),
},
},
server: {
open: true,
},
});
这是我的 tsconfig.json:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": true,
"skipLibCheck": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"noFallthroughCasesInSwitch": true
},
"include": ["./src"]
}
以及基础 tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"rootDir": ".",
"sourceMap": true,
"isolatedModules": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@schon/legacy-components/*": ["apps/app/src/components/*"],
"@schon/graphql/*": ["apps/app/src/graphql/*"],
"@schon/hooks/*": ["libs/components/src/hooks/*"],
"@schon/components/*": ["libs/components/src/ad/*"],
"@schon/legacy-components2/*": ["libs/components/src/stories/*"],
"@schon/theme": ["libs/components/src/styles/index.ts"],
"@schon/typings": ["libs/components/src/typings/index.ts"],
"@schon/utils": ["libs/components/src/utils/index.ts"],
"~/*": ["apps/app/src/*"]
}
},
"exclude": ["node_modules", "tmp"]
}
项目.json:
{
"root": "apps/app",
"projectType": "application",
"sourceRoot": "apps/app/src",
"tags": [],
"targets": {
"serve": {
"executor": "nx-plugin-vite:serve",
"options": {
"configFile": "apps/app/vite.config.ts",
"port": 3001,
"host": false,
"https": false
}
},
"preview": {
"executor": "nx-plugin-vite:preview",
"options": {
"configFile": "apps/app/vite.config.ts"
}
},
"build": {
"executor": "nx-plugin-vite:build",
"options": {
"outDir": "dist",
"configFile": "apps/app/vite.config.ts",
"watch": false,
"write": true,
"emitAtRootLevel": false,
"manifest": true
}
}
}
}
这是给我带来问题的文件之一(向我抛出 React 未定义):
import { memo } from 'react';
import { Container as MaterialContainer } from '@material-ui/core';
import { ThemeSpecs } from '../../../styles/theme';
type ContainerProps = {
children?:
| JSX.Element
| JSX.Element[]
| React.ReactNode
| React.ReactChildren;
className?: string;
};
/**
* Jose decided to wrap this up, in case we needed to apply a general styling to the container
* itself, and avoid repeating it in every other component.
*/
const Component: React.FC<ContainerProps> = (props) => (
<MaterialContainer
className={props.className}
fixed
style={{ paddingTop: ThemeSpecs.container.paddingTop }}
>
{props.children!}
</MaterialContainer>
);
type withContainerProps = {};
/**
* This is a HOC so we can use this to Containerize the imports back
* at root. This way we can choose which routes use Containers
* and which don't.
*/
export const withContainer = <P extends object>(
ComponentToContainer: React.ComponentType<P>
) =>
class WithContainer extends React.PureComponent<P & withContainerProps> {
render() {
return (
<Container>
<ComponentToContainer {...this.props} />
</Container>
);
}
};
export const Container = memo(Component) as typeof Component;
包.json
{
"scripts": {
"start": "nx serve",
"build": "nx build",
"test": "nx test"
},
"private": true,
"dependencies": {
"@apollo/client": "^3.5.6",
"@auth0/auth0-react": "^1.8.0",
"@aws-sdk/client-s3": "^3.44.0",
"@date-io/date-fns": "^2.11.0",
"@material-table/core": "^4.3.11",
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^4.11.2",
"@material-ui/lab": "^4.0.0-alpha.60",
"@material-ui/pickers": "^3.3.10",
"@material-ui/system": "^4.12.1",
"@nivo/calendar": "^0.74.0",
"@nivo/core": "^0.74.0",
"@nivo/line": "^0.74.0",
"@nivo/tooltip": "^0.74.0",
"@reach/router": "^1.3.4",
"auth0-js": "^9.18.0",
"aws-appsync-auth-link": "^3.0.7",
"aws-appsync-subscription-link": "^3.0.9",
"aws-sdk": "^2.1046.0",
"clsx": "^1.1.1",
"core-js": "^3.6.5",
"d3-array": "^3.1.1",
"date-fns": "^2.27.0",
"dotenv": "^10.0.0",
"exceljs": "^4.3.0",
"file-saver": "^2.0.5",
"formik": "^2.2.9",
"formik-persist": "^1.1.0",
"framer-motion": "^5.4.5",
"fraql": "^1.2.1",
"graphql": "^16.1.0",
"husky": "^7.0.4",
"immer": "^9.0.7",
"linkifyjs": "^3.0.4",
"lodash": "^4.17.21",
"logrocket": "^2.1.2",
"material-table": "^1.69.3",
"msw": "^0.36.3",
"password-validator": "^5.2.1",
"randomcolor": "^0.6.2",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-dropzone-uploader": "^2.11.0",
"react-elastic-carousel": "^0.11.5",
"react-error-boundary": "^3.1.4",
"react-google-docs-viewer": "^1.0.1",
"react-icons": "^4.3.1",
"react-intersection-observer": "^8.32.5",
"react-lazy-load-image-component": "^1.5.1",
"react-loading-skeleton": "^3.0.1",
"react-prerendered-component": "^1.2.4",
"regenerator-runtime": "0.13.7",
"stream-browserify": "^3.0.0",
"styled-components": "^5.3.3",
"suneditor": "^2.41.3",
"suneditor-react": "^3.3.1",
"sw-precache": "^5.2.1",
"tiny-slider-react": "^0.5.3",
"tslib": "^2.0.0",
"use-debounce": "^7.0.1",
"uuid": "^8.3.2",
"validate-password": "^1.0.4",
"yup": "^0.32.11"
},
"devDependencies": {
"@angular-devkit/schematics": "^13.0.4",
"@babel/core": "7.12.13",
"@babel/preset-typescript": "7.12.13",
"@nrwl/cli": "13.2.4",
"@nrwl/cypress": "13.2.4",
"@nrwl/eslint-plugin-nx": "13.2.4",
"@nrwl/jest": "13.2.4",
"@nrwl/linter": "13.2.4",
"@nrwl/node": "^13.2.4",
"@nrwl/nx-cloud": "latest",
"@nrwl/react": "13.2.4",
"@nrwl/storybook": "^13.3.0",
"@nrwl/tao": "^13.2.4",
"@nrwl/web": "13.2.4",
"@nrwl/workspace": "^13.2.4",
"@nxext/react": "^13.0.0",
"@snowpack/plugin-dotenv": "^2.2.0",
"@snowpack/plugin-react-refresh": "^2.5.0",
"@snowpack/plugin-typescript": "^1.2.1",
"@snowpack/web-test-runner-plugin": "^0.2.2",
"@storybook/addon-actions": "^6.4.9",
"@storybook/addon-essentials": "~6.3.0",
"@storybook/addon-knobs": "^6.4.0",
"@storybook/addon-links": "^6.4.9",
"@storybook/addon-storysource": "^6.4.9",
"@storybook/builder-webpack5": "~6.3.0",
"@storybook/manager-webpack5": "~6.3.0",
"@storybook/react": "~6.3.0",
"@svgr/webpack": "^5.4.0",
"@testing-library/react": "12.1.2",
"@testing-library/react-hooks": "7.0.2",
"@types/auth0-js": "^9.14.5",
"@types/chai": "^4.2.21",
"@types/jest": "27.0.2",
"@types/mocha": "^9.0.0",
"@types/node": "14.14.33",
"@types/react": "17.0.30",
"@types/react-dom": "17.0.9",
"@types/react-lazy-load-image-component": "^1.5.2",
"@types/snowpack-env": "^2.3.4",
"@types/tiny-slider-react": "^0.3.3",
"@types/uuid": "^8.3.3",
"@types/yup": "^0.29.13",
"@typescript-eslint/eslint-plugin": "~4.33.0",
"@typescript-eslint/parser": "~4.33.0",
"@vitejs/plugin-react": "^1.1.3",
"@web/test-runner": "^0.13.17",
"babel-jest": "27.2.3",
"babel-loader": "8.1.0",
"chai": "^4.3.4",
"cypress": "^8.3.0",
"eslint": "7.32.0",
"eslint-config-prettier": "8.1.0",
"eslint-plugin-cypress": "^2.10.3",
"eslint-plugin-import": "2.25.2",
"eslint-plugin-jsx-a11y": "6.4.1",
"eslint-plugin-react": "7.26.1",
"eslint-plugin-react-hooks": "4.2.0",
"jest": "27.2.3",
"nx-plugin-snowpack": "^0.3.0",
"nx-plugin-vite": "^1.1.0",
"prettier": "^2.3.1",
"react-test-renderer": "17.0.2",
"snowpack": "^3.8.8",
"storybook-theme-toggle": "^0.1.2",
"ts-jest": "27.0.5",
"typescript": "~4.4.3",
"url-loader": "^3.0.0",
"vite": "^2.7.1",
"vite-plugin-eslint": "^1.3.0",
"vite-preset-react": "^2.2.0",
"vite-tsconfig-paths": "^3.3.17"
}
}
nx.json:
{
"npmScope": "schon",
"affected": {
"defaultBase": "main"
},
"cli": {
"defaultCollection": "@nrwl/react"
},
"implicitDependencies": {
"package.json": {
"dependencies": "*",
"devDependencies": "*"
},
".eslintrc.json": "*"
},
"tasksRunnerOptions": {
"default": {
"runner": "@nrwl/nx-cloud",
"options": {
"cacheableOperations": ["build", "lint", "test", "e2e"],
}
}
},
"targetDependencies": {
"build": [
{
"target": "build",
"projects": "dependencies"
}
]
},
"generators": {
"@nrwl/react": {
"application": {
"style": "css",
"linter": "eslint",
"babel": true
},
"component": {
"style": "css"
},
"library": {
"style": "css",
"linter": "eslint"
}
}
},
"defaultProject": "app"
}
最佳答案
我还收到了ReferenceError: React is not defined
将我的应用程序从 create-react-app 移植到 vite 时。该问题源于 CRA 使用新的 jsx 运行时将 React 自动导入 jsx 文件。
Vite 默认不使用新的 jsx 运行时,但您可以通过安装 @vitejs/plugin-react
轻松添加它。 :
# install the plugin
yarn add @vitejs/plugin-react
...然后更新
vite.config.js
:
// vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()]
})
注:
@vitejs/plugin-react
还包括
react-refresh
所以你应该可以删除
@vitejs/plugin-react-refresh
如果你正在使用它。
关于javascript - 引用错误 : React is not defined - Migrating from CRA to Vite and NX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70519656/
我使用 CRA typescript 模板构建应用程序。 我在使用这段代码时,出现了解析错误。 export type { BaseModalProps } from "./BaseModalProp
我有一个使用 CRA 创建的 React 应用程序,它编译并运行良好。但是使用 yarn buld 制作并使用 serve -s build 提供的生产构建在控制台中显示以下错误: Uncaught
我正在尝试将 react-router 与 code splitting 一起使用在我的 create-react-app 应用程序中。 import loadSettings from 'bundl
我使用以下方法创建了一个 TypeScript React 应用程序: yarn create react-app my-app --template typescript 这样,我的项目编译为Bab
更新:我的用例主要是在 CI 上运行测试,但我通常想知道的是覆盖默认的 CRA Jest 参数。 我正在使用 Jest 运行测试, Create React App 附带的配置。它总是启动到交互模式:
我是Scrapy的新手,正在阅读教程。 跑了这个命令,出现了一些错误。 C:\Users\Sandra\Anaconda>scrapy shell 'http://scrapy.org' 特别是这个U
当使用 create-react-app 时,我的图片文件夹应该放在什么目录下? 在 src 或 public 中? 当我准备好运行 yarn build 时,一个或另一个会出现问题吗? 我听说只有放
所以我有这个带有多个页面的社交媒体应用程序,它都是通过创建带有上下文 api 的 react 应用程序和用于路由的 react-router 和用于后端的 laravel 创建的。现在,谷歌将无法索引
使用时 npx create-react-app appname , react-scripts安装的软件包包含一个 eslint 依赖项,其中包含“一组可查找常见错误的最小规则”。我想使用 pret
我正在使用react(使用create-react-app)创建一个Wordpress插件。我需要在不同的地方使用我的 React 应用程序,例如仪表板、用户配置文件、自定义管理页面、前端页面等。因此
我已经删除了 head 标签中的链接,但图标仍然被渲染,是否可以删除或更改? 最佳答案 您的网站图标位于project_root > public > favicon.ico。 | `--- publ
当我更新站点时,运行 npm run build 并将新文件上传到服务器,我仍在查看站点的旧版本。 没有 React,我可以看到带有缓存清除功能的网站的新版本。我这样做: 上一个文件 新文件 我怎
我有一个 CRA 应用程序,正在尝试使用 manual mocks Jest 的功能,但我的手动模拟似乎没有应用。 我使用 src 的基本网址(通过将其设置为 tsconfig.json )。简化,我
我想创建一个错误边界,以防止我的整个应用程序崩溃,而是通知用户出现问题并让他选择重新加载页面。我的错误边界组件: import React, { Fragment, PureComponent } f
我已经使用 create-react-app 创建了一个网站,现在由于 seo 的原因我想使用服务器端渲染。 经过长时间的研究,我发现了一个简单的工具 here . 它工作正常,但在我的代码中我有一个
我正在使用带有 typescript 和 enzyme 的 React 16,但 jest 没有读取我的适配器所在的 setupTests.ts 文件。如果我尝试在所有测试开始时配置适配器,它会工作,
我刚刚更新到最新的 create-react-app (web pack 5)。现在出现以下错误(我在这里只显示前 2 个错误)。任何帮助解释此错误的含义以及如何修复它的帮助将不胜感激。我认为问题不在
我在当前项目中不需要 PWA 功能。我怎样才能禁用它? 我阅读了CRA的文档,删除了src文件夹中的service-Worker.js,但我无法排除 list 文件和service-worker。 j
我安装了eslint-plugin-import-order并将其添加到 eslintConfig在 package.json . 我有错误: Definition for rule 'import/
我正在尝试将弹出的 create-react-app 迁移到 Webpack 5 并遇到此错误: Failed to compile. The "path" argument must be of t
我是一名优秀的程序员,十分优秀!