Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide platform (and other metadata) when generating configs #3976

Open
imhoffd opened this issue Dec 22, 2020 · 4 comments
Open

Provide platform (and other metadata) when generating configs #3976

imhoffd opened this issue Dec 22, 2020 · 4 comments
Milestone

Comments

@imhoffd
Copy link
Contributor

imhoffd commented Dec 22, 2020

When we run npx cap sync with capacitor.config.{ts,js} files, we can provide context (e.g. with environment variables) so app developers can differentiate configuration values based on it.

For example, launchShowDuration can be 0 for iOS and 3000 for Android:

 import { CapacitorConfig } from '@capacitor/cli';

 const config: CapacitorConfig = {
   appId: 'com.capacitorjs.testapp',
   appName: 'capacitor-testapp',
   webDir: 'build',
   plugins: {
     SplashScreen: {
+      launchShowDuration: process.env.CAPACITOR_PLATFORM === 'ios' ? 0 : 3000,
     },
   },
 };

 export = config;

An alternative would be to provide Platform.select() like React Native (see #3977). Example:

launchShowDuration: Platform.select({ android: 3000, default: 0 }),
@imhoffd imhoffd added this to the 3.0.0-rc milestone Dec 22, 2020
@imhoffd imhoffd modified the milestones: 3.0.0-rc, 3.1.0 Jan 8, 2021
@jgoux
Copy link

jgoux commented Feb 3, 2021

I was going to open an issue about it!

It will be really useful for development too :

import type { CapacitorConfig } from '@capacitor/cli';
import getInternalIp from "internal-ip";

let server: CapacitorConfig["server"] = {};
if (process.env.CAPACITOR_PLATFORM === "android") {
  server.cleartext = true;
  server.url = `http://${getInternalIp.v4.sync()}:3000`;
} else if (process.env.CAPACITOR_PLATFORM === "ios") {
  server.url = "http://localhost:3000";
}

const config: CapacitorConfig = {
  appId: 'pro.clovis.app',
  appName: 'Clovis',
  android: {
    path: 'platforms/android',
  },
  bundledWebRuntime: false,
  ios: {
    path: 'platforms/ios'
  },
  server,
  webDir: 'platforms/web',
};

export default config;

@gerritvanaaken
Copy link

I would appreciate this.

const config: CapacitorConfig = {
  appId: 'de.sharegroup.scouter',
  appName: 'scouter',
  webDir: 'dist',
  server: {
    hostname: 'scouter.de'
    //hostname: process.env.CAPACITOR_PLATFORM === "android" ? 'app.scouter.de' : 'scouter.de'
  }
};

@alex-steinberg
Copy link

Since there are already ios and md properties in the root, it would be simpler allow the general properties such as server to be specified separately per platform (i.e. server could be in the root or in md.server and ios.server -- or both, with the more specific being preferred).

Personally, I have an app that, for various reasons, has a different id on Android to iOS so I would benefit from appId being specified per platform.

@nseb
Copy link

nseb commented Aug 30, 2021

it's works or not , use process.env in Capacitor File ?

RagingCactus added a commit to RagingCactus/Beanconqueror that referenced this issue Oct 4, 2024
To retain all data in the web view IndexedDB when updating from Cordova
builds, it is necessary to retain the same origin for the app files the
Cordova version used.

Both the scheme and the hostname are different between iOS and Android,
but Capacitor only support changing the scheme natively per platform. As
a workaround, the configuration now dynamically parses its own command
line arguments to determine the platform that is currently being
configured. Until Capacitor exposes the current platform in a documented
way (see ionic-team/capacitor#3976), this will
have to suffice.

This slightly changes the workflow. Now, it is not possible to run
"npx cap sync" anymore to sync all platforms. Instead, the platforms
must be synced separately, for example using "npx cap sync android" and
"npx cap sync ios". For convenience, the command "npm run capsync" is
also provided to sync all platforms.

Note that the project currently only contains an Android target. The
ios target must be added to the "npm run capsync" command once it is
part of the repository.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants