Skip to content

Commit 40ecb6d

Browse files
FatmeFatme
Fatme
authored and
Fatme
committed
Merge pull request #547 from NativeScript/ikoevska/plugins.md
Reworking Plugins.md
2 parents f012542 + afc43b9 commit 40ecb6d

File tree

1 file changed

+185
-66
lines changed

1 file changed

+185
-66
lines changed

PLUGINS.md

Lines changed: 185 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,149 @@
11
PLUGINS
22
=========
33

4-
NativeScript plugin is a npm package that consist of the following:
4+
Starting with NativeScript CLI 1.1.0, you can develop or use plugins in your NativeScript projects.
55

6-
* Metadata about the package itself: name, version, supported runtime versions, etc.
7-
* CommonJS module(one or many) that expose the native API via a single JavaScript API.
8-
* Native libraries and resources.
6+
* [What Are NativeScript Plugins](#what-are-nativescript-plugins)
7+
* [Create a Plugin](#create-a-plugin)
8+
* [Directory Structure](#directory-structure)
9+
* [Package.json Specification](#package-json-specification)
10+
* [Install a Plugin](#install-a-plugin)
11+
* [Valid Plugin Sources](#valid-plugin-sources)
12+
* [Installation Specifics](#installation-specifics)
13+
* [Manual Steps After Installation](#manual-steps-after-installation)
14+
* [Use a Plugin](#use-a-plugin)
15+
* [Remove a Plugin](#remove-a-plugin)
16+
* [Removal Specifics](#removal-specifics)
17+
* [Manual Steps After Removal](#manual-steps-after-removal)
18+
19+
## What Are NativeScript Plugins
20+
21+
A NativeScript plugin is any npm package, published or not, that exposes a native API via JavaScript and consists of the following elements.
22+
23+
* A `package.json` file which contains the following metadata: name, version, supported runtime versions, dependencies and others. For more information, see the [Package.json Specification](#package-json-specification) section.
24+
* One or more CommonJS modules that expose a native API via a unified JavaScript API. For more information about Common JS modules, see the [CommonJS Wiki](http://wiki.commonjs.org/wiki/CommonJS).
25+
* `AndroidManifest.xml` and `Info.plist` which describe the permissions, features or other configurations required or used by your app for Android and iOS, respectively.
26+
27+
The plugin must have the directory structure, described in the [Directory Structure](#directory-structure) section.
28+
29+
## Create a Plugin
30+
31+
If the NativeScript framework does not expose a native API that you need, you can develop a plugin which exposes the required functionality. When you develop a plugin, keep in mind the following requirements.
32+
33+
* The plugin must be a valid npm package.
34+
* The plugin must expose a built-in native API. Currently, you cannot expose native APIs available via custom native libraries.
35+
* The plugin must be written in JavaScript or TypeScript and must comply with the CommonJS specification. If written in TypeScript, make sure to include the compiled `JavaScript` file in your plugin.
36+
* The plugin directory structure must comply with the specification described below.
37+
* The plugin must contain a valid `package.json` which complies with the specification described below.
38+
* If the plugin requires any permissions, features or other configuration specifics, it must contain `AndroidManifest.xml` and `Info.plist` file which describe them.
39+
40+
### Directory Structure
41+
42+
NativeScript plugins which consist of one CommonJS module must have the following directory structure.
943

10-
A package json file has the following structure:
1144
```
12-
{
13-
"name": "myplugin",
14-
"version": "0.0.1",
15-
"nativescript": {
16-
"platforms": {
17-
"ios": "1.0.0",
18-
"android": "1.1.0"
19-
}
20-
}
21-
}
45+
my-plugin/
46+
├── index.js
47+
├── package.json
48+
└── platforms/
49+
├── android/
50+
│ └── AndroidManifest.xml
51+
└── ios
52+
└── Info.plist
2253
```
23-
`nativescript` key is a mandatory section for a NativeScript plugin. `platforms` section describes the supported runtime versions for each platform.
2454

25-
A plugin directory has the following structure:
55+
NativeScript plugins which consist of multiple CommonJS modules must have the following directory structure.
56+
57+
```
58+
my-plugin/
59+
├── package.json
60+
├── MyModule1/
61+
│ ├── index1.js
62+
│ └── package.json
63+
├── MyModule2/
64+
│ ├── index2.js
65+
│ └── package.json
66+
└── platforms/
67+
├── android/
68+
│ └── AndroidManifest.xml
69+
└── ios
70+
└── Info.plist
2671
```
27-
myPlugin/
28-
-- index.js
29-
-- package.json
30-
-- platforms
31-
-- Android
32-
-- myjar.jar
33-
-- Android.manifest
34-
-- iOS
35-
-- myFramework.framework
36-
-- Info.plist
72+
73+
* `index.js`: This file is the CommonJS module which exposes the native API. You can use platform-specific `*.platform.js` files. For example: `index.ios.js` and `index.android.js`. During the plugin installation, the NativeScript CLI will copy the platform resources to the `tns_modules` subdirectory in the correct platform destination in the `platforms` directory of your project.
74+
* `package.json`: This file contains the metadata for your plugin. It sets the supported runtimes, the plugin name and version and any dependencies. The `package.json` specification is described in detail below.
75+
* `platforms\android\AndroidManifest.xml`: This file describes any specific configuration changes required for your plugin to work. For example: required permissions. For more information about the format of `AndroidManifest.xml`, see [App Manifest](http://developer.android.com/guide/topics/manifest/manifest-intro.html).<br/>During the plugin installation, the NativeScript CLI will merge the plugin `AndroidManifest.xml` with the `AndroidManifest.xml` for your project. The NativeScript CLI will not resolve any contradicting or duplicate entries during the merge. After the plugin is installed, you need to manually resolve such issues.
76+
* `platforms\ios\Info.plist`: This file describes any specific configuration changes required for your plugin to work. For example: required permissions. For more information about the format of `Info.plist`, see [About Information Property List Files](https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/AboutInformationPropertyListFiles.html).<br/>During the plugin installation, the NativeScript CLI will merge the plugin `Info.plist` with the `Info.plist` for your project. The NativeScript CLI will not resolve any contradicting or duplicate entries during the merge. After the plugin is installed, you need to manually resolve such issues.
77+
78+
> **IMPORTANT:** Currently, you cannot add any platform-specific resources other than `AndroidManifest.xml` and `Info.plist` in the `platforms` directory of the plugin.
79+
80+
### Package.json Specification
81+
82+
Every NativeScript plugin should contain a valid `package.json` file in its root. This `package.json` file must meet the following requirements.
83+
84+
* It must comply with the [npm specification](https://docs.npmjs.com/files/package.json).<br/>The `package.json` must contain at least `name` and `version` pairs. You will later use the plugin in your code by requiring it by its `name`.
85+
* It must contain a `nativescript` section which describes the supported NativeScript runtimes and their versions. This section can be empty. If you want to define supported platforms and runtimes, you can nest a `platforms` section. In this `platforms` section, you can nest `ios` and `android` key-value pairs. The values in these pairs must be valid runtime versions or ranges of values specified by a valid semver(7) syntax.
86+
* If the plugin depends on other npm modules, it must contain a `dependencies` section as described [here](https://docs.npmjs.com/files/package.json#dependencies).<br/>The NativeScript CLI will resolve the dependencies during the plugin installation.
87+
88+
#### Package.json Example
89+
90+
The following is an example of a `package.json` file for a NativeScript plugin which supports the 1.0.0 version of the iOS runtime and the 1.1.0 version of the Android runtime.
91+
92+
```JSON
93+
{
94+
"name": "myplugin",
95+
"version": "0.0.1",
96+
"nativescript": {
97+
"platforms": {
98+
"ios": "1.0.0",
99+
"android": "1.1.0"
100+
}
101+
}
102+
}
37103
```
38104

39-
You can use `platforms` folder and place platform specific files in each platform directory.
105+
## Install a Plugin
40106

41-
Here is an example.
107+
To install a plugin for your project, inside your project, run the following command.
42108

43-
Let you say that you have the following `Android.manifest` file in `platforms\android\` folder in your plugin:
109+
```Shell
110+
tns plugin add <Plugin>
44111
```
112+
113+
### Valid Plugin Sources
114+
115+
You can specify a plugin by name in the npm registry, local path or URL. The following are valid values for the `<Plugin>` attribute.
116+
117+
* A `<Name>` or `<Name>@<Version>` for plugins published in the npm registry.
118+
* A `<Local Path>` to the directory which contains the plugin files and its `package.json` file.
119+
* A `<Local Path>` to a `.tar.gz` archive containing a directory with the plugin and its `package.json` file.
120+
* A `<URL>` which resolves to a `.tar.gz` archive containing a directory with the plugin and its `package.json` file.
121+
* A `<git Remote URL>` which resolves to a `.tar.gz` archive containing a directory with the plugin and its `package.json` file.
122+
123+
### Installation Specifics
124+
125+
The installation of a NativeScript plugin mimics the installation of an npm module.
126+
127+
The NativeScript CLI takes the plugin and installs it to the `node_modules` directory in the root of your project. During this process, the NativeScript CLI resolves any dependencies described in the plugin `package.json` file and adds the plugin to the project `package.json` file in the project root.
128+
129+
Next, the NativeScript CLI runs a partial `prepare` operation for the plugin for all platforms configured for the project. During this operation, the CLI copies only the plugin to the `tns_modules` subdirectories in the `platforms\android` and `platform\ios` directories in your project. If your plugin contains platform-specific `JS` files, the CLI copies them to the respective platform subdirectory and renames them by removing the platform modifier.
130+
131+
> **TIP:** If you have not configured any platforms, when you run `$ tns platform add`, the NativeScript CLI will automatically prepare all installed plugins for the selected platform.
132+
133+
Finally, the CLI merges the plugin `AndroidManifest.xml` and `Info.plist` files with `platforms\android\AndroidManifest.xml` and `platforms\ios\Info.plist` in your project.
134+
135+
> **IMPORTANT:** Currently, the merging of the platform configuration files does not resolve any contradicting or duplicate entries.
136+
137+
#### AndroidManifest.xml Merge Example
138+
139+
The following is an example of a plugin `AndroidManifest`, project `AndroidManifest.xml` and the resulting merged file after the plugin installation.
140+
141+
**The Plugin Manifest**
142+
143+
```XML
45144
<?xml version="1.0" encoding="UTF-8"?>
46-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
47-
package="com.example.android.basiccontactables"
48-
android:versionCode="1"
49-
android:versionName="1.0" >
50-
51-
<uses-sdk
52-
android:minSdkVersion="19"
53-
android:targetSdkVersion="21" />
54-
145+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
146+
55147
<uses-permission android:name="android.permission.READ_CONTACTS"/>
56148
<uses-permission android:name="android.permission.INTERNET" />
57149
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
@@ -63,32 +155,12 @@ Let you say that you have the following `Android.manifest` file in `platforms\an
63155
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
64156
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
65157

66-
<application
67-
android:name="com.tns.NativeScriptApplication"
68-
android:allowBackup="true"
69-
android:icon="@drawable/icon"
70-
android:label="@string/app_name"
71-
android:theme="@style/AppTheme" >
72-
<activity
73-
android:name="com.tns.NativeScriptActivity"
74-
android:label="@string/title_activity_kimera"
75-
android:configChanges="keyboardHidden|orientation|screenSize">
76-
77-
<intent-filter>
78-
<action android:name="android.intent.action.MAIN" />
79-
<action android:name="android.intent.action.EDIT" />
80-
<action android:name="android.intent.action.VIEW" />
81-
82-
<category android:name="android.intent.category.LAUNCHER" />
83-
</intent-filter>
84-
</activity>
85-
</application>
86-
87158
</manifest>
88159
```
89160

90-
and the default `Android.manifest` file in your app in `platforms\android\` folder.
91-
```
161+
**The Project Manifest Located in `platforms\android\`**
162+
163+
```XML
92164
<?xml version="1.0" encoding="utf-8"?>
93165
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
94166
package="org.nativescript.test"
@@ -124,8 +196,9 @@ and the default `Android.manifest` file in your app in `platforms\android\` fold
124196
</manifest>
125197
```
126198

127-
The produced result xml will be:
128-
```
199+
**The Merged Manifest Located in `platforms\android\`**
200+
201+
```XML
129202
<?xml version="1.0" encoding="utf-8"?>
130203
<manifest
131204
xmlns:android="http://schemas.android.com/apk/res/android" package="org.nativescript.test" android:versionCode="1" android:versionName="1.0">
@@ -140,8 +213,8 @@ The produced result xml will be:
140213
<uses-permission android:name="android.permission.CALL_PHONE"/>
141214
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
142215
<!--
143-
Some comment here
144-
-->
216+
Some comment here
217+
-->
145218
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
146219
<application android:name="com.tns.NativeScriptApplication" android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme">
147220
<activity android:name="com.tns.NativeScriptActivity" android:label="@string/title_activity_kimera" android:configChanges="keyboardHidden|orientation|screenSize">
@@ -156,4 +229,50 @@ The produced result xml will be:
156229
</manifest>
157230
```
158231

232+
### Manual Steps After Installation
233+
234+
After the installation is complete, you need to open `platforms\android\AndroidManifest.xml` and `platforms\ios\Info.plist` in your project and inspect them for duplicate or contradicting entries. Make sure to preserve the settings required by the plugin. Otherwise, your app might not build or it might not work as expected, when deployed on device.
235+
236+
## Use a Plugin
237+
238+
To use a plugin inside your project, you need to add a `require` in your app.
239+
240+
```JavaScript
241+
var myPlugin = require("myplugin");
242+
```
243+
244+
This will look for a `myplugin` module with a valid `package.json` file in the `tns_modules` directory. Note that you must require the plugin with the value for the `name` key in the plugin `package.json` file.
245+
246+
## Remove a Plugin
247+
248+
To remove a plugin from your project, inside your project, run the following command.
249+
250+
```Shell
251+
tns plugin remove <Plugin>
252+
```
253+
254+
You must specify the plugin by the value for the `name` key in the plugin `package.json` file.
255+
256+
### Removal Specifics
257+
258+
The removal of a NativeScript plugin mimics the removal of an npm module.
259+
260+
The NativeScript CLI removes any plugin files from the `node_modules` directory in the root of your project. During this process, the NativeScript CLI removes any dependencies described in the plugin `package.json` file and removes the plugin from the project `package.json` file in the project root.
261+
262+
> **IMPROTANT:** This operation does not remove files from the `platforms\android` and `platforms\ios` directories and does not unmerge the `AndroidManifest.xml` and `Info.plist` files.
263+
264+
### Manual Steps After Removal
265+
266+
After the plugin removal is complete, you need to run the following command.
267+
268+
```Shell
269+
tns prepare <Platform>
270+
```
271+
272+
Make sure to run the command for all platforms configured for the project. During this operation, the NativeScript CLI will remove any leftover plugin files from your `platforms\android` and `platforms\ios` directories.
273+
274+
> **TIP:** Instead of `$ tns prepare` you can run `$ tns build`, `$ tns run`, `$ tns deploy` or `$ tns emulate`. All these commands run `$ tns prepare`.
275+
276+
Next, open your `platforms\android\AndroidManifest.xml` and `platforms\ios\Info.plist` files and remove any leftover entries from the plugin `AndroidManifest.xml` and `Info.plist` files.
159277

278+
Finally, make sure to update your code not to use the uninstalled plugin.

0 commit comments

Comments
 (0)