diff --git a/src/core/bootstrap.js b/src/core/bootstrap.js index ef008fe..fd55af0 100644 --- a/src/core/bootstrap.js +++ b/src/core/bootstrap.js @@ -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') \ No newline at end of file +if (runtime.isNode) global.THREE = runtime.require('three') diff --git a/src/core/polyfills.js b/src/core/polyfills.js index 1f4e5e6..2ce1962 100644 --- a/src/core/polyfills.js +++ b/src/core/polyfills.js @@ -97,7 +97,7 @@ if (runtime.isBrowser) { // node: use module global.performance = { - now: require('performance-now') + now: runtime.require('performance-now') } } \ No newline at end of file diff --git a/src/core/runtime.js b/src/core/runtime.js index 98776c4..33f46dd 100644 --- a/src/core/runtime.js +++ b/src/core/runtime.js @@ -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() @@ -26,6 +30,7 @@ var runtime = { isDebugMode: false, isNode: isNode, + isReactNative: isReactNative, // browser specific @@ -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() } @@ -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 diff --git a/src/utils/file/gzip.js b/src/utils/file/gzip.js index 6dfb1a3..cd3958d 100644 --- a/src/utils/file/gzip.js +++ b/src/utils/file/gzip.js @@ -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)) } \ No newline at end of file diff --git a/src/utils/io/fetch.js b/src/utils/io/fetch.js index 9c92f40..6331ca6 100644 --- a/src/utils/io/fetch.js +++ b/src/utils/io/fetch.js @@ -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 { diff --git a/src/utils/io/form-data.js b/src/utils/io/form-data.js index 8b9718c..b75a615 100644 --- a/src/utils/io/form-data.js +++ b/src/utils/io/form-data.js @@ -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_ \ No newline at end of file +export default FormData_ diff --git a/test/aframe/component/architecture-toolkit/floor.js b/test/aframe/component/architecture-toolkit/floor.js index b9d3f25..ec05d90 100644 --- a/test/aframe/component/architecture-toolkit/floor.js +++ b/test/aframe/component/architecture-toolkit/floor.js @@ -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'}) diff --git a/test/aframe/component/architecture-toolkit/kitchen.js b/test/aframe/component/architecture-toolkit/kitchen.js index edd066f..38f3c56 100644 --- a/test/aframe/component/architecture-toolkit/kitchen.js +++ b/test/aframe/component/architecture-toolkit/kitchen.js @@ -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 diff --git a/test/furniture/search.js b/test/furniture/search.js index 633367f..efb6e6f 100644 --- a/test/furniture/search.js +++ b/test/furniture/search.js @@ -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') diff --git a/test/scene/structure/get.js b/test/scene/structure/get.js index e353948..14fdd4e 100644 --- a/test/scene/structure/get.js +++ b/test/scene/structure/get.js @@ -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' diff --git a/test/scene/structure/to-aframe-elements.js b/test/scene/structure/to-aframe-elements.js index 68f86ad..a7163e1 100644 --- a/test/scene/structure/to-aframe-elements.js +++ b/test/scene/structure/to-aframe-elements.js @@ -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', () => { diff --git a/test/staging/get-furnishings.js b/test/staging/get-furnishings.js index 68964bd..76e3259 100644 --- a/test/staging/get-furnishings.js +++ b/test/staging/get-furnishings.js @@ -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 = [{ diff --git a/test/staging/replace-furniture.js b/test/staging/replace-furniture.js index 30f175d..a7beb15 100644 --- a/test/staging/replace-furniture.js +++ b/test/staging/replace-furniture.js @@ -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 () => {