From 7ed524517d0166ee01207b2a6820f7b865fc9769 Mon Sep 17 00:00:00 2001 From: j Date: Wed, 18 Feb 2026 18:16:43 +0100 Subject: [PATCH] nodejs fixes --- package.json | 4 +++- source/Ox/js/Collection.js | 3 ++- source/Ox/js/Constants.js | 16 +++++++++------- source/Ox/js/Core.js | 1 - source/Ox/js/DOM.js | 7 +++++++ source/Ox/js/Request.js | 4 ++++ source/Ox/js/Type.js | 12 +++++++----- 7 files changed, 32 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 0002b464..0b8332b1 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,10 @@ { "name": "@0x2620/oxjs", "version": "1.0.0", - "module": "min/Ox.esm.js", + "main": "min/Ox/Ox.js", + "module": "min/Ox/Ox.js", "browser": "min/Ox.js", + "type": "module", "devDependencies": { "@rollup/plugin-node-resolve": "^16.0.3", "@rollup/plugin-terser": "^0.4.4", diff --git a/source/Ox/js/Collection.js b/source/Ox/js/Collection.js index 8e636783..1b695fe0 100644 --- a/source/Ox/js/Collection.js +++ b/source/Ox/js/Collection.js @@ -466,6 +466,7 @@ Ox.slice = Ox.toArray = function(collection, start, stop) { // value. Firefox 3.6 returns an array of undefined values if a string is passed // as value. if ( + typeof document !== 'undefined' && ( Ox.slice([0]).length == 0 || Ox.slice('0')[0] === null || Ox.slice('0')[0] === void 0 @@ -474,7 +475,7 @@ if ( return Ox.slice(document.getElementsByTagName('a')); } catch (error) {} }()) -) { +)) { // FIXME: remove toArray alias Ox.slice = Ox.toArray = function(collection, start, stop) { var args = stop === void 0 ? [start] : [start, stop], diff --git a/source/Ox/js/Constants.js b/source/Ox/js/Constants.js index 34e7e6e5..22c04459 100644 --- a/source/Ox/js/Constants.js +++ b/source/Ox/js/Constants.js @@ -93,13 +93,15 @@ Ox.SHORT_MONTHS = Ox.MONTHS.map(function(val) { }); //@ Ox.PATH Path of Ox.js Ox.PATH = (function() { - // IE8 can't apply slice to NodeLists, see Ox.slice - var index, regexp = /Ox\.js(\?.+|)$/, - scripts = document.getElementsByTagName('script'), src; - for (index = scripts.length - 1; index >= 0; index--) { - src = scripts[index].src; - if (regexp.test(src)) { - return src.replace(regexp, ''); + if (typeof document !== 'undefined') { + // IE8 can't apply slice to NodeLists, see Ox.slice + var index, regexp = /Ox\.js(\?.+|)$/, + scripts = document.getElementsByTagName('script'), src; + for (index = scripts.length - 1; index >= 0; index--) { + src = scripts[index].src; + if (regexp.test(src)) { + return src.replace(regexp, ''); + } } } // FIXME: fix path detection diff --git a/source/Ox/js/Core.js b/source/Ox/js/Core.js index aeb847a6..6ce6ed9a 100644 --- a/source/Ox/js/Core.js +++ b/source/Ox/js/Core.js @@ -69,7 +69,6 @@ Ox.load = function() { callback(true); } else { let fn = Ox.noop - console.log("!!", modules) if ('UI' in modules) { fn = function(callback) { var path = Ox.PATH + 'UI/jquery/jquery-1.7.1.min.js?' + Ox.VERSION; diff --git a/source/Ox/js/DOM.js b/source/Ox/js/DOM.js index 1069617e..2b3cc251 100644 --- a/source/Ox/js/DOM.js +++ b/source/Ox/js/DOM.js @@ -829,6 +829,13 @@ Ox.documentReady Calls a callback function once the DOM is ready @*/ Ox.documentReady = (function() { var callbacks = []; + if (typeof document === 'undefined') { + return function(callback) { + callback(); + return true; + } + } + document.onreadystatechange = globalThis.onload = function() { if (document.readyState == 'complete') { callbacks.forEach(function(callback) { diff --git a/source/Ox/js/Request.js b/source/Ox/js/Request.js index aafe734e..c8355e16 100644 --- a/source/Ox/js/Request.js +++ b/source/Ox/js/Request.js @@ -88,6 +88,10 @@ Ox.getAsync = function(urls, get, callback) { (function() { + if (typeof document == 'undefined') { + console.log("running in node, disable some things in Requets.js") + return + } var cache = {}, head = document.head || document.getElementsByTagName('head')[0] diff --git a/source/Ox/js/Type.js b/source/Ox/js/Type.js index 1331f331..251063d5 100644 --- a/source/Ox/js/Type.js +++ b/source/Ox/js/Type.js @@ -443,11 +443,13 @@ Ox.typeOf = function(value) { // Mobile Safari returns 'DOMWindow' for null and undefined // Firefox 30+ returns 'window' for window if ( - Ox.typeOf((function() { return arguments; }())) != 'arguments' - || Ox.typeOf(document.getElementsByTagName('a')) != 'nodelist' - || Ox.typeOf(null) != 'null' - || Ox.typeOf(window) != 'global' - || Ox.typeOf() != 'undefined' + (typeof document != 'undefined') && ( + Ox.typeOf((function() { return arguments; }())) != 'arguments' + || Ox.typeOf(document.getElementsByTagName('a')) != 'nodelist' + || Ox.typeOf(null) != 'null' + || Ox.typeOf(window) != 'global' + || Ox.typeOf() != 'undefined' + ) ) { Ox.typeOf = function(value) { var type = Object.prototype.toString.call(