标签: create-react-app

如何使用 Service Worker 处理 index.html 文件的缓存?

我目前正在使用create-react-app默认服务工作者开发渐进式 Web 应用程序。

在发布我们的一个 javascript 块的新版本时,我遇到了破坏缓存的问题。

构建时,输出 javascript 文件使用 acontenthash来确保当 JS 文件中的内容更改时,文件名也会更改。在没有 Service Worker 的情况下运行时,这成功地破坏了缓存。

但是,在使用create-react-appService Worker 时,包括我的index.html文件在内的所有静态资产都会被缓存。这意味着旧index.html的将提供给用户,其中包括<script>我的旧缓存 javascript 文件的标签,而不是带有更新的 .js 文件的新文件contenthash。

我已经弹出并修改了 webpack.config.jsWorkboxWebpackPlugin以排除我的 index.html 文件:

 new WorkboxWebpackPlugin.GenerateSW({
      clientsClaim: true,
      exclude: [/\.map$/, /asset-manifest\.json$/, /index.html/],
      importWorkboxFrom: "cdn",
      navigateFallbackBlacklist: [
          // Exclude URLs starting with /_, as they're likely an API call
             new RegExp("^/_"),
          // Exclude URLs containing a dot, as they're likely a resource in …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs service-worker create-react-app

7
推荐指数
1
解决办法
2884
查看次数

在 create-react-app 中找不到 svg 模块打字稿错误

在 create-react-app 中,我将正在运行的 jsx 文件转换为 tsx 文件,并添加以下行:

import { ReactComponent as Logo } from "./logo.svg";
Run Code Online (Sandbox Code Playgroud)

抛出打字稿错误:找不到模块“./logo.svg”或其相应的类型声明.ts(2307)

在 tsconfig.json 我有:

{
  "include": ["src", "./src/**/*.ts", "./src/**/*.svg"]
}
Run Code Online (Sandbox Code Playgroud)

看起来 node_modules/react-scripts/lib/react-app.d.ts 已经有:


declare module '*.svg' {
  import * as React from 'react';

  export const ReactComponent: React.FunctionComponent<React.SVGProps<
    SVGSVGElement
  > & { title?: string }>;

  const src: string;
  export default src;
}
Run Code Online (Sandbox Code Playgroud)

所以我不确定问题是什么。

SVG 的开头如下:

<svg xmlns="http://www.w3.org/2000/svg" height="100" width="100"><path d="M38.0
Run Code Online (Sandbox Code Playgroud)

svg typescript create-react-app react-typescript

7
推荐指数
1
解决办法
7321
查看次数

使用 create-react-app 时,React 将连续构建文件放在哪里

我在用着create-react-app。当我运行npm start( react-scripts start) 时,它会不断为我构建更改并发挥神奇作用。但它的输出文件夹是什么?我知道当我手动构建它时文件去了哪里。

我想使用 firebase 模拟器来服务我的所有反应的当前版本(连续构建),但我不明白输出文件夹在哪里或如何实现它。

firebase reactjs firebase-tools create-react-app

7
推荐指数
1
解决办法
4137
查看次数

Dotenv 与 create-react-app 反应脚本出现问题/错误

我的反应应用程序中不断收到此错误

    Compiled with problems: X

    ERROR in ./ node_modules / dotenv / lib / main.js 1: 11 - 24

    Module not found: Error: Can't resolve 'fs' in 'C: \Users\USER\Desktop\dapps\nefty\node_modules\dotenv\lib'


    ERROR in ./ node_modules / dotenv / lib / main.js 3: 13 - 28

    Module not found: Error: Can't resolve 'path' in 'C: \Users\USER\Desktop\dapps\nefty\node_modules\dotenv\lib'

    BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
    This is no longer the case. Verify if you need this module …
Run Code Online (Sandbox Code Playgroud)

reactjs webpack react-scripts create-react-app dotenv

7
推荐指数
1
解决办法
1万
查看次数

React.js:如何在不同端口上启动反应应用程序

我刚刚开始学习React.js并使用Create React App制作了两个项目。

现在,如果我想同时启动这两个项目,我不能,因为默认情况下两个项目都在端口3000上启动。

有什么方法可以将其中一个的默认端口更改为其他端口,例如 http://localhost:3001 并同时运行它们?

注意:我是 Windows 用户

port localhost reactjs create-react-app

7
推荐指数
1
解决办法
1万
查看次数

Webpack 5 splitChunks 破坏了 WebpackDevServer

我从 create-react-app 的新副本开始。然后我运行弹出,并想添加代码分割。但是,每当我添加

splitChunks: {
        automaticNameDelimiter: '.',
        chunks: 'all',
      },
Run Code Online (Sandbox Code Playgroud)

该应用程序卡在正在启动开发服务器...

如果我删除chunks: all该应用程序将再次开始工作。

注意:我根本没有改变任何其他内容。

  1. npx create-react-app my-app --template typescript
  2. cd 我的应用程序
  3. npm 运行弹出
  4. 编辑 webpack.config.js
  5. npm 启动
  6. 卡在启动开发服务器上......

我没有收到任何错误或任何关于如何找出问题所在的有用信息。它只是卡住了,有什么想法可能是错误的,或者我可以做些什么来获得更多关于卡住的地方的调试信息吗?

webpack.config.js

module.exports = function (webpackEnv) {
  const isEnvDevelopment = webpackEnv === "development";
  const isEnvProduction = webpackEnv === "production";

  const isEnvProductionProfile =
    isEnvProduction && process.argv.includes("--profile");

  const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1));

  const shouldUseReactRefresh = env.raw.FAST_REFRESH;

  // common function to get style loaders
  const getStyleLoaders = (cssOptions, preProcessor) => {
    const loaders …
Run Code Online (Sandbox Code Playgroud)

webpack create-react-app webpack-splitchunks

7
推荐指数
1
解决办法
2722
查看次数

如何在 create-create-app 项目中禁用 babel-loader 缓存?

我正在开发一个create-create-app项目,并注意到当node_modules手动更新依赖项时,更改不会到达浏览器。

经过一番挖掘后,我发现依赖项现在缓存在node_modules/.cache/babel-loader.

有没有办法禁用单个或所有依赖项的缓存?

感谢您的帮助!

node.js create-react-app

7
推荐指数
0
解决办法
764
查看次数

更新 React 和 Electron 并收到“Uncaught ReferenceError:全局未定义”

我有一个 Electron + React 应用程序,几年来没有更新过,最初使用这些包:

"@rescripts/cli": "^0.0.13",
"@rescripts/rescript-env": "^0.0.11",
"electron": "^7.1.10",
"electron-builder": "^22.3.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.4.0",
Run Code Online (Sandbox Code Playgroud)

现在我希望它使用:

"@rescripts/cli": "^0.0.13",
"@rescripts/rescript-env": "^0.0.11",
"electron": "^21.1.1",
"electron-builder": "^23.6.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
Run Code Online (Sandbox Code Playgroud)

当我运行我的应用程序时,Electron 窗口/菜单/等会完美加载,但应用程序的实际 React 部分却不会。React 应用程序本身的代码此时并不真正相关,因为它甚至不会渲染基本的“hello world”。

我已经更新(或注释掉)了几乎所有已弃用的功能,但我认为rescripts/Create React App 的 webpack 部分导致了错误Uncaught ReferenceError: global is not defined.

global此时该变量在我的代码中不存在,但我的.webpack.config.js有:

module.exports = config => {
  config.target = "electron-renderer";
  return config;
};
Run Code Online (Sandbox Code Playgroud)

我的.rescriptsrc.js有:

module.exports = [require.resolve("./.webpack.config.js")];
Run Code Online (Sandbox Code Playgroud)

我认为这些是罪魁祸首,因为当我点击错误时,它指向实际不在我的代码库中的已编译的 webpack 文件。

我在 …

javascript reactjs webpack electron create-react-app

7
推荐指数
1
解决办法
1091
查看次数

Vite CommonJS Resolver 导入 ESM 时抛出意外令牌错误

我有一个 create-react-app 项目,我正在迁移到 Vite。该项目使用“样式组件”,如下所示:

\n
import styled from \'styled-components\';\n\nconst someDiv = styled.div`...`;\n
Run Code Online (Sandbox Code Playgroud)\n

现在运行时出现问题vite build:

\n
vite v4.0.2 building for production...\n\xe2\x9c\x93 51 modules transformed.\n[commonjs--resolver] Unexpected token (1:167492) in /project/node_modules/styled-components/dist/styled-components.browser.esm.js\nfile: /project/node_modules/styled-components/dist/styled-components.browser.esm.js:1:167492\n1: import{typeOf as e,isElement as t,isValidElementType as n}from"react-is";import r,{useState as o,useContext as i,useMemo as s,useEffect as a,useRef as c,createElement as u,useDebugValue as l,useLayoutEffect as d}from"react";import h from"shallowequal";import p from"@emotion/stylis";import f from"@emotion/unitless";import m from"@emotion/is-prop-valid";import y from"hoist-non-react-statics";function // ... rest of file\n
Run Code Online (Sandbox Code Playgroud)\n

所以看起来它正在尝试styled-components.browser.esm.js使用 commonjs--resolver 进行导入,但是import该文件的顶部有一些语句,并且似乎很混乱。

\n …

commonjs es6-modules create-react-app vite

7
推荐指数
1
解决办法
4420
查看次数

在 React 和 axios 中使用不同的 API url 进行开发和生产

我正在使用 React 前端和 Node.js 后端(API)构建 Web 应用程序。
在 React 前端,我调用这样的 API 方法:

axios({
    method: 'post',
    url: 'http://servername:9999/reports.activities',
    data: {
        user_id: 1
    }
}).then(res => {
    this.setState(res.data);
});
Run Code Online (Sandbox Code Playgroud)

有时我需要测试尚未发布到生产环境的 API 方法。
当我在本地测试后端时,我nodemon api.js在localhost:9999.
每次我想用本地运行的 API 测试前端时,我都http://servername:9999必须http://localhost:9999在我的前端代码中更改为。
我认为这不是正确的方法。
使用不同 url 进行开发(在npm start本地运行时)和生产(npm build)的最佳方法是什么?
我正在考虑为此目的使用dotenv。这是正确的方法吗?
最佳做法是什么?

javascript reactjs axios create-react-app

6
推荐指数
2
解决办法
5903
查看次数