Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support React Native #37

Merged
merged 21 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
544e411
Update build system to support a React Native bundle
smithki Apr 10, 2020
cecf18f
Add initial implementation for React Native (requires e2e testing)
smithki Apr 11, 2020
40d5e2c
Fix CJS builds and rename 'WebViewController' to 'ReactNativeWebViewC…
smithki Apr 11, 2020
60c926f
Remove unnecessary return statement
smithki Apr 11, 2020
063b820
Fix React dependency regex in Webpack config
smithki Apr 11, 2020
9e7e68d
Update CircleCI config
smithki Apr 11, 2020
ac50c28
Re-organize '/core' tests to reflect additional view controllers
smithki Apr 11, 2020
93e2657
Organizational comments
smithki Apr 11, 2020
d2e2021
Remove superfluous comment
smithki Apr 11, 2020
73e6c53
Add first draft CHANGELOG entry for React Native feature
smithki Apr 11, 2020
e6a1957
Update 'isViewReady' condition
smithki Apr 11, 2020
768dc7b
Package 'react-native-webview' and 'whatwg-url' in the RN bundle
smithki Apr 11, 2020
1abad60
Add .vscode to git ignores
smithki Apr 11, 2020
337e6c3
Add /dist to eslint ignores
smithki Apr 11, 2020
c62a6a6
Clean up Webpack config
smithki Apr 11, 2020
520e510
Externalize 'react-native-webview' & fix event listener bug in 'Paylo…
smithki Apr 14, 2020
857e503
Merge branch 'master' into react_native
smithki Apr 17, 2020
a690a39
Merge branch 'master' into react_native
smithki Apr 21, 2020
5385d97
Merge branch 'master' into react_native
smithki Apr 21, 2020
849d1b0
React native unit tests (#49)
smithki Apr 22, 2020
62cedc0
Add some testing cleanups
smithki Apr 22, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Package 'react-native-webview' and 'whatwg-url' in the RN bundle
  • Loading branch information
smithki committed Apr 15, 2020
commit 768dc7b41a5d6f5d674c876cfb527d1ee44c3dbf
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
"lint": "eslint --fix src/**/*.ts",
"clean": "npm-run-all -s clean:*",
"clean:test-artifacts": "rimraf coverage && rimraf .nyc_output",
"clean:build": "rimraf dist && rimraf RN",
"clean:build": "rimraf dist",
"clean_node_modules": "rimraf node_modules"
},
"dependencies": {},
"dependencies": {
"react-native-webview": "^8.1.2",
"whatwg-url": "^8.0.0"
},
"devDependencies": {
"@ikscodes/browser-env": "~0.3.1",
"@ikscodes/eslint-config": "~6.2.0",
Expand All @@ -32,6 +35,7 @@
"@types/react-native": "^0.62.2",
"@types/sinon": "~7.5.0",
"@types/webpack": "~4.41.0",
"@types/whatwg-url": "^6.4.0",
"@typescript-eslint/eslint-plugin": "~2.17.0",
"ava": "2.2.0",
"cli-glob": "^0.1.0",
Expand All @@ -49,7 +53,6 @@
"prettier": "~1.19.1",
"react": "^16.13.1",
"react-native": "^0.62.2",
"react-native-webview": "^9.1.4",
"rimraf": "~3.0.0",
"sinon": "7.1.1",
"ts-loader": "~6.2.1",
Expand Down
2 changes: 1 addition & 1 deletion react-native.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './dist/react-native/index.react-native';
export * from './dist/react-native/index.react-native.d.ts';
2 changes: 1 addition & 1 deletion react-native.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./dist/react-native');
module.exports = require('./dist/react-native/index.js');
7 changes: 4 additions & 3 deletions src/core/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { MagicSDKAdditionalConfiguration } from '../types';
import { RPCProviderModule } from '../modules/rpc-provider';
import { ViewController } from '../types/core/view-types';
import { ReactNativeWebViewController } from './views/react-native-webview-controller';
import { createURL } from '../util/url';

export class MagicSDK {
private static readonly __transports__: Map<string, PayloadTransport> = new Map();
Expand Down Expand Up @@ -44,12 +45,12 @@ export class MagicSDK {

const fallbackEndpoint = IS_REACT_NATIVE ? MGBOX_URL : MAGIC_URL;

this.endpoint = new URL(options?.endpoint ?? fallbackEndpoint).origin;
this.endpoint = createURL(options?.endpoint ?? fallbackEndpoint).origin;
this.encodedQueryParams = encodeQueryParameters({
API_KEY: this.apiKey,
DOMAIN_ORIGIN: window.location ? window.location.origin : '',
ETH_NETWORK: options?.network,
host: new URL(this.endpoint).host,
host: createURL(this.endpoint).host,
sdk: IS_REACT_NATIVE ? `${SDK_NAME}-rn` : SDK_NAME,
version: SDK_VERSION,
});
Expand Down Expand Up @@ -90,7 +91,7 @@ export class MagicSDK {
protected get overlay(): ViewController {
if (!MagicSDK.__overlays__.has(this.encodedQueryParams)) {
const controller = IS_REACT_NATIVE
? (undefined as any)
? new ReactNativeWebViewController(this.transport, this.endpoint, this.encodedQueryParams)
: new IframeController(this.transport, this.endpoint, this.encodedQueryParams);
MagicSDK.__overlays__.set(this.encodedQueryParams, controller);
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/views/iframe-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { MagicIncomingWindowMessage } from '../../types';
import { PayloadTransport } from '../payload-transport';
import { createDuplicateIframeWarning } from '../sdk-exceptions';
import { createURL } from '../../util/url';

/**
* Magic `<iframe>` overlay styles. These base styles enable `<iframe>` UI
Expand Down Expand Up @@ -70,8 +71,8 @@ export class IframeController {
if (!checkForSameSrcInstances(this.encodedQueryParams)) {
const iframe = document.createElement('iframe');
iframe.classList.add('magic-iframe');
iframe.dataset.magicIframeLabel = new URL(this.endpoint).host;
iframe.src = new URL(`/send?params=${this.encodedQueryParams}`, this.endpoint).href;
iframe.dataset.magicIframeLabel = createURL(this.endpoint).host;
iframe.src = createURL(`/send?params=${this.encodedQueryParams}`, this.endpoint).href;
applyOverlayStyles(iframe);
document.body.appendChild(iframe);
resolve(iframe);
Expand Down
1 change: 0 additions & 1 deletion src/util/type-guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/

import { JsonRpcRequestPayload, JsonRpcResponsePayload, MagicPayloadMethod, RPCErrorCode } from '../types';
import { ViewController } from '../types/core/view-types';

/**
* Assert `value` is `undefined`.
Expand Down
16 changes: 16 additions & 0 deletions src/util/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { URL as PolyfillURL } from 'whatwg-url';

/**
* Builds a `URL` object depending on the code environment (whether Browser or
* React Native).
*/
export function createURL(url: string, base?: string): URL {
// We only include the polyfill in the React Native-compatible bundle.
// Otherwise, we strip out the `whatwg-url` dependency completely.
if (PolyfillURL) {
return (new PolyfillURL(url, base) as unknown) as URL;
}

// This line should only be reached in the CJS/CDN bundles.
return new URL(url, base);
}
10 changes: 6 additions & 4 deletions webpack/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ function configBase(tsconfig: string, isReactNative = false) {
config.plugin('environment').use(EnvironmentPlugin, [envVariables]);
config.plugin('react-native-environment').use(DefinePlugin, [{ 'process.env.IS_REACT_NATIVE': isReactNative }]);

const reactDependencyRegex = /^(react|react-native|react-native-webview)$/;

if (isReactNative) {
// In React Native environments, we expect the developer to provide their
// own React dependencies, so we mark them as "externals".
config.externals(reactDependencyRegex);
config.externals(/^(react|react-native)$/);
} else {
// In browser environments, we must ensure that React dependencies are not
// included or `required` anywhere, so we force these modules to be replaced
// with an empty module.
config
.plugin('remove-react-dependencies')
.use(NormalModuleReplacementPlugin, [reactDependencyRegex, resolve(__dirname, '../src/noop-module.ts')]);
.use(NormalModuleReplacementPlugin, [
/^(react|react-native|react-native-webview|whatwg-url)$/,
resolve(__dirname, '../src/noop-module.ts'),
]);
}

config.resolve.extensions.merge(['.ts', '.tsx', '.js']);
Expand All @@ -57,6 +58,7 @@ configCJS.output
const configReactNative = configBase('tsconfig.react-native.json', true);
configReactNative.name('react-native');
configReactNative.entry('main').add('./src/index.react-native.ts');
configReactNative.performance.hints(false);
configReactNative.output
.path(resolve(__dirname, '../dist/react-native'))
.filename('index.js')
Expand Down
Loading