Skip to main content

Packaging the Plugin

The ScreenOrientation plugin is functionally complete and integrated into the Napacitor application as a local plugin. However, the ScreenOrientation plugin can't be used by other Napacitor applications in its current state.

Let's go ahead and package the plugin for publishing to make the ScreenOrientation plugin globally available.

Note: This section references steps and procedures from the Creating Napacitor Plugins portion of the documentation. Please refer to the documentation for details beyond the scope of this tutorial.

Creating the plugin package​

Unlike Capacitor, Napacitor does not ship a plugin generator — the layout is simple enough to create by hand. Create a new folder screen-orientation/ outside your app with this structure:

screen-orientation/
├── package.json
├── tsconfig.json
├── src/
│ ├── definitions.ts
│ ├── index.ts
│ └── web.ts
└── maui/
├── Napacitor.ScreenOrientation.csproj
└── ScreenOrientationPlugin.cs

package.json follows the @napacitor/* convention:

{
"name": "@napacitor/screen-orientation",
"version": "0.0.1",
"description": "Work with the screen orientation in a common way for Android, iOS, macOS, Windows, and web",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/",
"maui/",
"!maui/bin/",
"!maui/obj/"
],
"scripts": {
"build": "tsc"
},
"peerDependencies": {
"@napacitor/core": "^0.1.0"
},
"devDependencies": {
"@napacitor/core": "^0.1.0",
"typescript": "~5.4.5"
}
}

Key points:

  • maui/ is shipped inside the npm package. nap sync compiles it as part of the host app — that is how the C# class reaches every platform.
  • bin/ and obj/ are excluded from the published package.
  • The compiled dist/ output is committed and published; consumers use the package without running a build step.

Port the plugin code​

The structure matches what we built inside the application, which makes porting trivial:

  1. Copy src/plugins/screen-orientation/definitions.ts, index.ts, and web.ts into src/.
  2. Copy maui/ScreenOrientationPlugin.cs (including ScreenOrientationResult) into maui/. Update the namespace to match the package.
  3. Create maui/Napacitor.ScreenOrientation.csproj as described in Project Setup, with AssemblyName set to Napacitor.ScreenOrientation — the Napacitor prefix is required for host discovery.

Build to verify the TypeScript compiles:

npm install
npm run build

Note: You can test the plugin before publishing it by installing it into a local project with npm install ../screen-orientation, then running npx nap sync. See Plugin Development Workflow for details.

Update the plugin documentation​

Document the plugin's API in README.md — the installation instructions are what developers will copy:

Install​

npm install @napacitor/screen-orientation
npx nap sync

Don't forget to run npx nap sync: it regenerates maui/napacitor.build.props with a project reference to the plugin's C# project. Without it, the plugin is not compiled into the app and calls fail with unimplemented.

Note: Always document any modifications developers will need to make when installing or configuring plugins you build.

Publishing the plugin​

The plugin is in a state where it can be published to an npm registry. We won't do that in this tutorial, but note that the command to publish a Napacitor plugin project is the same as publishing any other npm package: npm publish.

You can publish a global Napacitor plugin to the public npm registry, a private registry, or just link it to a bunch of projects locally on your machine. Whatever fits your use-case.

Conclusion​

Napacitor's Plugin API is a flexible and robust solution to supplement Napacitor applications with native functionality unavailable to the web, whether the need is to add custom C# code to a specific application or reuse native code between a fleet of apps — on Android, iOS, macOS, and Windows, all from a single C# class.

Looking forward to the plugin you develop next! 🎉