我想.scss在我的应用程序中添加支持,这是使用create-react-app创建的.
我确实弹出npm run eject并安装了必要的依赖项:npm install sass-loader node-sass --save-dev
在里面config/webpack.config.dev.js我添加到加载器这个片段:
{
test: /\.scss$/,
include: paths.appSrc,
loaders: ["style", "css", "scss"]
},
Run Code Online (Sandbox Code Playgroud)
所以loaders数组的开头现在看起来像这样:
loaders: [
// Process JS with Babel.
{
test: /\.(js|jsx)$/,
include: paths.appSrc,
loader: 'babel',
query: require('./babel.dev')
},
// "postcss" loader applies autoprefixer to our CSS.
// "css" loader resolves paths in CSS and adds assets as dependencies.
// "style" loader turns CSS into JS modules that inject <style> tags.
// In production, we …Run Code Online (Sandbox Code Playgroud) 我正在学习如何使用componentDidCatch().它看起来很直接.它可以工作,但仍然在视图上显示完整的错误堆栈.
在单独的文件中:
import React, { Component } from 'react'
class ErrorBoundary extends Component {
constructor(props) {
super(props);
this.state = {
hasError: false
}
}
componentDidCatch(error, info) {
console.log("Catching an error") // this is never logged
this.setState(state => ({...state, hasError: true }))
}
render() {
if (this.state.hasError) { return <div>Sorry, an error occurred</div> }
return this.props.children
}
}
export default ErrorBoundary
...
import React, { Component } from 'react'
class Foo extends Component {
render() {
return this.props.a.b; // …Run Code Online (Sandbox Code Playgroud) 带有 create-react-app 的 WebStorm 在/public/index.html文件中有一个文件夹路径/public标记为%PUBLIC_URL%.
它找不到目录,%PUBLIC_URL%因为它不存在,也不应该存在。
除了使用 抑制错误之外<!--suppress HtmlUnknownTarget -->,Webstorm 中是否还有一种方法可以将目录变量(如 , )别名%PUBLIC_URL%到/publicfor 开发中没有编辑器错误的根路径?
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with …Run Code Online (Sandbox Code Playgroud) 我在我的 Heroku 应用程序中定义了一个配置变量“BASE_URL”,我试图在我的 React 应用程序中访问它
process.env.BASE_URL
Run Code Online (Sandbox Code Playgroud)
但是当我 console.log 它时它给了我 undefined 因为它似乎不存在。如何在我的 React 应用程序中访问 Heroku 配置变量?
我不想使用create-react-app. 那么如何为一个简单的 React 应用程序配置一个最小的工作开发环境呢?
注意:我知道我可能只是在运行时将所有内容都包含为 JS(这是有据可查的),但我不想要这个,因为我仍然想要一个可用于生产的版本!
我不想要的:
我确实想:
基本上,我只想使用 React。没有它周围的所有花哨的其他东西!*
我只是问这个,因为官方 React 文档没有提到这种可能性。
注意:为那些想知道为什么我想要这个的人推理。
* 实际上,忽略这些方便的开发功能等听起来太疯狂了。但我声称这有正当的理由/用例。我的意思是,所有这些对我来说都是不可用的/破坏了东西,因为我正在尝试使用 React 构建浏览器扩展。
是的,我看到了这个非常相似的问题,但不幸的是,用户比我领先一步,答案只是针对他们问题的非常具体的答案。我想先了解一般情况,即我需要什么以及如何设置?
随着Create React App 3.0的发布以及浏览器列表的添加,添加对IE 11的支持变得微不足道,但是如果您不知道在哪里查找,则很难找到这些步骤。该文档列出了“受支持的浏览器”,并显示了默认的浏览器列表配置(https://facebook.github.io/create-react-app/docs/supported-browsers-features),但这并不是您所需要的全部。
使用生产设置检查browserl.ist时,您会发现IE 11的使用率比Edge仍然高(https://browserl.ist/?q=%3E0.2%25%2C+not+dead%2C+not+op_mini + all),使用默认的CRA 3应用程序,您将在main.chunk.js中收到语法错误,而在使用IE 11的应用程序中将收到其他错误。
在我的应用程序(基于 create-react-app 的 React)的源代码中,我使用了这样的 env 变量:process.env.REACT_APP_API_URL它们存储在我的.env.*文件中。
但是当我在 Cypress 下运行相同的应用程序时,process.env对象是空的。当 React 应用程序在 Cypress 下运行时,如何提供这些变量以在 React 应用程序中使用?
我知道我可以设置 Cypress env 变量,但这不是我想要的 - 这是一个不同的范围。
我目前有一个 monorepo,其中有两个 (+) 使用纱线工作区的包:
root
/packages
/common
/web
...
root/package.json
...
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": [
"**"
],
},
Run Code Online (Sandbox Code Playgroud)
web是一个带有 typescript 模板的普通 create-react-app。我有一些TS的代码common我想在使用web,但似乎CRA不支持编译的代码之外src,给我一个错误,当我尝试进口来自common于web:
../common/state/user/actions.ts 4:66
Module parse failed: Unexpected token (4:66)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import { UpdateUserParams } from './reducer';
|
> export const login = …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的团队创建一个自定义 cra 模板文件以供使用,但一直遇到一个问题,即我在运行npx create-react-app test-app --template my-custom-template.
internal/modules/cjs/loader.js:796
throw err;
^
Error: Cannot find module 'cra-template-my-custom-template'
Require stack:
- C:\Users\user\Desktop\cra-test-app\test-app\node_modules\react-scripts\scripts\init.js
- C:\Users\user\Desktop\cra-test-app\test-app\[eval]
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
at Function.resolve (internal/modules/cjs/helpers.js:80:19)
at module.exports (C:\Users\user\Desktop\cra-test-app\test-app\node_modules\react-scripts\scripts\init.js:110:13)
at [eval]:3:14
at Script.runInThisContext (vm.js:116:20)
at Object.runInThisContext (vm.js:306:38)
at Object.<anonymous> ([eval]-wrapper:9:26)
at Module._compile (internal/modules/cjs/loader.js:955:30)
at evalScript (internal/process/execution.js:80:25)
at internal/main/eval_string.js:23:3 {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\user\\Desktop\\cra-test-app\\test-app\\node_modules\\react-scripts\\scripts\\init.js',
'C:\\Users\\user\\Desktop\\cra-test-app\\test-app\\[eval]'
]
}
Run Code Online (Sandbox Code Playgroud)
模板本身目前相当简单,我认为这里的问题可能是我们的团队对某些内部包(它们是模板中的依赖项)使用了私有提要。此私有提要设置为使用 npm 作为上游提要。
我尝试使用该--template file:../path/to/template方法在本地测试模板并将模板发布到我们的内部提要。我可以使用 npm install 安装该软件包,因此该软件包肯定会在注册表中找到。
我还尝试根据其他一些建议刷新 npm 缓存并卸载 create-react-app。
我是否必须将模板发布到公共 npm 注册表才能使其工作?还是我错过了其他东西? …
我一直在使用 create-react-app 和我添加的其他一些库开发一个小应用程序。鉴于计划的架构和一些我想用 react-router 运行的测试,我希望能够使用 url 中的域 mame 在本地运行我的应用程序,这样而不是
本地主机:3000/
这将是
somedomain.com
我已经尝试了一些事情,比如弹出 ceate-react-app 但我不知道如何修改 webpack 配置来改变公共路径,或者如何在我的机器上设置某种代理,这样 somedomain.com 就相当于输入本地主机:3000
我不确定正确的方法是什么,我似乎还找不到任何可靠的信息。
create-react-app ×10
reactjs ×7
node.js ×2
cypress ×1
ecmascript-6 ×1
heroku ×1
monorepo ×1
npm ×1
proxy ×1
sass ×1
typescript ×1
webpack ×1
webstorm ×1