我正在使用create-react-app。我想要反应路由器基本代码分割,但我想获取用户在浏览器中打开的第一个块,然后在后台异步获取其他块
路线
const HomeModule = React.lazy(() => import('./modules/ft_home_module/src/main'));
const AuthModule = React.lazy(() => import('./modules/ft_auth_module/src/main'));
const ProfileModule = React.lazy(() => import('./modules/ft_profile_module/src/main'));
const MerchantModule = React.lazy(() => import('./modules/ft_merchant_module/src/main'));
<Route path="/home" component={HomeModule} />
<Route path="/auth" component={AuthModule} />
<Route path="/profile" component={ProfileModule} />
<Route path="/merchant" component={MerchantModule} />
Run Code Online (Sandbox Code Playgroud)
假设,如果用户在浏览器中打开 /home,则在加载第一个块之后将首先加载 home 块,并在后台异步调用其他块
所需输出
/home在浏览器中打开实际上我正在通过lighthouse chrome 扩展测试性能。路由器基本代码分割为我提供了第一页的良好性能,但是当我打开第二页时,它需要时间,但不应该花费时间。我认为如果我们在加载第一个块后在后台异步获取其他块,这是可能的
reactjs code-splitting create-react-app react-router-dom code-splitting-async
我正在尝试将参数传递给我的反应项目。我用 create-react-app 设置它。
我像这样设置 package.json :
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --open --mode development",
"build": "webpack --mode production"
},
Run Code Online (Sandbox Code Playgroud)
索引.js:
console.log(process.argv)
Run Code Online (Sandbox Code Playgroud)
我启动它并传入我的参数:
yarn start --serverPort=3531
Run Code Online (Sandbox Code Playgroud)
但 process.argv 只是一个空数组。如何将参数传递给反应应用程序?
我创建了一个 React 应用程序,并使用 Docker 在 Azure DevOps 中设置了一个完全工作的管道来构建、测试和发布我的应用程序。
我的管道 yaml 文件中的片段:
#----------------------------------------------------------------------
# Build the docker image
#----------------------------------------------------------------------
- task: Docker@2
inputs:
containerRegistry: 'XXXcontainerrepo'
repository: 'XXX'
command: 'build'
Dockerfile: '**/production.Dockerfile'
tags: |
$(Build.BuildNumber)
arguments: '--build-arg buildNumber="$(Build.BuildNumber)"'
displayName: 'Docker build'
#----------------------------------------------------------------------
# Export and publish test results
#----------------------------------------------------------------------
- script: |
export id=$(docker images --filter "label=test=$(Build.BuildNumber)" -q | head -1)
echo "Container ID: ${id}"
docker create --name testcontainer $id
docker cp testcontainer:/app/coverage ./coverage
docker cp testcontainer:/app/junit.xml ./junit.xml
docker rm testcontainer
displayName: …Run Code Online (Sandbox Code Playgroud) 我已经使用 Create-React-App 版本 3.4.1 启动了我的应用程序,并使用 Craco 版本 5.6.4 对 CRA 提供的 Webpack 配置进行修改。由于特定的客户期望超出了我的控制范围,我需要最终确定build/js/main.js文件build/css/main.css结构。我已经成功地与 Craco 一起根据需要修改文件输出,只是我最终得到了 CSS 文件的附加副本,并添加了块哈希。
这是我的craco.config.js:
const { POSTCSS_MODES } = require('@craco/craco');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
style: {
postcss: {
mode: POSTCSS_MODES.file,
},
},
webpack: {
configure: {
optimization: {
splitChunks: {
cacheGroups: {
default: false,
},
},
runtimeChunk: false,
},
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
}),
new MiniCssExtractPlugin({
filename: 'css/[name].css',
}),
],
output: …Run Code Online (Sandbox Code Playgroud) create-react-app 3.4.1出于测试/学习的目的,我使用的是 附带的弹出版本css-loader 3.4.2,并且我尝试重现这些结果,其中 css 选择器以短横线大小写编写,但 js 样式对象将它们转换为驼峰命名法:
样式.css
.foo-baz {
color: red;
}
.bar {
color: blue;
}
Run Code Online (Sandbox Code Playgroud)
索引.js
import styles from './styles.css';
console.log(styles);
// expected result: { fooBaz, bar }
// actual, default results: { foo-baz, bar }
Run Code Online (Sandbox Code Playgroud)
根据3.0.0 版本中的css-loader变更日志:
exportLocalsStyle选项被删除以支持localsConvention选项,它也只接受值(如果您之前的值是并且如果您之前的值是,则{String}使用值)'camelCase'true'asIs'false
所以我尝试这样做:
webpack.config.js
...
{
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,
sourceMap: isEnvProduction && shouldUseSourceMap,
localsConvention: "camelCase", // my only addition …Run Code Online (Sandbox Code Playgroud) 好吧,我一整天都在绞尽脑汁,但仍然找不到一个优雅的解决方案。我创建了一个 React 应用程序,它对后端的 Django 服务器进行一些 API 调用。我创建了一个 settings.js 文件,该文件根据应用程序是在开发中还是在生产中运行来更改 API url。settings.js 文件如下所示:
let API_SERVER_VAL = '';
let MEDIA_SERVER_VAL = '';
let FRONTEND_SERVER_VAL = '';
switch (process.env.NODE_ENV) {
case 'development':
API_SERVER_VAL = 'http://localhost:8000';
MEDIA_SERVER_VAL = 'http://localhost:8000';
FRONTEND_SERVER_VAL = 'http://localhost:3000';
break;
case 'production':
API_SERVER_VAL = process.env.API_SERVER;
MEDIA_SERVER_VAL = process.env.API_SERVER;
FRONTEND_SERVER_VAL = process.env.API_SERVER;
break;
default:
API_SERVER_VAL = 'http://localhost:8000';
MEDIA_SERVER_VAL = 'http://localhost:8000';
FRONTEND_SERVER_VAL = 'http://localhost:3000';
break;
}
export const API_SERVER = API_SERVER_VAL;
export const MEDIA_SERVER = MEDIA_SERVER_VAL;
export const FRONTEND_SERVER = FRONTEND_SERVER_VAL;
export const …Run Code Online (Sandbox Code Playgroud) 我需要运行一个版本 16 和 typescript 的沙箱 React 应用程序。
npx create-react-app --template=typescript blah有谁知道现在使用 React 17该怎么做?
更新
正如评论中所建议的,我尝试使用以下命令进行安装:
npm init react-app migration-calculator-npm-demo --scripts-version 3.4.4
和
npm init react-app migration-calculator-npm-demo --template typescript --scripts-version 3.4.4
3.4.4看起来像是react-scripts重大更新之前的最后一个版本。
不幸的是,我两者都出错了。
内部/模块/cjs/loader.js:1088
抛出错误;^错误:找不到模块“cra-template”
和
内部/模块/cjs/loader.js:1088
抛出错误;^错误:找不到模块“cra-template-typescript”
我创建了一个简单的 React 应用程序,create-react-app它在本地主机上运行良好。我现在正在尝试 Dockerify 该应用程序。这是我的 package.json:
{
"name": "yeet",
"version": "0.1.0",
"engines": {
"node": "12.x"
},
"scripts": {
"client": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"start": "node server.js",
"production": "npm run build && npm run start"
},
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"axios": "^0.21.0",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-session": "^1.17.1",
"mongoose": "^5.10.13",
"mysql": "^2.18.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.0",
"web-vitals": "^0.2.4"
},
"devDependencies": {
"babel-core": …Run Code Online (Sandbox Code Playgroud) 我尝试在我的 React 项目中使用react-player。我正在使用打字稿。当我构建它时,我收到以下错误:
“ReactPlayer”不能用作 JSX 组件。其实例类型“ReactPlayer”不是有效的 JSX 元素。“render()”返回的类型在这些类型之间不兼容。类型“ReactNode”不可分配给类型“false” | 元素| 无效的'。类型“未定义”不可分配给类型“假” | 元素| 无效的'。我怀疑这是 ReactPlayer 特有的。我可能错误配置了该项目。有什么帮助吗?
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
]
}
Run Code Online (Sandbox Code Playgroud)
包.json
{
"name": "home-exercise",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"@types/jest": "^24.9.1",
"@types/node": "^12.12.67",
"@types/react": "^16.9.56", …Run Code Online (Sandbox Code Playgroud) 运行后npm run devbuild 文件夹中没有 service-worker.js 文件。
我也在
serviceWorker.register()index.js 中 进行了更改
,我也在这个项目中使用firebase。
这是package.json文件
{
"name": "movie",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"axios": "^0.20.0",
"email-verifier": "^0.4.1",
"firebase": "^7.24.0",
"history": "^5.0.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-lottie": "^1.2.3",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini …Run Code Online (Sandbox Code Playgroud) create-react-app ×10
reactjs ×7
docker ×3
webpack ×2
azure-devops ×1
babeljs ×1
craco ×1
css ×1
css-loader ×1
firebase ×1
javascript ×1
jestjs ×1
node.js ×1
npm ×1
typescript ×1