Skip to content

Commit afc43b9

Browse files
committed
Applied comments
1 parent 318023b commit afc43b9

File tree

1 file changed

+33
-59
lines changed

1 file changed

+33
-59
lines changed

PLUGINS.md

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PLUGINS
22
=========
33

4-
Starting with NativeScript CLI 1.1.0, you can develop or use custom plugins in your NativeScript projects.
4+
Starting with NativeScript CLI 1.1.0, you can develop or use plugins in your NativeScript projects.
55

66
* [What Are NativeScript Plugins](#what-are-nativescript-plugins)
77
* [Create a Plugin](#create-a-plugin)
@@ -21,28 +21,28 @@ Starting with NativeScript CLI 1.1.0, you can develop or use custom plugins in y
2121
A NativeScript plugin is any npm package, published or not, that exposes a native API via JavaScript and consists of the following elements.
2222

2323
* 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 single 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 and features required or used by your app for Android and iOS, respectively.
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.
2626

2727
The plugin must have the directory structure, described in the [Directory Structure](#directory-structure) section.
2828

2929
## Create a Plugin
3030

31-
If the NativeScript framework does not expose a native API that you need, you can develop a custom plugin which exposes the required functionality. When you develop a custom plugin, keep in mind the following requirements.
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.
3232

3333
* The plugin must be a valid npm package.
3434
* 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 and must comply with the CommonJS specification.
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.
3636
* The plugin directory structure must comply with the specification described below.
3737
* The plugin must contain a valid `package.json` which complies with the specification described below.
38-
* The plugin must contain `AndroidManifest.xml` and `Info.plist` file which describe any platform-specific settings such as required permissions.
38+
* If the plugin requires any permissions, features or other configuration specifics, it must contain `AndroidManifest.xml` and `Info.plist` file which describe them.
3939

4040
### Directory Structure
4141

4242
NativeScript plugins which consist of one CommonJS module must have the following directory structure.
4343

4444
```
45-
MyPlugin/
45+
my-plugin/
4646
├── index.js
4747
├── package.json
4848
└── platforms/
@@ -55,45 +55,41 @@ MyPlugin/
5555
NativeScript plugins which consist of multiple CommonJS modules must have the following directory structure.
5656

5757
```
58-
MyPlugin/
58+
my-plugin/
59+
├── package.json
5960
├── MyModule1/
6061
│ ├── index1.js
61-
│ ├── package.json
62-
│ └── platforms/
63-
│ ├── android/
64-
│ │ └── AndroidManifest.xml
65-
│ └── ios
66-
│ └── Info.plist
67-
└── MyModule2/
68-
├── index2.js
69-
├── package.json
70-
└── platforms/
71-
├── android/
72-
│ └── AndroidManifest.xml
73-
└── ios
74-
└── Info.plist
62+
│ └── package.json
63+
├── MyModule2/
64+
│ ├── index2.js
65+
│ └── package.json
66+
└── platforms/
67+
├── android/
68+
│ └── AndroidManifest.xml
69+
└── ios
70+
└── Info.plist
7571
```
7672

77-
* `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 their correct platform destination in the `platforms` directory of your project.
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.
7874
* `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.
7975
* `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.
8076
* `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.
8177

82-
Currently, you cannot add any platform-specific resources other than `AndroidManifest.xml` and `Info.plist` in the `platforms` directory of the plugin.
78+
> **IMPORTANT:** Currently, you cannot add any platform-specific resources other than `AndroidManifest.xml` and `Info.plist` in the `platforms` directory of the plugin.
8379
8480
### Package.json Specification
8581

8682
Every NativeScript plugin should contain a valid `package.json` file in its root. This `package.json` file must meet the following requirements.
8783

8884
* 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`.
89-
* 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.
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.
9086
* 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.
9187

9288
#### Package.json Example
9389

9490
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.
9591

96-
```
92+
```JSON
9793
{
9894
"name": "myplugin",
9995
"version": "0.0.1",
@@ -128,9 +124,13 @@ You can specify a plugin by name in the npm registry, local path or URL. The fol
128124

129125
The installation of a NativeScript plugin mimics the installation of an npm module.
130126

131-
The NativeScript CLI takes the plugin and copies it to the `app\tns_modules` directory inside 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.
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.
132130

133-
Next, the NativeScript CLI runs `$ tns prepare` for all platforms configured for the project. During this operation, the CLI copies 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. 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.
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.
134134

135135
> **IMPORTANT:** Currently, the merging of the platform configuration files does not resolve any contradicting or duplicate entries.
136136
@@ -142,15 +142,8 @@ The following is an example of a plugin `AndroidManifest`, project `AndroidManif
142142

143143
```XML
144144
<?xml version="1.0" encoding="UTF-8"?>
145-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
146-
package="com.example.android.basiccontactables"
147-
android:versionCode="1"
148-
android:versionName="1.0" >
145+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
149146

150-
<uses-sdk
151-
android:minSdkVersion="19"
152-
android:targetSdkVersion="21" />
153-
154147
<uses-permission android:name="android.permission.READ_CONTACTS"/>
155148
<uses-permission android:name="android.permission.INTERNET" />
156149
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
@@ -162,27 +155,6 @@ The following is an example of a plugin `AndroidManifest`, project `AndroidManif
162155
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
163156
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
164157

165-
<application
166-
android:name="com.tns.NativeScriptApplication"
167-
android:allowBackup="true"
168-
android:icon="@drawable/icon"
169-
android:label="@string/app_name"
170-
android:theme="@style/AppTheme" >
171-
<activity
172-
android:name="com.tns.NativeScriptActivity"
173-
android:label="@string/title_activity_kimera"
174-
android:configChanges="keyboardHidden|orientation|screenSize">
175-
176-
<intent-filter>
177-
<action android:name="android.intent.action.MAIN" />
178-
<action android:name="android.intent.action.EDIT" />
179-
<action android:name="android.intent.action.VIEW" />
180-
181-
<category android:name="android.intent.category.LAUNCHER" />
182-
</intent-filter>
183-
</activity>
184-
</application>
185-
186158
</manifest>
187159
```
188160

@@ -273,7 +245,7 @@ This will look for a `myplugin` module with a valid `package.json` file in the `
273245

274246
## Remove a Plugin
275247

276-
To install a plugin for your project, inside your project, run the following command.
248+
To remove a plugin from your project, inside your project, run the following command.
277249

278250
```Shell
279251
tns plugin remove <Plugin>
@@ -285,7 +257,7 @@ You must specify the plugin by the value for the `name` key in the plugin `packa
285257

286258
The removal of a NativeScript plugin mimics the removal of an npm module.
287259

288-
The NativeScript CLI removes any plugin files from the `app\tns_modules` directory inside 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.
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.
289261

290262
> **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.
291263
@@ -299,6 +271,8 @@ tns prepare <Platform>
299271

300272
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.
301273

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+
302276
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.
303277

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

0 commit comments

Comments
 (0)