如何使用 create-react-app 从 webpack 中的捆绑文件路径、publicPath、PUBLIC_URL 中删除初始斜杠?

发布于 2025-01-11 02:58:12 字数 255 浏览 1 评论 0原文

我们如何从 webpack 中自动包含在 index.html 中的所有文件中删除初始斜杠(例如

如何在命令行 npx create-react-app 创建的 React 项目中自定义 webpack.config.js 而不弹出它?

How can we remove the initial slash from all the files included automatically in index.html from webpack (like <script src="/static/js/...">...) and all the paths of a react app?

How is possible to customize the webpack.config.js in a react project created by the command line npx create-react-app without ejecting it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

上课铃就是安魂曲 2025-01-18 02:58:12

Fancesco Orsi 的回答的第 1 部分解决了我的部分问题,但 /static/ 似乎需要搞乱配置或添加库。

将在我的反应和电子构建之间运行的后记搞砸在一起更简单。我相信人们可以根据他们的需要重新利用它。

我使用以下代码创建了一个 JavaScript 文件,并将 package.json 中的后脚本指向它。

"postbuild": "node mid-build.js"

const { readFileSync, writeFileSync } = require('fs');
let txt = readFileSync('./build/index.html');
txt = txt.toString().replaceAll('/static/', 'static/');
writeFileSync('./build/index.html', txt);
console.log('\nmid-build edits complete\n');

不太优雅,但它有效,我可以返回调试构建。

Part 1 of Fancesco Orsi's answer fixed part of my problem, but the /static/ appears to require messing with configs or adding libraries.

It was simpler to botch together a postscript that runs between my react and electron build. I'm sure people could repurpose this for what they need.

I made a JavaScript file with the following code and pointed a post-script in package.json to it.

"postbuild": "node mid-build.js"

const { readFileSync, writeFileSync } = require('fs');
let txt = readFileSync('./build/index.html');
txt = txt.toString().replaceAll('/static/', 'static/');
writeFileSync('./build/index.html', txt);
console.log('\nmid-build edits complete\n');

Not elegant, but it works and I can get back to debugging builds.

趁微风不噪 2025-01-18 02:58:12

打开 public/index.html 删除 %PUBLIC_URL% 之后/之前的所有斜杠,

例如,

<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />

则变为

<link rel="icon" href="%PUBLIC_URL%favicon.ico" />

如果您使用变量 process.env.PUBLIC_URL, > 在 jsx 中,始终删除变量前后的斜杠。

例如,

const json = await http({ url: `${process.env.PUBLIC_URL}/config.json`});

const json = await http({ url: `${process.env.PUBLIC_URL}config.json`});

在项目的根目录中创建一个名为 .env 的空文件,输入以下文本

PUBLIC_URL=

替换 webpack.config.jspublicPath 值(您通常可以在 /node_modules/react-scripts/config/webpack.config.js 中找到此文件)而不触及原始(不推荐!!),您可以使用像 react-app-rewiredcraco 这样的库>。

下面您可以看到 craco 的示例 https://www.npmjs.com/package/@ craco/craco

通过 npm i @craco/craco 安装后,

您需要将 package.json 中的一些行从 替换

之后

...
  "scripts": {
    "start": "react-script start",
    "build": "react-script build",
    "test": "react-script test",
    "eject": "react-script eject"
  },
...

...
  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "craco eject"
  },
...

,在您的根项目添加一个名为 craco.config.js

在文件内的

module.exports = {
    configure: {
      output: {
        publicPath: ''
      }
    }
  }
}

添加以下代码即可

open the public/index.html removing all the slashes after/before %PUBLIC_URL%

for example

<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />

become

<link rel="icon" href="%PUBLIC_URL%favicon.ico" />

if you use the variable process.env.PUBLIC_URL in your jsx, remove always the slash after/before the variable.

for example

const json = await http({ url: `${process.env.PUBLIC_URL}/config.json`});

become

const json = await http({ url: `${process.env.PUBLIC_URL}config.json`});

Create a empty file in the root of the project called .env entering this text:

PUBLIC_URL=

to replace the publicPath value of the webpack.config.js (you normally find this file in /node_modules/react-scripts/config/webpack.config.js) without touching the original (it is not recommended!!) you can use a library like react-app-rewired or craco.

Below you can see an example with craco https://www.npmjs.com/package/@craco/craco :

After the installation by npm i @craco/craco

you need to replace some lines of your package.json

from

...
  "scripts": {
    "start": "react-script start",
    "build": "react-script build",
    "test": "react-script test",
    "eject": "react-script eject"
  },
...

to

...
  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "craco eject"
  },
...

after that, in your root project add a file called craco.config.js

inside the file add the following code

module.exports = {
    configure: {
      output: {
        publicPath: ''
      }
    }
  }
}

that's it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文