gpt4 book ai didi

Quasar changes capacitor.config.json on dev server start(Quasar在开发服务器启动时更改Capacitor.config.json)

转载 作者:bug小助手 更新时间:2023-10-24 23:53:55 28 4
gpt4 key购买 nike



Running Quasar dev server in Capacitor mode withquasar dev -m capacitor -T android results in the dialog to choose external IP address:

在电容模式下运行Quasar dev服务器,并使用quasar dev-m dev-T android,会出现选择外部IP地址的对话框:


? What external IP should Quasar use? 192.168.0.3
App • Updated src-capacitor/package.json
App • Updated capacitor.config.json
App • Running "...\node_modules\@capacitor\cli\bin\capacitor sync android" in ...

The problem is that this updates src-capacitor/capacitor.config.json with local information that can be accidentally pushed, which is never desirable:

问题是,这会使用可能被意外推送的本地信息来更新src-apacitor.config.json,这永远不是我们所希望的:


  "server": {
"androidScheme": "https",
"url": "http://192.168.0.3:9500" <-- this line is added
}

Not to mention that most times it would be practical to skip this dialog, as the application runs locally and can safely use localhost instead of external IP.

更不用说,大多数情况下跳过此对话框是可行的,因为应用程序在本地运行,并且可以安全地使用本地主机而不是外部IP。


As loosely described in the documentation, this seems to be the expected behaviour but not desirable one.

正如文档中大致描述的那样,这似乎是预期的行为,但不是理想的行为。


How can updating server.url in capacitor.config.json be avoided?

如何避免更新Capacitor.config.json中的server.url?


更多回答
优秀答案推荐

quasar dev -m capacitor -T android starts a web server and makes capacitor use that web server url for live reload.
That’s what the dev option is for, so you can’t change that behavior.
If you want to run your app without the live reload server you can use
quasar build -m capacitor -T android instead, that will remove the server url from the capacitor.config.json.

Quasar dev-m Capacity-T Android启动一台Web服务器,并使用该Web服务器URL进行实时重新加载。这就是dev选项的作用,因此您无法更改该行为。如果你想在没有实时重载服务器的情况下运行你的应用程序,你可以使用quasar Build-m Capacity-T android,这将从capacitor.config.json文件中删除服务器的url。



Under the hood of Quasar/Vite/Capacitor setup, devServer.host configuration option is compared against hardcoded set of addresses:

在QUASAR/VITE/CAACITOR设置的幕后,将DevServer.host配置选项与硬编码的地址集进行比较:


`'0.0.0.0', 'localhost', '127.0.0.1', '::1'`

In case a host is considered local (0.0.0.0 by default), external IP dialog is triggered, otherwise devServer.host is used as is. The value is written to capacitor.config.json nonetheless.

如果主机被视为本地主机(默认情况下为0.0.0.0),则会触发外部IP对话,否则将按原样使用devServer.host。尽管如此,该值仍被写入Capacitor.config.json。


It's likely possible to specify another loopback (127.0.0.2) in devServer.host to pass this check and keep a usable default configuration for iOS-only project. It won't be available in Android Studio emulator as it aliases 127.0.0.1 that falls under the restriction.

很可能在devServer.host中指定另一个环回(127.0.0.2),以通过此检查并为仅iOS项目保留可用的默认配置。它将不会在Android Studio模拟器中使用,因为它是受限制的127.0.0.1的别名。


A reasonable approach would be pass locally configured external IP to quasar.config.json with dotenv and discard the changes in capacitor.config.json by means of Git.

一种合理的方法是将本地配置的外部IP传递给带有dotenv的quasar.fig.json,并通过Git丢弃Capacitor.config.json中的更改。




The problem is that this updates src-capacitor/capacitor.config.json with local information that can be accidentally pushed, which is never desirable:



That is why your actual config files should remain private (not tracked by Git).

You should only track config file values, plus a config file template

这就是为什么你的实际配置文件应该是私有的(不被Git跟踪)。您应该只跟踪配置文件值,外加配置文件模板


  src-capacitor/capacitor.config.dev.json
src-capacitor/capacitor.config.prod.json
src-capacitor/capacitor.config.tpl.json

Then, you can use a content filter driver, using .gitattributes declaration.

然后,您可以使用内容筛选器驱动程序,并使用.gittributes声明。


smudge
(image from "Customizing Git - Git Attributes", from "Pro Git book")

(图片来自《定制Git-Git属性》,摘自《专业Git书》)


The script generate the right json file by replacing placeholder values with the fixed value "url": "http://192.168.0.3:9500" each time you checkout a branch, when the smudge script detects a local development environment.

该脚本通过将占位符值替换为固定值“URL”来生成正确的JSON文件:“当http://192.168.0.3:9500”脚本检测到本地开发环境时,每次您签出一个分行时。


The generated actual conf remains ignored (by the .gitignore).

No synchronization issue during merges.

生成的实际配置文件仍然被忽略(通过.gitignore)。合并期间没有同步问题。


The smudge script selects the correct value file and generates the correct json based on the template the smudge script is applied to during a git switch (or old git checkout).

涂抹脚本选择正确的值文件,并基于git切换(或旧git签出)期间应用涂抹脚本的模板生成正确的json。


See a content driver setup example at "git smudge/clean filter between branches".

参见“分支之间的git涂抹/清理过滤器”中的内容驱动程序设置示例。


Your .gitattributes would include src-capacitor/capacitor.config.json filter=smudgeScript

您的.git属性将包括src-Capacity/Capacitor.config.json Filter=smudgeScript


git config filter.smudgeScript.smudge /path/to/your/smudgeScript
git config filter.smudgeScript.clean "cat src-capacitor/capacitor.config.tpl.json"

With smudgeScript (here, I simulate the logic which detects you are in a "devServer" with a hypothetical ENV variable):

使用SmudgeScrip(在这里,我用一个假设的ENV变量模拟检测到您在“devServer”中的逻辑):


#!/bin/bash

ENV=$(git branch --show-current)

if [[ "$ENV" == "development" ]]; then
CONFIG_FILE="src-capacitor/capacitor.config.dev.json"
elif [[ "$ENV" == "production" ]]; then
CONFIG_FILE="src-capacitor/capacitor.config.prod.json"
else
# Default to development environment
CONFIG_FILE="src-capacitor/capacitor.config.dev.json"
fi

cat $CONFIG_FILE



The alternative is to code that logic in the quasar.conf.js

另一种方法是在quasar.conf.js中编码该逻辑


module.exports = function (/* ctx */) {
return {
// ...other configurations
devServer: {
public: 'http://localhost:8080', // replace with your local development server address
},
}
}

That would set a static URL in your quasar.conf.js. Inside the devServer property, specify a public property with your desired URL. You need to add the logic which detects you are in a "devServer".

这将在quasar.conf.js中设置一个静态URL。在devServer属性中,指定一个带有所需URL的公共属性。您需要添加检测您处于“devServer”中的逻辑。





Unfortunately, it appears that it's not possible to specify 127.0.0.1 for Capacitor in the config without modifying Quasar source code, even though it would be possible to make it work with Android Studio. Looks like specifying 192.168.xx.xx IP and discarding the changes from git is the way to go.



True, and using the first approach, where src-capacitor/capacitor.config.json is ignore, will allow you to specify what you need in it, without risking to add/commit/push said modification.

正确,并且使用第一种方法,其中忽略src-Capacitor.fig.json,将允许您在其中指定您需要的内容,而无需冒险添加/提交/推送所述修改。


更多回答

Nice, that seems simpler than my answer.

很好,这似乎比我的回答简单。

So server.url doesn't affect neither dev server nor a build in Quasar, correct?

所以,server.url既不影响开发服务器,也不影响Quasar中的构建,对吗?

If you (or quasar) set the server.url, the app will load from that url, it can be a local liver reload server url or a remote website. But if you set it manually it has to match a server already running, it won’t make any changes in quasar server.

如果你(或准星体)设置了server.url,应用程序将从该url加载,它可以是本地肝脏再加载服务器url或远程网站。但如果您手动设置它,它必须与已经运行的服务器匹配,它不会在quasar服务器中进行任何更改。

Thanks for the detailed answer. The last part isn't precise but a good starting point for the research. Unfortunately, it appears that it's not possible to specify 127.0.0.1 for Capacitor in the config without modifying Quasar source code, even though it would be possible to make it work with Android Studio. Looks like specifying 192.168... ip and discarding the changes from git is the way to go

谢谢你的详细回答。最后一部分并不精确,但却是研究的一个很好的起点。不幸的是,在不修改Quasar源代码的情况下,似乎不可能在配置中为Capacitor指定127.0.0.1,即使它可以与Android Studio一起工作。看起来像是指定了192.168……IP和放弃来自Git的更改是大势所趋

@EstusFlask Good point. The first part of the answer should help with any modification to src-capacitor/capacitor.config.json

@EstusFlask点得很好。答案的第一部分应该有助于对src-deacitor.config.json进行任何修改

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com