feat(cli): Check npm workspace packages for plugins #7723
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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/
andandroid/
the same way it would with plugins insidenode_modules/
Testing
git clone https://github.com/laine-hallot/cap-cli-npm-workspace-example
cd cap-cli-npm-workspace-example/
npm i
npm -ws run build
npx -w my-app cap sync
@korterra/[email protected]
in the plugin listnpx -w my-app/ cap open ios
(ornpx -w my-app/ cap open android
)Dev notes
About the dependency
So I decided to pull in
@npmcli/map-workspaces
to match paths/globs inworkspace:[]
partially out of laziness but mostly since its the same package thatnpm
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 likeyarn
andpnpm
(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 tostring
(again I'm just guessing this implementation wont work outside ofnpm
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 likenpmWorkspaces?: 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 runnpx cap sync
. I figure its not a big deal though sincesync
never handled building your apps JS/TS code anyways.Screenshots