ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.93k stars 996 forks source link

Multi-app projects config #691

Closed GuilhermeBCC closed 5 years ago

GuilhermeBCC commented 6 years ago

Feature request

Please add support for multiple projects in angular

Ionic is adding https://github.com/ionic-team/ionic-cli/issues/3281

Expected result: npx cap add android --project=app2 npx cap update --project=app2

kevin-lot commented 6 years ago

Question ??? In capicator configuration, "only" input and output entries for each project are needed.

GuilhermeBCC commented 6 years ago

Hello Kevin, could you send a configuration as an example? Is there any documentation on this? How would you configure the Capacitor for each project? Ionic and Angular use an extra configuration file at the root of each project. I think that capacitor should have too.

imhoffd commented 6 years ago

@GuilhermeBCC Putting multiple Capacitor configurations into a single file is a good feature request for some point in the future, but not particularly necessary to get a good experience in Ionic CLI projects.

For Cordova, which requires a single config.xml, the Ionic CLI uses the "active project" to spin up the Cordova CLI in different directories. I believe using this same approach for Capacitor will be the easiest. Please subscribe to https://github.com/ionic-team/ionic-cli/issues/3494 for updates on that.

sneko commented 5 years ago

@GuilhermeBCC any news on this request?

As described on https://github.com/ionic-team/capacitor/issues/342#issuecomment-434563021 I think it could be really useful to switch quickly between environments.

GuilhermeBCC commented 5 years ago

See https://github.com/ionic-team/ionic-docs/issues/83

imhoffd commented 5 years ago

@GuilhermeBCC Yep, this should be ready to go for Ionic apps: https://beta.ionicframework.com/docs/cli/configuration#multi-app

It's not a super smooth process yet, but hopefully it's usable 😄

But, again, I doubt this feature will make it into Capacitor itself.

GuilhermeBCC commented 5 years ago

https://beta.ionicframework.com/docs/cli/configuration/#adding-an-app

This approach has a lot of problems.

In fact using this you are not creating a project in monorepo but rather a standard project in the wrong place.

The project is not added to ionic.config.json neither the angular.json and the project does not extend tsconfig.json from root .

imhoffd commented 5 years ago

@GuilhermeBCC Please create an issue in the Ionic CLI repo and we can discuss.

GuilhermeBCC commented 5 years ago

@dwieeb I think I do not need to, since Ionic 4 are just web components. I do not think there's a need for that anymore. Monorepo is already in Angular.

I'd like to test Capacitor or Cordova support (for Monorepo). I saw that the issue was closed. Is there already a way to do this?

GuilhermeBCC commented 5 years ago

Knowing better the capacitor I see that there is no need for this feature so I am closing this issue.

For those who arrive here I leave my conclusion.

Capacitor is great. In fact it is the differential of the original Ionic3 framework. Ionic3 (great) was divided into Ionic 4 (cool) and Capacitor (great).

To work with Monorepo:

  1. Install @capacitor/cli (in root project)
  2. Create a new project called Myproject_android (by example) with the Capacitor cli; 2.1. Use the npx @capacitor/cli create command to create 2.2. Next command execute in project folder (Myproject_android)
  3. Add an android project: npx cap add android
  4. Build your original project (Web?) And move the output (www) to the www folder of Myproject_android;
  5. If there are Cordova plugins add them to the package.json of the project Myproject_android and run npm install.
  6. Sync with Android project: npx cap sync
  7. Open on Android Studio (npx cap open android) and build it for your device.

Very easy.

Thanks @dwieeb for your time and patience

matheo commented 5 years ago

@GuilhermeBCC I just found that it's possible! and indeed, capacitor needs to do nothing, it all on ionic!

With this config:

{
  "defaultProject": "my-ionic",
  "projects": {
    "my-ionic": {
      "name": "my-app",
      "type": "angular",
      "root": "apps/my-ionic",
      "integrations": {
        "capacitor": {}
      }
    }
  }
}

and having apps/my-app/capacitor.config.json we're able to run the ionic capacitor commands like ionic capacitor run android --livereload --address=0.0.0.0!

Awesome stuff!

cubicleWar commented 4 years ago

@GuilhermeBCC while capacitor can work with multi-project repo's it is a bit cumbersome to set up and requires the plugin to be installed via npm at both the root and app levels otherwise plugins will not be automatically imported into the native app. This voids some of the benefits of the monorepo approach.

It would be great if capacitor supported the --project and then searched root node_modules folder for plugins to import into that native project.

Alternatively the apps/app/capacitor.config.json could be used to specify which node_modules folder to search for plugins.

codearmadillo commented 4 years ago

Knowing better the capacitor I see that there is no need for this feature so I am closing this issue.

For those who arrive here I leave my conclusion.

Capacitor is great. In fact it is the differential of the original Ionic3 framework. Ionic3 (great) was divided into Ionic 4 (cool) and Capacitor (great).

To work with Monorepo:

  1. Install @capacitor/cli (in root project)
  2. Create a new project called Myproject_android (by example) with the Capacitor cli; 2.1. Use the npx @capacitor/cli create command to create 2.2. Next command execute in project folder (Myproject_android)
  3. Add an android project: npx cap add android
  4. Build your original project (Web?) And move the output (www) to the www folder of Myproject_android;
  5. If there are Cordova plugins add them to the package.json of the project Myproject_android and run npm install.
  6. Sync with Android project: npx cap sync
  7. Open on Android Studio (npx cap open android) and build it for your device.

Very easy.

Thanks @dwieeb for your time and patience

This is not easy. This is incredibly inefficient approach. Moreover, it completely defeats the whole purpose of mono-repos. I don't want to manage packages all around my repository, I want to have them centralized...

Coly010 commented 4 years ago

@dwieeb Following on from what @jiriKralovec has said, Nx Workspaces provides a monorepo solution that does not place a package.json within the app folder.
Due to this we get the following error:

[error] Capacitor needs to run at the root of an npm package.
Make sure you have a "package.json" in the directory where you run capacitor.

When we run ionic capacitor  commands in a multi-app ionic project with config such as:

{
  "defaultProject": "test-mobile-poc",
  "projects": {
    "test-mobile-poc": {
      "name": "My App",
      "integrations": {
        "capacitor": {}
      },
      "type": "angular",
      "root": "apps/test-mobile-poc"
    }
  }
}

Are there any properties we need to add to either the ionic.config.json or the capacitor.config.json files that will bypass the check for capacitor running at root level? Even if you execute the command ionic capacitor <command> from the root of the monorepo, the error above occurs. Here is my capacitor.config.json which is sitting at apps/test-mobile-poc level:

{
  "appId": "io.ionic.starter",
  "appName": "My App",
  "bundledWebRuntime": false,
  "npmClient": "npm",
  "webDir": "www",
  "cordova": {}
}

If there is additional work required to allow this to work, I'll gladly open a PR for it if I can figure it out. cc @mhartington @mlynch

EDIT: I've also noticed that using ionic capacitor add ios will add the @capacitor/ios dependency to the root package.json even if one exists in the app subfolder, and then trying to run ionic capacitor run ios will error with the following:

[ERROR] The project's package.json file seems malformed.
eduardoRoth commented 4 years ago

@Coly010

Not the best way, but a 'fix' to make capacitor work in a monorepo (I'm using Nx):

  1. cd to the app folder (e.g. apps/native-app) and create a package.json file with the following: package.json

    {
    "name": "app-native-name",
    "version": "0.0.0",
    "license": "MIT",
    "private": true,
    "dependencies": {},
    "devDependencies": {}
    }
  2. Then install capacitor npm install --save @capacitor/core @capacitor/cli. (I know, this kills the main reason to use a monorepo, but in the meantime, just update your .gitignore and /node_modules to node_modules)

  3. Run npx cap init, this creates a capacitor.config.json file for the project

  4. Modify capacitor.config.json``webDir property according to the path of the app dist folder (e.g. "webDir": "../../dist/apps/native-app")

  5. Add the required platforms:

    • npx cap add android
    • npx cap add ios

That's it for the install process!

When you want to run the app you'll have to:

  1. From root of the monorepo, run ng build app-native (or the name of your project)
  2. cd to the app folder (e.g. cd apps/native-app)
  3. In the same folder, run npx cap copy android (or ios)
  4. In the same folder, run npx cap run android(or ios)

Hope this helps!

eduardoRoth commented 4 years ago

@mhartington @mlynch ; I'll try to make some time this weekend and create a PR with support for monorepos with the previous steps.

Coly010 commented 4 years ago

@eduardoRoth I managed to get this set up in Nx with the capacitor deps at the root package JSON.

I did have to create a package.json in the app-specif directory, however, I only needed the following:

{
  "name": "my app"
}

Although the capacitor config and all cap commands must be run within that app directory also.

eduardoRoth commented 4 years ago

@Coly010 , if I don't install capacitor in the local folder, I get this error: image

Probably you have capacitor installed globally. Edit: Or I'm doing something wrong haha 😅

eduardoRoth commented 4 years ago

Following @matheo comment, you can use ionic cli:

This is the structure of my project (created a new one) image

ionic.config.json file

{
  "projects": {
    "native": {
      "name": "native",
      "integrations": {},
      "type": "angular"
    }
  }
}

You'll need to install capacitor as usual: npm install --save @capacitor/core @capacitor/cli

Then run ionic capacitor add android --project="native"

It'll create the capacitor.config.json file and then fail! Haha It's because it doesn't find the www folder

Update capacitor.config.json to set the new webDir folder to the right path: "webDir": "dist/apps/native",

Run ionic build --project="native" Run ionic capacitor add android --project="native" and this time it'll work

Then you can copy or run the project ionic capacitor copy android --project="native" ionic capacitor run android --project="native"

Although, this creates a root capacitor.config.json and doesn't work for multiple apps.

Edit: I'm not even sure that the --project="native" does something because it adds the configuration for root. I'll take a look

Coly010 commented 4 years ago

@eduardoRoth Yes, double checked my CD pipeline. Not installing the cap CLI globally, but I am using npx Ionic capacitor

Also, yes the multi-app setup works for a root ionic.config.json but I had to drop the capacitor.config.json into the local app folder

PhilippRoessner commented 3 years ago

Hey folks! I just tried to setup a mono repo project and when it comes to capacitor, my approach fails. I had the following project structure:

-myMonoRepoProject

The issue here is, that I had to create a package.json file with the @capacitor/cli devdependency inside the myIonicProject - folder. When I built the myIonicProject with the package.json, my build fails because of a conflict in of the rxjs library @capacitor/cli uses (conflicting with the rxjs lib in the node_modules of the root myMonoRepoProject). Without the package.json inside my myIonicProject the build is fine.

Any ideas how to solve this? Is the capacitor-cli going to support mono-repos in the future?

Radecom commented 3 years ago

Apparently, the Ionic Angular monorepos with NX publication and the Y @nxtend/capacitor were a success, as many developers are trying to move their projects to Monorepos workspaces. And that has led us to find different situations on this path.

For example, I am trying to get the plugin to work:

@codetrix-studio/capacitor-google-auth;

in an ionic-angular project inside an Nx workspace: But, once installed I get this error:

Error: Module not found: Error: Can't resolve '../../../../../capacitor.config.json'

and I had patched it by specifying the capacitor.config.json file location, but when developers work with multiple ionic projects. How are we going to patch it?

This has led me to think that maybe Capacitor needs to rethink the way to integrate the multi projects (Monorepos) feature to the Capacitor workflow since the current way forces us to have a node_modules folder inside our project and that breaks the monkey repo approach.

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.