create-react-app是一个入门套件,用于创建没有构建配置的React应用程序。如果您开始使用React,请使用此选项来自动构建应用程序。没有配置文件,react-scripts是package.json中唯一的额外构建依赖项。您的环境将拥有构建现代React应用程序所需的一切。
npm run build <p><code>index.html</code><strong> </strong>您可以看到,路径集为<pre><code><script src="./static/js/5.a4bfdba9.chunk.js"></script> </code></pre><pre> </pre>,但我希望将路径设置为</p><code>./static/js/</code><pre></pre> <p>我无法弄清楚在何处或何处可以确保每次运行构建时,它都会采用我指定的路径而不是默认路径?<pre></pre> </p><p>注:<pre>我在</pre><code>./static/dashboard/js/5.a4bfdba9.chunk.js</code></p>中查看<p><code>"homepage": "."</code></p>属性,但是更改此属性只会在<p><code>package.json</code><strong></strong>之前附加一条路径 <pre> </pre> today有一个更好的解决方案。来自<pre>hashRocket</pre>:<pre> </pre>当我构建CRA应用程序时,我会为以 /静态开头的资产(CS,图像)提供一条路径。如果我将应用程序部署到</p><code>/static/js/</code></question>,那么该应用程序将尝试访问<answer tick="false" vote="15"><code>https://something.com/myapp</code><p>处的这些资产路径。那不是资产所居住的地方。资产居住在<a href="https://til.hashrocket.com/posts/xtgpx9kssz-set-the-relative-path-of-assets-in-a-cra-app" rel="noreferrer"><code>https://something.com/static/asset.css</code></a>。 Create React应用程序允许您更改A内置资产的前缀,该资产具有</p><code>https://something.com/myapp/static/asset.css</code><p>文件中的主页属性。 您可以将其设置为MyApp:<pre> </pre><code>package.json</code><pre> 然后资产将具有</pre><code>"homepage": "/myapp" </code><pre></pre>的路径 </p> <p>在我的情况下,我对我的<pre><code>/myapp/static/asset.css</code></pre>进行了以下更改:</p> <p><code>package.json</code></p> <pre>这是因为我有一个nginx反向代理,可服务于另一个端点,因此我所有的静态资产都必须从</pre><code> "homepage": "/search", "build": "react-scripts build && mkdir build/search && mv build/static build/search/static" </code><p>提供。 <pre> </pre> </p><hr/>update:<p><pre> 您将需要更新WebPack配置以实现此目的。有多种方法:</pre> </p> <pre>反应式弹出(不建议)</pre> <p>-patch-package<pre> </pre>反理申请 - 涂层 <pre> 我正在为</pre><code>/search</code></p>。 您需要更改React-Scripts软件包的文件</answer><code>/search/static</code><answer tick="true" vote="8">文件。这是您需要做出的一些更改的差异:<p> <strong><code>patch-package</code></strong> </p>现在,如果您对<p><code>config/webpack.config.js</code></p>进行这些更改,则将输出的文件输出到所需的文件夹和index.html也将具有正确的路径。 但是,如果您安装/卸载任何软件包,则Node_modules的更改将被覆盖。为了坚持这些更改,您可以使用<ol>Patch-package<li>,它将在安装后写下您的node_modules的更改。 </li>这里是设置补丁包装的步骤,您可以阅读其<li>readme文件</li>有关更多详细信息:<li> </li> </ol><p><pre><code>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 </code></pre><br/> <pre> </pre></p>在package.json的脚本部分中添加了此内容:<pre> </pre><code>node_modules/react-scripts/config/webpack.config.js</code><p><pre> </pre> <br/><a href="https://www.npmjs.com/package/patch-package" rel="noreferrer">run patch-ackage以创建.patch文件:</a> </p><code>yarn add patch-package postinstall-postinstall</code><p><a href="https://github.com/ds300/patch-package#readme" rel="noreferrer"> </a> </p><ol>召集补丁文件以与您的团队共享修复程序<li> <p><code>"postinstall": "patch-package"</code><pre></pre> </p><code>npx patch-package some-package</code></li><li> <p> <br/> <pre>我也创建了一个</pre>库存储库</p>供您参考。 </li><li>答案:<p><br/> <pre></pre>战争:</p>不适用于移动构建文件夹的内容。只能移动整个构建文件夹。例如:</li><code>git add patches/react-scripts+4.0.3.patch</code><li><p> <br/>改变创建React应用程序的设置,以便将其输出到不同文件夹将非常复杂。但是您可以在构建完成后移动文件,这很简单。 <pre>在您的包裹中。 </pre><code>git commit -m "change output dir structure in react-scripts"</code><br/> <pre>您可以添加另一个称为the build后进行操作的脚本,并将由NPM/YARN自动调用。 </pre><code>mv build/ dist/</code></p> </li>该文件应将文件移至其他文件夹。 </ol> <p> <a href="https://github.com/vaibhavhrt/cra-custom-build-dir-structure" rel="noreferrer">1-纱线添加 @craco/craco 2-创建craco.config.js文件 3 craco.config.js = </a><code>"scripts": { "build": "react-scripts build" </code></p>$ {staticfileName}/js/[name]。[contentHash].chunk.js<p><code>postbuild</code><strong>$ {staticfilename}/js/[name] </strong><code>"postbuild": "mv build/ dist/" </code></p> <p>};`<strong> </strong> <pre> </pre>中其他任何人都遇到了类似的问题。我的问题是,项目名称已被置于静态路径上。 </p>路径是<p><code>module.exports = { webpack: { configure: (webpackConfig) => { webpackConfig.output.chunkFilename = </code></p>而不是<p><code>; webpackConfig.output.filename = </code></p><pre> </pre>关注解决方案,只要我遇到了一个注释,即从主页推断出“公共路径”。在我的主页URL中包括项目名称。 <p><pre>wrong</pre> “主页”:“ https:// {url} .io/project-name”</p> <pre></pre>纠正 “主页”:“ https:// {url} .io/project-name”<p> 从此URL中删除项目名称解决了问题。 </p> </answer><answer tick="false" vote="0">
这可能是一个愚蠢的问题,但我真的不知道发生了什么。 我已经创建了一个使用Crate React应用程序的React应用程序,并将其作为私人存储库上传到我的GitLab帐户。 (默认)...
我有一个CRA打字稿应用程序,我正在尝试运行测试,但是当他们偶然发现Ky Import时,它们会出错。
模块解析失败:意外令牌(257:106)您可能需要适当的加载程序来处理此文件类型
在我的应用程序上运行“ NPM更新”后,我突然遇到以下错误: 编译有问题: ./node_modules/pako/lib/zlib/trees.js 257:106 模块解析失败:出乎意料的Tok ...
在运行npx create-react-app my-app上的vs vs code
npx create-react-app my-app
我正在尝试部署并完成我的第一个React网页项目。 我的代码纯粹是在JS中制成的,我认为我不使用typesctipt
MyNPM安装运行良好,当我开始构建它时,也没有任何问题的迹象。
无法在我的 Windows 上的 vscode 中运行 create-react-app
以下是日志文件中的消息。请任何帮助,真的令人沮丧。 '日志文件: C:\Users\用户\AppData\Local pm-cache_logs�5-01-07T21_38_20_381Z-debug-0.log npm解析错误报告 W...
项目构建后存在CSS未应用的问题。使用 npm run start 在本地 CSS 显示正确。使用 npm run build 和 npmserve 在本地构建和服务...
自上周以来,这个错误阻碍了我的进步......我尝试过人工智能、谷歌搜索、Github,询问有经验的人,但没有任何效果。 所以我只是react和nodejs的初学者。我正在关注A博士...
创建 React 应用程序错误:ERESOLVE 错误依赖项
我正在使用react.dev网站上提到的这个命令创建我的第一个React应用程序: npx 创建反应应用程序我的应用程序 它抛出错误: 使用 npm 安装模板依赖项... npm 错误 c...
如何使用<script>使Vite应用程序在嵌入到另一个项目时与CRA应用程序表现相同?
我正在关注一个类似项目的教程,我们想要以浮动按钮的形式创建一个小部件,单击该按钮时将显示一个模式。我们也在尝试制作这个小部件
React 版本 19 错误:水合失败,因为服务器呈现的 HTML 与客户端不匹配
我是 React JS 的新手,并通过以下方式设置了 React 应用程序: npx create-next-app@最新项目名称 花了很长时间后,安装完成,但出现错误: 我试图用谷歌搜索这个错误但是...
我使用 create-react-app 来创建我的 ReactJS 应用程序。现在,我看到由于在捆绑代码时加载块时出现一些错误,页面上生成了一些内联脚本。因此,公司...