diff --git a/src/core/component.js b/src/core/component.js index 658e92c5dd0..555da93ea34 100644 --- a/src/core/component.js +++ b/src/core/component.js @@ -1,5 +1,6 @@ -/* global HTMLElement */ +/* global HTMLElement, Node */ var schema = require('./schema'); +var scenes = require('./scene/scenes'); var systems = require('./system'); var utils = require('../utils/'); @@ -11,6 +12,10 @@ var isSingleProp = schema.isSingleProperty; var stringifyProperties = schema.stringifyProperties; var stringifyProperty = schema.stringifyProperty; var styleParser = utils.styleParser; +var warn = utils.debug('core:component:warn'); + +var aframeScript = document.currentScript; +var upperCaseRegExp = new RegExp('[A-Z]+'); /** * Component class definition. @@ -261,6 +266,11 @@ Component.prototype = { } }; +// For testing. +if (window.debug) { + var registrationOrderWarnings = module.exports.registrationOrderWarnings = {}; +} + /** * Registers a component to A-Frame. * @@ -272,13 +282,29 @@ module.exports.registerComponent = function (name, definition) { var NewComponent; var proto = {}; - var testForUpperCase = new RegExp('[A-Z]+'); + // Warning if component is statically registered after the scene. + if (document.currentScript && document.currentScript !== aframeScript) { + scenes.forEach(function checkPosition (sceneEl) { + // Okay to register component after the scene at runtime. + if (sceneEl.hasLoaded) { return; } + + // Check that component is declared before the scene. + if (document.currentScript.compareDocumentPosition(sceneEl) === + Node.DOCUMENT_POSITION_FOLLOWING) { return; } + + warn('The component `' + name + '` was registered in a