Technical Articles
Create|Migrate|Publish native script plugins and consume in an MDK App
Introduction
- Creating a hello world Native Script plugin
- Migrating an existing Native Script plugin
- Publishing Native Script plugin to npm registry
- Using a Native Script plugin in MDK application
Creating a simple Native Script plugin
npm i <plugin-name> (or in case of MDK, referencing it in MDKProject.json)
- Click on “Use this template” button in https://github.com/NativeScript/plugin-seed
- Follow GitHub instructions (set repo name, visibility, description) & clone your new repo
- Now that we have the template in our local, execute the command
npm run setup
to set up the workspace
- Configure your npm scope by executing the command,
npm run config
- Since we are planning to publish it as a user scope package, name the scope with your npm repository user name
- Provide the rest of the details and we will get our workspace configured with the new scope.
- Now we need to add a new package, i.e the folder within which our actual plugin will reside
- Execute the command,
npm run add
- Let us name the package hello-world-toast.We can see that a new folder named hello-world-toast is created within the packages folder.
- Now let’s add a generic function in common.ts which can be used by Android or iOS platform, wherever we have the plugin used
export class HelloWorldToastCommon extends Observable { static sayHello(){ console.log("hello world!") } }
Note: You may also refer https://v7.docs.nativescript.org/plugins/plugin-reference to have a look at the basic structure of a plugin.
- Now that we have the functionality set for our plugin, let’s test it. The template has already taken care of the basic framework for tests. Within the tools/demo/hello-world-toast folder we have a file named index.ts where our plugin is already imported.
- Replace the contents of the testit() function within index.ts file as below
import { DemoSharedBase } from '../utils'; import {HelloWorldToast } from '@adilek/hello-world-toast'; export class DemoSharedHelloWorldToast extends DemoSharedBase { testIt() { HelloWorldToast.sayHello(); } }
- Now, we can test the implementation. On the plugin root folder runnpm start
- On click of “test hello world toast” the console logs hello world
- Now that we have the plugin, we can deploy it
- Navigate to the hello-world-toast folder in packages
- Execute npm publish –access public
- This will ask you to login with your npm user id
- Once logged in, the package will get published
Migrating an existing nativescript plugin
- Create a new package fetch-task
- Copy the below files from within the src folder of the native script-background-fetch git repo to the src folder of fetch-task package.
- Copy the below files from within the src folder of the native script-background-fetch git repo to the src folder of fetch-task package.
- Execute the command,
ns migrate (this command helps us migrate the library dependencies)
- Open the aar file in the previous version of the plugin in android studio and change the java versions and targetsdk versions, build and create a new aar file
- Add the aar file in platforms/android/libs folder in the fetch-task package
- Change the two entries in package.json as below
- Make changes as in the original native script-background-fetch plugin demo folder in the apps/demo/src folder
- Execute npm run start and run the package in android to check if the plugin is working properly
- Navigate to the fetch-task folder in packages
Publish NativeScript plugin to npm registry
- Execute npm publish –access public
- This will ask you to login with your npm user id
- Once logged in, the package will get published
You will be able to install it as @npmusername/package-name as in https://www.npmjs.com/package/@adilek/fetch-task
in any native script projects and for MDK, we can have it add adding it in NSPlugins section of MDKProject.json
Use NativeScript plugin in MDK application
Summary
Be the first to leave a comment
You must be Logged on to comment or reply to a post.