Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

Add support for react native environment #125

Merged
merged 21 commits into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 13 additions & 2 deletions src/core/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@ Logger.useDefaults()

// print header to console in browser environment
if (runtime.isBrowser) {
console.log(homepage+' '+version+' (@'+GIT_BRANCH+' #'+GIT_COMMIT.substr(0,7)+' '+BUILD_DATE+')' )
console.log(
homepage +
' ' +
version +
' (@' +
GIT_BRANCH +
' #' +
GIT_COMMIT.substr(0, 7) +
' ' +
BUILD_DATE +
')'
)
}

// global dependencies

// three.js
if (runtime.isNode) global.THREE = require('three')
if (runtime.isNode) global.THREE = runtime.require('three')
2 changes: 1 addition & 1 deletion src/core/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ if (runtime.isBrowser) {

// node: use module
global.performance = {
now: require('performance-now')
now: runtime.require('performance-now')
}

}
16 changes: 14 additions & 2 deletions src/core/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ var isNode = !!(
&& typeof process !== 'undefined'
&& Object.prototype.toString.call(process) === '[object process]'
)
// detect react native environment
var isReactNative = !!(
typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
)
var isBrowser = !isNode && typeof window !== 'undefined' && Object.prototype.toString.call(window) === '[object Window]'
// detect whether webgl is available
var webGlInfo = getWebGlInfo()
Expand All @@ -26,6 +30,7 @@ var runtime = {

isDebugMode: false,
isNode: isNode,
isReactNative: isReactNative,

// browser specific

Expand All @@ -50,10 +55,12 @@ var runtime = {
homepage: packageJson.homepage,
githubRepository: packageJson.repository,
gitBranchName: GIT_BRANCH,
gitCommitHash: GIT_COMMIT.substr(0,7),
gitCommitHash: GIT_COMMIT.substr(0, 7),
buildDate: BUILD_DATE,
license: packageJson.license
}
},

require: getDynamicRequire()

}

Expand All @@ -65,6 +72,11 @@ function assertBrowser(message) {
if (!isBrowser) throw (message || 'Sorry this feature requires a browser environment.')
}

// work around for react-native's metro bundler dynamic require check, see https://github.com/facebook/metro/issues/65
function getDynamicRequire() {
return typeof require !== 'undefined' ? require.bind(require) : null
}

function getWebGlInfo () {

var canvas = typeof document !== 'undefined' ? document.createElement('canvas') : null
Expand Down
4 changes: 2 additions & 2 deletions src/utils/file/gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ function deflateFile (file) {
// helpers

function loadDeflateLib () {
return runtime.isBrowser ? fetchScript(PAKO_LIB.deflate.url) : Promise.resolve(require(PAKO_LIB.deflate.module))
return runtime.isBrowser ? fetchScript(PAKO_LIB.deflate.url) : Promise.resolve(runtime.require(PAKO_LIB.deflate.module))
}

function loadInflateLib () {
return runtime.isBrowser ? fetchScript(PAKO_LIB.inflate.url) : Promise.resolve(require(PAKO_LIB.inflate.module))
return runtime.isBrowser ? fetchScript(PAKO_LIB.inflate.url) : Promise.resolve(runtime.require(PAKO_LIB.inflate.module))
}
4 changes: 3 additions & 1 deletion src/utils/io/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import runtime from '../../core/runtime.js'
export default (function(){

if (runtime.isNode) {
return require('node-fetch')
// overwrite whatwg-fetch polyfill
global.fetch = runtime.require('node-fetch')
return global.fetch
} else if (typeof fetch !== 'undefined') {
return fetch
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/io/form-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import runtime from '../../core/runtime.js'

var FormData_
if (runtime.isNode) {
FormData_ = require('form-data')
FormData_ = runtime.require('form-data')
} else if (typeof FormData !== 'undefined') {
FormData_ = FormData
} else {
console.warn('Missing FormData API.')
FormData_ = function FormDataError () {
FormData_ = function FormDataError() {
throw new Error('Missing FormData API.')
}
}

export default FormData_
export default FormData_
2 changes: 1 addition & 1 deletion test/aframe/component/architecture-toolkit/floor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import floor from '../../../../src/aframe/component/architecture-toolkit/floor.j
import applyDefaults from '../../../../src/scene/structure/apply-defaults.js';

// mock runtime module to prevent from tests blowing up
jest.mock('../../../../src/core/runtime.js', () => ({isBrowser: false, isNode: true}))
jest.mock('../../../../src/core/runtime.js', () => ({isBrowser: false, isNode: true, require: require}))

test('get floor data3d', () => {
const el3d = applyDefaults({type: 'floor'})
Expand Down
3 changes: 1 addition & 2 deletions test/aframe/component/architecture-toolkit/kitchen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import kitchen from '../../../../src/aframe/component/architecture-toolkit/kitch
import applyDefaults from '../../../../src/scene/structure/apply-defaults.js';

// mock runtime module to prevent from tests blowing up
jest.mock('../../../../src/core/runtime.js', () => ({isBrowser: false, isNode: true}))
jest.mock('../../../../src/core/runtime.js', () => ({isBrowser: false, isNode: true, require: require }))

test('get kitchen data3d', async () => {
let el3d = applyDefaults({type: 'kitchen'})

kitchen.attributes = el3d
kitchen.materials = {}
// kitchen mesh generation is async because of external dependencies
Expand Down
2 changes: 1 addition & 1 deletion test/furniture/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import getInfo from '../../src/furniture/get-info'
import get from '../../src/furniture/get'

// mock runtime module to prevent from tests blowing up
jest.mock('../../src/core/runtime.js', () => ({isBrowser: false, isNode: true}))
jest.mock('../../src/core/runtime.js', () => ({isBrowser: false, isNode: true, require: require}))

test('Furniture: search for chairs', async () => {
const result = await search('chair')
Expand Down
2 changes: 1 addition & 1 deletion test/scene/structure/get.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import getStructure from '../../../src/scene/structure/get'

// mock runtime module to prevent from tests blowing up
jest.mock('../../../src/core/runtime.js', () => ({isBrowser: false, isNode: true}))
jest.mock('../../../src/core/runtime.js', () => ({isBrowser: false, isNode: true, require: require}))

test('Scene: get sceneStructure form id', async () => {
const id = '5a281187-475a-4613-8fa5-a2e92af9914d'
Expand Down
3 changes: 2 additions & 1 deletion test/scene/structure/to-aframe-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { getAttributes, parseCameraBookmarks } from '../../../src/scene/structur
jest.mock('../../../src/core/runtime.js', () => ({
isBrowser: false,
assertBrowser: () => {},
isNode: true
isNode: true,
require: require
}))

test('sceneStructure to a-entity', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/staging/get-furnishings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '../../src/staging/get-furnishings'

// mock runtime module to prevent from tests blowing up
jest.mock('../../src/core/runtime.js', () => ({isBrowser: false, isNode: true}))
jest.mock('../../src/core/runtime.js', () => ({isBrowser: false, isNode: true, require: require}))

test('normalize sceneStructure input for home staging', async () => {
const sceneStructure = [{
Expand Down
2 changes: 1 addition & 1 deletion test/staging/replace-furniture.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import replaceFurniture from '../../src/staging/replace-furniture'

// mock runtime module to prevent from tests blowing up
jest.mock('../../src/core/runtime.js', () => ({isBrowser: false, isNode: true}))
jest.mock('../../src/core/runtime.js', () => ({isBrowser: false, isNode: true, require: require}))

// replace furniture
test('Replace furniture items', async () => {
Expand Down