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

feat(cli): Check npm workspace packages for plugins #7723

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

laine-hallot
Copy link

Info

No one's really asked for this feature but it was straight forward to implement and pretty nice to have for my team so I figured I should still make a PR in case this is useful to anyone else.

This PR just lets you tell the capacitor CLI to check your workspace packages for plugins and copy those over to ios/ and android/ the same way it would with plugins inside node_modules/

Testing

  1. Clone the demo repo git clone https://github.com/laine-hallot/cap-cli-npm-workspace-example
  2. cd cap-cli-npm-workspace-example/
  3. npm i
  4. npm -ws run build
  5. npx -w my-app cap sync
  6. npx -w my-app/ cap open ios (or npx -w my-app/ cap open android)
  7. Build and run the app
  8. Once the finishes loading filling in the text box at the top of the screen then press the button right under it
    • The text you entered should show up next to the input and you should see output from the plugin in both the JS console and native logs

Dev notes

About the dependency

So I decided to pull in @npmcli/map-workspaces to match paths/globs in workspace:[] partially out of laziness but mostly since its the same package that npm itself uses to manage workspace paths.

https://www.npmjs.com/package/@npmcli/map-workspaces
https://github.com/npm/cli/blob/286739c0224bad88c4a38927bafd61973f71098c/lib/utils/get-workspaces.js#L2

I could get rid of it and write the glob matching from scratch if the maintainers want to keep dependencies to a minimum but I figured pulling that in was worth the piece of mind that comes with knowing that Capacitor's workspace path resolution is identical to npm's.

Config option

I added a new capacitor config option called workspaces and set this feature to only run if that option is enabled.

The type on workspaces only accepts 'npm' here since I'm pretty sure this won't work with other workspace setups in stuff like yarn and pnpm (Well I'm almost certain pnpm wont work but yarn might(??)).

If there's interest in fully supporting those package managers down the line then the type on workspaces could be expanded later with a union type like 'npm' | 'yarn' | 'pnpm' or just set to string (again I'm just guessing this implementation wont work outside of npm workspaces). If supporting other workspace setups isn't something the maintainers ever want to deal with then the config option could be changed to something like npmWorkspaces?: boolean.

Build quirks

A minor quirk of this implementation is that unlike with plugins inside node_modules/ you do need to make sure you run your build process on your plugin code before you run npx cap sync. I figure its not a big deal though since sync never handled building your apps JS/TS code anyways.

Screenshots

cap-cli-workpaces-demo-sync

cap-cli-workpaces-demo-app

@laine-hallot laine-hallot changed the title Feature: Check npm workspace packages for plugins feat: Check npm workspace packages for plugins Oct 24, 2024
@laine-hallot laine-hallot changed the title feat: Check npm workspace packages for plugins feat(cli): Check npm workspace packages for plugins Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant