Skip to content

Commit

Permalink
revert 7ac47c0 It breaks site generation infra
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarcos committed Jun 4, 2024
1 parent 74c7ec2 commit b98d2f1
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 58 deletions.
9 changes: 4 additions & 5 deletions docs/core/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ global. This same interface is also exposed if requiring with CommonJS

## Requiring `AFRAME` in a Node.js Environment

It is possible to run A-Frame in [Node.js](https://nodejs.org/en/about) to get access to its globals. The only catch is we need to supply a browser `window` mock since Node.js lacks a `window` object. You can do that with [jsdom-global](https://www.npmjs.com/package/jsdom-global), and you also need to mock `customElements`.
It is possible to run A-Frame in [Node.js](https://nodejs.org/en/about) to get access to its globals. The only catch is we need to supply a browser `window` mock since Node.js lacks a `window` object. A-Frame is tested with [jsdom](https://github.com/jsdom/jsdom), although any JavaScript-based browser implementation should work.

```js
const cleanup = require('jsdom-global')();
global.customElements = { define: function () {} };
const jsdom = require("jsdom");
global.window = new jsdom.JSDOM().window;
var aframe = require('aframe/src');
console.log(aframe.version);
cleanup();
```

You can't use jsdom to run tests with aframe components because `customElements` api is missing. A-Frame is using karma to open a real browser to run the tests.
Although A-Frame can load in Node.js, A-Frame isn't (yet) able to run any simulations at run time.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"test:firefox": "npm test -- --browsers Firefox",
"test:chrome": "npm test -- --browsers Chrome",
"test:nobrowser": "NO_BROWSER=true npm test",
"test:node": "node --experimental-require-module ./node_modules/mocha/bin/mocha --ui tdd tests/node"
"test:node": "mocha --ui tdd tests/node"
},
"repository": "aframevr/aframe",
"license": "MIT",
Expand All @@ -40,7 +40,7 @@
"deep-assign": "^2.0.0",
"load-bmfont": "^1.2.3",
"super-animejs": "^3.1.0",
"three": "npm:super-three@0.165.0",
"super-three": "0.165.0",
"three-bmfont-text": "dmarcos/three-bmfont-text#eed4878795be9b3e38cf6aec6b903f56acd1f695",
"webvr-polyfill": "^0.10.12"
},
Expand All @@ -60,8 +60,7 @@
"git-rev": "^0.2.1",
"glob": "^8.0.3",
"husky": "^0.11.7",
"jsdom": "^24.0.0",
"jsdom-global": "^3.0.2",
"jsdom": "^20.0.0",
"karma": "^6.4.0",
"karma-chai-shallow-deep-equal": "0.0.4",
"karma-chrome-launcher": "^3.1.1",
Expand Down
4 changes: 2 additions & 2 deletions src/components/scene/inspector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global AFRAME, INSPECTOR_VERSION */
/* global AFRAME */
var AFRAME_INJECTED = require('../../constants').AFRAME_INJECTED;
var pkg = require('../../../package');
var registerComponent = require('../../core/component').registerComponent;
Expand All @@ -16,7 +16,7 @@ function getFuzzyPatchVersion (version) {

var INSPECTOR_DEV_URL = 'https://aframe.io/aframe-inspector/dist/aframe-inspector.js';
var INSPECTOR_RELEASE_URL = 'https://unpkg.com/aframe-inspector@' + getFuzzyPatchVersion(pkg.version) + '/dist/aframe-inspector.min.js';
var INSPECTOR_URL = typeof INSPECTOR_VERSION !== 'undefined' && INSPECTOR_VERSION === 'dev' ? INSPECTOR_DEV_URL : INSPECTOR_RELEASE_URL;
var INSPECTOR_URL = process.env.INSPECTOR_VERSION === 'dev' ? INSPECTOR_DEV_URL : INSPECTOR_RELEASE_URL;
var LOADING_MESSAGE = 'Loading Inspector';
var LOADING_ERROR_MESSAGE = 'Error loading Inspector';

Expand Down
4 changes: 2 additions & 2 deletions src/core/scene/a-scene.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global Promise, customElements, screen, CustomEvent */
/* global Promise, screen, CustomEvent */
var initMetaTags = require('./metaTags').inject;
var initWakelock = require('./wakelock');
var loadingScreen = require('./loadingScreen');
Expand Down Expand Up @@ -920,7 +920,7 @@ function constrainSizeTo (size, maxSize) {
return size;
}

customElements.define('a-scene', AScene);
window.customElements.define('a-scene', AScene);

/**
* Return the canvas size where the scene will be rendered.
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ require('./extras/primitives/');

console.log('A-Frame Version: 1.6.0 (Date 2024-06-03, Commit #99844828)');
console.log('THREE Version (https://github.com/supermedium/three.js):',
THREE.REVISION);
pkg.dependencies['super-three']);
console.log('WebVR Polyfill Version:', pkg.dependencies['webvr-polyfill']);

// Wait for ready state, unless user asynchronously initializes A-Frame.
Expand Down
5 changes: 1 addition & 4 deletions src/lib/three.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
var THREE = require('./three.mjs').default;
// TODO: Eventually include these only if they are needed by a component.
global.THREE = THREE;
require('../../vendor/DeviceOrientationControls');
var THREE = require('./three.module.js').default;

// In-memory caching for XHRs (for images, audio files, textures, etc.).
if (THREE.Cache) {
Expand Down
21 changes: 0 additions & 21 deletions src/lib/three.mjs

This file was deleted.

24 changes: 24 additions & 0 deletions src/lib/three.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as SUPER_THREE from 'super-three';
import { DRACOLoader } from 'super-three/examples/jsm/loaders/DRACOLoader';
import { GLTFLoader } from 'super-three/examples/jsm/loaders/GLTFLoader';
import { KTX2Loader } from 'super-three/examples/jsm/loaders/KTX2Loader';
import { OBB } from 'super-three/addons/math/OBB.js';
import { OBJLoader } from 'super-three/examples/jsm/loaders/OBJLoader';
import { MTLLoader } from 'super-three/examples/jsm/loaders/MTLLoader';
import * as BufferGeometryUtils from 'super-three/examples/jsm/utils/BufferGeometryUtils';
import { LightProbeGenerator } from 'super-three/examples/jsm/lights/LightProbeGenerator';

var THREE = window.THREE = SUPER_THREE;

// TODO: Eventually include these only if they are needed by a component.
require('../../vendor/DeviceOrientationControls'); // THREE.DeviceOrientationControls
THREE.DRACOLoader = DRACOLoader;
THREE.GLTFLoader = GLTFLoader;
THREE.KTX2Loader = KTX2Loader;
THREE.OBJLoader = OBJLoader;
THREE.MTLLoader = MTLLoader;
THREE.OBB = OBB;
THREE.BufferGeometryUtils = BufferGeometryUtils;
THREE.LightProbeGenerator = LightProbeGenerator;

export default THREE;
3 changes: 1 addition & 2 deletions src/utils/debug.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var debug = require('debug');
var isBrowserEnvironment = require('./device').isBrowserEnvironment;

var settings = {
colors: {
Expand Down Expand Up @@ -115,6 +114,6 @@ if (ls && (parseInt(ls.logs, 10) || ls.logs === 'true')) {
debug.enable('*:error,*:info,*:warn');
}

if (isBrowserEnvironment) { window.logs = debug; }
if (process.browser) { window.logs = debug; }

module.exports = debug;
4 changes: 2 additions & 2 deletions src/utils/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ module.exports.isLandscape = function () {
* We need to check a node api that isn't mocked on either side.
* `require` and `module.exports` are mocked in browser by bundlers.
* `window` is mocked in node.
* `process` is also mocked by webpack running with karma, but has custom properties like process.browser.
* `process` is also mocked by browserify, but has custom properties.
*/
module.exports.isBrowserEnvironment = typeof process === 'undefined' || process.browser === true;
module.exports.isBrowserEnvironment = !!(!process || process.browser);

/**
* Check if running in node on the server.
Expand Down
9 changes: 1 addition & 8 deletions tests/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Karma configuration.
var path = require('path');
var glob = require('glob');
var webpack = require('webpack');
var webpackConfiguration = require('../webpack.config.js');
var webpackConfiguration = require("../webpack.config.js");

// Define test files.
var FILES = [
Expand Down Expand Up @@ -30,13 +29,7 @@ if (process.env.TEST_FILE) {

// add 'src' to be able to resolve require('utils/tracked-controls') for
// example in the tests
if (!webpackConfiguration.resolve) {
webpackConfiguration.resolve = {};
}
webpackConfiguration.resolve.modules = ['src', 'node_modules'];
webpackConfiguration.plugins.push(new webpack.ProvidePlugin({
process: 'process/browser'
}));
// webpack will create a lot of files, use build directory instead of dist
webpackConfiguration.output.path = path.resolve(__dirname, '../build');

Expand Down
19 changes: 13 additions & 6 deletions tests/node/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@
'use strict';

const path = require('path');
const assert = require('assert');
const assert = require('chai').assert;
const jsdom = require('jsdom');

suite('node acceptance tests', function () {
setup(function () {
this.jsdomCleanup = require('jsdom-global')();
global.customElements = { define: function () {} };
let _window = global.window = new jsdom.JSDOM().window;
global.navigator = _window.navigator;
global.document = _window.document;
global.screen = {};
global.HTMLElement = _window.Element;
global.window.customElements = {define: function () {}};
});

teardown(function () {
delete global.customElements;
this.jsdomCleanup();
delete global.window;
delete global.navigator;
delete global.document;
delete global.screen;
});

test('can run in node', function () {
const aframe = require(path.join(process.cwd(), 'src'));
let aframe = require(path.join(process.cwd(), 'src'));

assert.ok(aframe.version);
});
Expand Down
8 changes: 7 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ module.exports = {
},
plugins: [
new webpack.DefinePlugin({
INSPECTOR_VERSION: JSON.stringify(
'process.env.INSPECTOR_VERSION': JSON.stringify(
process.env.INSPECTOR_VERSION
)
}),
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer']
})
],
resolve: {
alias: {
three: 'super-three'
}
},
module: {
rules: [
{
Expand Down

0 comments on commit b98d2f1

Please sign in to comment.