Skip to content

Commit

Permalink
Enjoy, world
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Dec 27, 2016
0 parents commit 54d0f1c
Show file tree
Hide file tree
Showing 86 changed files with 1,472 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.idea
.vscode
*.js.map
*.log
!scripts/*.js
demo/*.d.ts
demo/lib
demo/platforms
demo/node_modules
node_modules
8 changes: 8 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.idea
.vscode
demo/
*.gif
*.png
*.log
*.map
!*.d.ts
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

nativescript-nodeify
Copyright (c) 2016, Eddy Verbruggen

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# NativeScript Nodeify
Makes most npm packages compatible with NativeScript

## Q & A
#### Q. WTF?
A. Good question, glad you asked! You can't just use any npm package with NativeScript as they may depend
on built-in Node modules (`fs`, `path`, `crypto` to name but a few).
Those modules aren't plain old JavaScript files so they can't be executed in the {N} runtimes.

#### Q. So how does this plugin overcome that situation?
A. You can install dependencies as normal, and this plugin as well, then at build time
a hook installed by this plugin will scan and modify your modules as it sees fit to make them {N}-compatible.

#### Q. Lol. Wut? Modify my precious modules!?
A. Yes. The hook looks at the `dependencies` node of your app's `package.json`, and for each dependency it finds it does a few things:
* Look for a `browser` node in their `package.json` and find-replaces any matching `require()` calls in that package and its dependencies.
* If there's a `main` replacement in the `browser` node it also takes care of that.
* If any of the dependencies (and this can go deeeeeeeeeeep - remember `left-pad`?) contains something we need to shim, we will based on [this list](https://github.com/EddyVerbruggen/nativescript-nodeify/blob/master/shims.json).
* There's more trickery and there may be more needed, so this list is a bit evolving atm..

#### Q. But doesn't browserify / Webpack solve this for us?
A. Not in this case, at least not without further modifications. Think modules that don't have a `browser` node in their `package.json`, or modules that do but shim their node dependency with something that needs a real browser..
Feel free to submit a PR for a nicer implementation, but this is the best I could think of.

#### Q. Not bad actually, but doesn't come with a performance hit?
A. Thanks. And good question. Most importantly, at runtime this should make no difference as you're not 'requiring'
more that you were already, just different implementations (that actually work, I hope).
A build time you will see a few seconds added to your build (but it shouldn't affect livesync).
The hook skips checking `tns-core-modules` and anything module starting with `nativescript` already, but build-performance could be better. It's just not the main priority for this first release.

## Installation
From the command prompt go to your app's root folder and execute:

```sh
tns plugin add nativescript-nodeify
```
## Usage
Include this in your code before requiring the problematic npm module.

```js
require("nativescript-nodeify");
```

## Demo app
[The demo](https://github.com/EddyVerbruggen/nativescript-nodeify) tests a few popular
libraries that depend on Node built-in modules which would normally not work in a NativeScript runtime environment.

Run the demo app from the root of the project: `npm run demo.ios` or `npm run demo.android`.

## Know issues
This plugin isn't perfect, but it tried to solve issues for as many of the gazillion npm packages out there. A few issues are known, if yours is not in this list please [create an issue](https://github.com/EddyVerbruggen/nativescript-nodeify/issues/new).

* Like Browserify we're using shims to fill the gaps between Node and the browser, but unlike Browserify shims we're not running in a browser, so some API's may not be available. Anything that touches the DOM for instance.
* On Android you may run into trouble when using npm packages ending with `.js`, [an issue has been submitted](https://github.com/NativeScript/android-runtime/issues/666) to the {N} Android runtime repo.

## Recipies
To get you started with a few popular npm modules, here's some recipies. Please share your own by sending a PR to this repo!

All recipies assume you've already done:

```bash
$ tns create awssdk
$ cd awssdk
$ tns platform add ios
$ tns platform add android
$ tns plugin add nativescript-nodeify
```

### `node-uuid`
```bash
$ npm install node-uuid --save
```
Boom! Done.

### `jsonwebtoken`
```bash
$ npm install jsonwebtoken --save
```
Boom! Done. Again.

### `amazon-cognito-identity-js` with `aws-sdk`
This one requires a bit more setup, but it's not too bad:

```bash
$ npm install amazon-cognito-identity-js --save
$ npm install nativescript-aws --save
```

```js
// require this to fix an issue with xhr event states
require('nativescript-nodeify');

// register a user (here's a bit, but see the demo and https://github.com/aws/amazon-cognito-identity-js for details)
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
var CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
var userPool = new CognitoUserPool({UserPoolId: 'foo', ClientId: 'bar'});

// then require AWS and interact with s3, dynamo, whatnot
var AWS = require('nativescript-aws');
```
For examples on AWS usage see [nativescript-aws](https://github.com/EddyVerbruggen/nativescript-aws#api).
43 changes: 43 additions & 0 deletions demo/app/App_Resources/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="__PACKAGE__"
android:versionCode="1"
android:versionName="1.0">

<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"/>

<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="__APILEVEL__"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:name="com.tns.NativeScriptApplication"
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<activity
android:name="com.tns.NativeScriptActivity"
android:label="@string/title_activity_kimera"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@style/LaunchScreenTheme">

<meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.tns.ErrorReportActivity"/>
</application>
</manifest>
16 changes: 16 additions & 0 deletions demo/app/App_Resources/Android/app.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Add your native dependencies here:

// Uncomment to add recyclerview-v7 dependency
//dependencies {
// compile 'com.android.support:recyclerview-v7:+'
//}

android {
defaultConfig {
generatedDensities = []
applicationId = "org.nativescript.starter"
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="fill">
<item>
<bitmap android:gravity="fill" android:src="@drawable/background" />
</item>
<item>
<bitmap android:gravity="center" android:src="@drawable/logo" />
</item>
</layer-list>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions demo/app/App_Resources/Android/values-v21/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ns_accent">#3d5afe</color>
</resources>
23 changes: 23 additions & 0 deletions demo/app/App_Resources/Android/values-v21/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- Application theme -->
<style name="AppTheme" parent="AppThemeBase">
<item name="android:datePickerStyle">@style/SpinnerDatePicker</item>
<item name="android:timePickerStyle">@style/SpinnerTimePicker</item>
</style>

<!-- Default style for DatePicker - in spinner mode -->
<style name="SpinnerDatePicker" parent="android:Widget.Material.Light.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>

<!-- Default style for TimePicker - in spinner mode -->
<style name="SpinnerTimePicker" parent="android:Widget.Material.Light.TimePicker">
<item name="android:timePickerMode">spinner</item>
</style>

<style name="NativeScriptToolbarStyle" parent="NativeScriptToolbarStyleBase">
<item name="android:elevation">4dp</item>
</style>
</resources>
7 changes: 7 additions & 0 deletions demo/app/App_Resources/Android/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ns_primary">#F5F5F5</color>
<color name="ns_primaryDark">#757575</color>
<color name="ns_accent">#33B5E5</color>
<color name="ns_blue">#272734</color>
</resources>
45 changes: 45 additions & 0 deletions demo/app/App_Resources/Android/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!-- theme to use FOR launch screen-->
<style name="LaunchScreenThemeBase" parent="Theme.AppCompat.Light.NoActionBar">
<item name="toolbarStyle">@style/NativeScriptToolbarStyle</item>

<item name="colorPrimary">@color/ns_primary</item>
<item name="colorPrimaryDark">@color/ns_primaryDark</item>
<item name="colorAccent">@color/ns_accent</item>

<item name="android:windowBackground">@drawable/splash_screen</item>

<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowTranslucentStatus">true</item>

</style>

<style name="LaunchScreenTheme" parent="LaunchScreenThemeBase">
</style>

<!-- theme to use AFTER launch screen is loaded-->
<style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar">
<item name="toolbarStyle">@style/NativeScriptToolbarStyle</item>

<item name="colorPrimary">@color/ns_primary</item>
<item name="colorPrimaryDark">@color/ns_primaryDark</item>
<item name="colorAccent">@color/ns_accent</item>

</style>

<style name="AppTheme" parent="AppThemeBase">
</style>

<!-- theme for actioon-bar -->
<style name="NativeScriptToolbarStyleBase" parent="Widget.AppCompat.Toolbar">
<item name="android:background">@color/ns_primary</item>
<item name="theme">@style/ThemeOverlay.AppCompat.ActionBar</item>
<item name="popupTheme">@style/ThemeOverlay.AppCompat</item>

</style>

<style name="NativeScriptToolbarStyle" parent="NativeScriptToolbarStyleBase">
</style>
</resources>
Loading

0 comments on commit 54d0f1c

Please sign in to comment.