Skip to content

Commit

Permalink
Create react-native build pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
legastero committed Apr 24, 2019
1 parent 07bfeb8 commit 87f21a7
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 80 deletions.
27 changes: 19 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
"description": "Modern XMPP in the browser, with a JSON API",
"version": "11.2.1",
"author": "Lance Stout <lancestout@gmail.com>",
"browser": {
"node-stringprep": false,
"ws": false,
"stream": "readable-stream",
"./dist/es/lib/async/index.js": "./dist/es/lib/async/index-browser.js",
"./dist/es/lib/crypto/index.js": "./dist/es/lib/crypto/index-browser.js",
"./dist/es/lib/fetch/index.js": "./dist/es/lib/fetch/index-browser.js"
"stanzajs_env_mappings": {
"browser": {
"./dist/es/lib/async/index.js": "./dist/es/lib/async/index-browser.js",
"./dist/es/lib/crypto/index.js": "./dist/es/lib/crypto/index-browser.js",
"./dist/es/lib/fetch/index.js": "./dist/es/lib/fetch/index-browser.js",
"./dist/es/lib/stringprep/index.js": "./dist/es/lib/stringprep/index-browser.js",
"./dist/es/lib/ws/index.js": "./dist/es/lib/ws/index-browser.js"
},
"react-native": {
"./dist/es/lib/async/index.js": "./dist/es/lib/async/index-browser.js",
"./dist/es/lib/crypto/index.js": "./dist/es/lib/crypto/index-react-native.js",
"./dist/es/lib/fetch/index.js": "./dist/es/lib/fetch/index-browser.js",
"./dist/es/lib/stringprep/index.js": "./dist/es/lib/stringprep/index-browser.js",
"./dist/es/lib/ws/index.js": "./dist/es/lib/ws/index-browser.js"
}
},
"bugs": "https://github.com/legastero/stanza.io/issues",
"contributors": [
Expand All @@ -19,11 +27,14 @@
"dependencies": {
"@types/async": "^2.4.0",
"@types/node": "^11.13.6",
"@types/readable-stream": "^2.3.1",
"@types/ws": "^6.0.1",
"async": "^2.6.2",
"async-es": "^2.6.2",
"cross-fetch": "^3.0.2",
"jxt": "^4.0.0",
"react-native-randombytes": "^3.5.2",
"readable-stream": "^2.3.6",
"sdp": "^2.9.0",
"tslib": "^1.9.3",
"uuid": "^3.3.2",
Expand All @@ -38,7 +49,6 @@
"rimraf": "^2.6.2",
"rollup": "^1.0.0",
"rollup-plugin-node-resolve": "^4.0.0",
"string-replace-loader": "^2.1.1",
"tap-spec": "^5.0.0",
"tape": "^4.8.0",
"ts-node": "^8.0.3",
Expand Down Expand Up @@ -95,6 +105,7 @@
"compile:module": "tsc -p . --outDir ./dist/es --target es2015 --module es2015",
"compile:rollup": "rollup -c rollup.config.js",
"compile:rollup-browser": "rollup -c rollup-browser.config.js",
"compile:rollup-react-native": "rollup -c rollup-react-native.config.js",
"compile:webpack": "webpack --mode production",
"lint": "tslint -p .",
"test": "nyc ts-node --files test/index.js | tap-spec && nyc report --reporter=text",
Expand Down
1 change: 1 addition & 0 deletions rollup-browser.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import resolve from 'rollup-plugin-node-resolve';

import baseconfig from './rollup.config';

export default {
Expand Down
9 changes: 9 additions & 0 deletions rollup-react-native.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import baseconfig from './rollup-browser.config';

export default {
...baseconfig,
output: {
file: 'dist/es/index-rn.module.js',
format: 'es'
}
};
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default {
'jxt',
'punycode',
'sdp',
'readable-stream',
'stream',
'tslib',
'uuid',
Expand Down
46 changes: 40 additions & 6 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,69 @@
const FS = require('fs');
const Child = require('child_process').execSync;

const Pkg = JSON.parse(FS.readFileSync('package.json'));

function fileReplace(fileName, placeholder, value) {
const originalFile = FS.readFileSync(fileName).toString();
FS.writeFileSync(fileName, originalFile.replace(placeholder, value));
}

function writeOverrides(overrides = undefined) {
FS.writeFileSync(
'package.json',
JSON.stringify(
{
...Pkg,
browser: overrides
},
null,
4
) + '\n'
);
}

// Copy local package files
Child('npm run clean');
Child('npm run compile');
Child('npm run compile:module');

// Embed package version into CJS and ES modules
fileReplace('dist/cjs/index.js', '__STANZAJS_VERSION__', Pkg.version);
fileReplace('dist/es/index.js', '__STANZAJS_VERSION__', Pkg.version);

Child('npm run compile:rollup');

// Create browser specific module
writeOverrides(Pkg.stanzajs_env_mappings.browser);
Child('npm run compile:rollup-browser');

// Create react-native specific module
writeOverrides(Pkg.stanzajs_env_mappings['react-native']);
Child('npm run compile:rollup-react-native');

// Clean up overrides
writeOverrides(undefined);

Child('mkdir dist/npm');
Child('cp -r dist/cjs/* dist/npm/');
Child('cp dist/es/index.module.js dist/npm/module.js');
Child('cp dist/es/index-browser.module.js dist/npm/browser-module.js');
Child('cp dist/es/index-rn.module.js dist/npm/rn-module.js');
Child(`cp ${__dirname}/../*.md dist/npm`);
Child('npm run compile:webpack');

// Create package.json file
const Pkg = JSON.parse(FS.readFileSync('package.json'));
FS.writeFileSync(
'dist/npm/package.json',
JSON.stringify(
{
...Pkg,
browser: {
...Pkg.browser,
'./module.js': './browser-module.js'
},
browser: './browser-module.js',
devDependencies: undefined,
'jsnext:main': './module.js',
main: './index.js',
module: './module.js',
private: false,
'react-native': './rn-module.js',
scripts: undefined,
sideEffects: false,
typings: './index'
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as jid from './protocol/jid';
import Client from './client';
import Plugins from './plugins';

export const VERSION = '__STANZAIO_VERSION__';
export const VERSION = '__STANZAJS_VERSION__';
export const JID = jid.JID;

export { Client, jid };
Expand Down
2 changes: 1 addition & 1 deletion src/lib/crypto/Hash.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// tslint:disable no-bitwise

import { Transform } from 'stream';
import { Transform } from 'readable-stream';

export default abstract class Hash extends Transform {
protected _block: Buffer;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/crypto/index-browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// tslint:disable no-bitwise

import { Transform } from 'stream';
import { Transform } from 'readable-stream';

import Hash from './Hash';
import MD5 from './MD5';
Expand Down
9 changes: 9 additions & 0 deletions src/lib/crypto/index-react-native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as RNRandomBytes from 'react-native-randombytes';

import { createHash, createHmac, getHashes, Hash, Hmac } from './index-browser';

export { Hash, Hmac, createHash, createHmac, getHashes };

export function randomBytes(size: number) {
return RNRandomBytes.randomBytes(size);
}
19 changes: 19 additions & 0 deletions src/lib/stringprep/index-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const punycode = require('punycode');

export const NATIVE_STRINGPREP = false;

export function toUnicode(data) {
return punycode.toUnicode(data);
}

export function nameprep(str) {
return str.toLowerCase();
}

export function nodeprep(str) {
return str.toLowerCase();
}

export function resourceprep(str) {
return str;
}
46 changes: 46 additions & 0 deletions src/lib/stringprep/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const punycode = require('punycode');

let StringPrep;
try {
StringPrep = require('node-stringprep');
} catch (err) {
StringPrep = false;
}

const HAS_STRINGPREP = !!StringPrep && !!StringPrep.StringPrep;
export const NATIVE_STRINGPREP = HAS_STRINGPREP && new StringPrep.StringPrep('nodeprep').isNative();

export function toUnicode(data) {
if (HAS_STRINGPREP) {
return punycode.toUnicode(StringPrep.toUnicode(data));
} else {
return punycode.toUnicode(data);
}
}

export function nameprep(str) {
if (HAS_STRINGPREP) {
const name = new StringPrep.StringPrep('nameprep');
return name.prepare(str);
} else {
return str.toLowerCase();
}
}

export function nodeprep(str) {
if (HAS_STRINGPREP) {
const node = new StringPrep.StringPrep('nodeprep');
return node.prepare(str);
} else {
return str.toLowerCase();
}
}

export function resourceprep(str) {
if (HAS_STRINGPREP) {
const resource = new StringPrep.StringPrep('resourceprep');
return resource.prepare(str);
} else {
return str;
}
}
1 change: 1 addition & 0 deletions src/lib/ws/index-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default WebSocket;
3 changes: 3 additions & 0 deletions src/lib/ws/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import WS from 'ws';

export default WS;
47 changes: 1 addition & 46 deletions src/protocol/jid.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,4 @@
const punycode = require('punycode');

let StringPrep;
try {
StringPrep = require('node-stringprep');
} catch (err) {
StringPrep = false;
}

const HAS_STRINGPREP = !!StringPrep && !!StringPrep.StringPrep;
export const NATIVE_STRINGPREP = HAS_STRINGPREP && new StringPrep.StringPrep('nodeprep').isNative();

export function toUnicode(data) {
if (HAS_STRINGPREP) {
return punycode.toUnicode(StringPrep.toUnicode(data));
} else {
return punycode.toUnicode(data);
}
}

export function nameprep(str) {
if (HAS_STRINGPREP) {
const name = new StringPrep.StringPrep('nameprep');
return name.prepare(str);
} else {
return str.toLowerCase();
}
}

export function nodeprep(str) {
if (HAS_STRINGPREP) {
const node = new StringPrep.StringPrep('nodeprep');
return node.prepare(str);
} else {
return str.toLowerCase();
}
}

export function resourceprep(str) {
if (HAS_STRINGPREP) {
const resource = new StringPrep.StringPrep('resourceprep');
return resource.prepare(str);
} else {
return str;
}
}
import { NATIVE_STRINGPREP, toUnicode, nameprep, nodeprep, resourceprep } from '../lib/stringprep';

// All of our StringPrep fallbacks work correctly
// in the ASCII range, so we can reliably mark
Expand Down
5 changes: 1 addition & 4 deletions src/transports/websocket.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import WildEmitter from 'wildemitter';
import * as async from '../lib/async';
import WS from '../lib/ws';

let WS = require('ws');
if (typeof WS !== 'function') {
WS = window.WebSocket;
}
const WS_OPEN = 1;

export default class WSConnection extends WildEmitter {
Expand Down
3 changes: 3 additions & 0 deletions typings/react-native-randombytes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'react-native-randombytes' {
export function randomBytes(size: number): Buffer;
}
13 changes: 0 additions & 13 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ module.exports = {
path: Path.resolve('dist')
},

module: {
rules: [
{
loader: 'string-replace-loader',
options: {
replace: Pkg.version,
search: '__STANZAIO_VERSION__'
},
test: /index\.[jt]s/
}
]
},

plugins: [
new BundleAnalyzerPlugin({
analyzerMode: 'static',
Expand Down

0 comments on commit 87f21a7

Please sign in to comment.