如何更改 React 项目中的生产构建静态文件夹路径?

苏维克·雷

我有一个我运行React项目,它为我npm run build制作了一个生产版本。问题是它给了我以下样式表注入index.html

<script src="./static/js/5.a4bfdba9.chunk.js"></script>

如您所见,路径集是 ./static/js/

但我希望将路径设置为 ./static/dashboard/js/5.a4bfdba9.chunk.js

我无法弄清楚每次运行构建时要更改的位置或内容,以确保它采用我指定的路径而不是默认路径?

注意:我查看了"homepage": "."属性 inpackage.json但更改此属性只会在之前附加一个路径/static/js/

瓦哈夫·维沙尔

更新:

你需要更新 webpack 配置来实现这一点。有多种方法可以做到这一点:

  1. 反应脚本弹出(不推荐)
  2. 补丁包
  3. react-app-rewired

我正在为patch-package.
您需要对config/webpack.config.jsreact-scripts 包的文件进行更改这是您需要进行的更改的 git diff:

diff --git a/node_modules/react-scripts/config/webpack.config.js b/node_modules/react-scripts/config/webpack.config.js
index 26c2a65..ad29fbd 100644
--- a/node_modules/react-scripts/config/webpack.config.js
+++ b/node_modules/react-scripts/config/webpack.config.js
@@ -212,13 +212,13 @@ module.exports = function (webpackEnv) {
       // There will be one main bundle, and one file per asynchronous chunk.
       // In development, it does not produce real files.
       filename: isEnvProduction
-        ? 'static/js/[name].[contenthash:8].js'
+        ? 'static/dashboard/js/[name].[contenthash:8].js'
         : isEnvDevelopment && 'static/js/bundle.js',
       // TODO: remove this when upgrading to webpack 5
       futureEmitAssets: true,
       // There are also additional JS chunk files if you use code splitting.
       chunkFilename: isEnvProduction
-        ? 'static/js/[name].[contenthash:8].chunk.js'
+        ? 'static/dashboard/js/[name].[contenthash:8].chunk.js'
         : isEnvDevelopment && 'static/js/[name].chunk.js',
       // webpack uses `publicPath` to determine where the app is being served from.
       // It requires a trailing slash, or the file assets will get an incorrect path.
@@ -676,8 +676,8 @@ module.exports = function (webpackEnv) {
         new MiniCssExtractPlugin({
           // Options similar to the same options in webpackOptions.output
           // both options are optional
-          filename: 'static/css/[name].[contenthash:8].css',
-          chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
+          filename: 'static/dashboard/css/[name].[contenthash:8].css',
+          chunkFilename: 'static/dashboard/css/[name].[contenthash:8].chunk.css',
         }),
       // Generate an asset manifest file with the following content:
       // - "files" key: Mapping of all asset filenames to their corresponding

现在,如果您进行这些更改,node_modules/react-scripts/config/webpack.config.js您会将文件输出到您想要的文件夹,并且 index.html 也会有正确的路径。
但是如果您安装/卸载任何软件包,node_modules 中的更改将被覆盖。要保留这些更改,您可以使用patch-package,它会在安装后在 node_modules 中写入您的更改。

以下是设置补丁包的步骤,您可以阅读他们的自述文件以获取更多详细信息:

  1. yarn add patch-package postinstall-postinstall

  2. 在 package.json 的脚本部分添加这个:
    "postinstall": "patch-package"

  3. 运行 patch-package 创建一个 .patch 文件:
    npx patch-package some-package

  4. 提交补丁文件以与您的团队共享修复
    git add patches/react-scripts+4.0.3.patch
    git commit -m "change output dir structure in react-scripts"

我也创建了一个git 存储库供您参考。

旧答案:

警告:不适用于移动构建文件夹的内容。只能移动整个构建文件夹。例如:mv build/ dist/

更改 create react app 的设置以使其输出到不同的文件夹将非常复杂。但是您可以在构建完成后移动文件,这非常简单。

在你的 package.json 你有:

"scripts": {
  "build": "react-scripts build"

您可以添加另一个称为postbuild在构建后执行某些操作的脚本,并将由 npm/Yarn 自动调用。

"postbuild": "mv build/ dist/"

这应该将文件移动到不同的文件夹。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React 应用程序生产构建静态文件夹路径更改

如何从React Native ios项目中的components文件夹访问js文件

如何在React Native项目中重新生成ios文件夹?

如何在 .Net Core+React 项目中创建 wwwroot 文件夹?

如何在React项目中集中快照文件夹?

React + Node + Firebase,如何部署构建文件夹?

C# System.IO 如何获取我的项目中文件夹的路径

在构建项目时,Spring Boot如何识别target / classes文件夹中的静态资源?

如何获取.net core项目中项目文件夹中存在的文件的相对路径

如何使用绝对路径参考文件夹构建VSTS项目?

在Vue-CLI项目中更改公用文件夹

如何更改.NET Core项目中`wwwroot`文件夹的图标?

如何在create-react-app中将`src`文件夹更改为其他内容

如何在Laravel项目中gzip文件夹

如何从项目中的文件夹向collectionview添加图像?

如何永久删除 Azure VSTS 项目中的文件夹?

如何根据正在使用的文件夹更改文件路径?

如何使用Webpack构建React应用并导入资产文件夹?

如何在路径不明确的子文件夹中设置React Router

如何在 React-nativeProject 中找到 app 文件夹路径

如何在Views文件夹中构建项目

如何在Jenkins中更改构建开始文件夹

React项目中.gitignore应该位于哪个文件或文件夹中?

如何将包含 create-react-app 子文件夹的项目推送到 GitHub?

IntelliJ:如何更改“项目文件夹”窗口的背景颜色?

如何更改HTACCESS以指向项目所在的子文件夹

React Native项目中应该忽略哪些文件夹git?

在 react-native 项目中重新创建 iOS 文件夹

如何使用react-native和expo获取位于assets文件夹中的文件的绝对路径?