diff --git a/ghu.js b/ghu.js index 994e2156..9bef5e0a 100644 --- a/ghu.js +++ b/ghu.js @@ -69,7 +69,7 @@ ghu.task('build:scripts', runtime => { return read(`${SRC}/_h5ai/public/js/scripts.js`) .then(newerThan(mapper, `${SRC}/_h5ai/public/js/**`)) .then(webpack(webpackConfig, {showStats: true})) - .then(wrap('\n\n// @include "vendor/*"\n\n')) + .then(wrap('\n\n// @include "global.js"\n\n')) .then(includeit()) .then(ife(() => runtime.args.production, uglify())) .then(wrap(runtime.commentJs)) diff --git a/src/_h5ai/private/php/pages/page.tpl.jade b/src/_h5ai/private/php/pages/page.tpl.jade index a42951af..9a7526b1 100644 --- a/src/_h5ai/private/php/pages/page.tpl.jade +++ b/src/_h5ai/private/php/pages/page.tpl.jade @@ -5,7 +5,6 @@ doctype html html(class='no-js', lang='en') head - meta(charset='utf-8') meta(http-equiv='x-ua-compatible', content='ie=edge') title #{title} diff --git a/src/_h5ai/public/js/global.js b/src/_h5ai/public/js/global.js new file mode 100644 index 00000000..020b63b5 --- /dev/null +++ b/src/_h5ai/public/js/global.js @@ -0,0 +1,45 @@ +(function check(win) { + /* eslint-disable func-names,no-console,no-var */ + + if (typeof win !== 'object' || win.window !== win || !win.document) { + throw new Error('no-window'); + } + + var noBrowser = 'no-browser'; + var docEl = win.document.documentElement; + docEl.className = ''; + + function assert(msg, expr) { + if (!expr) { + docEl.className = noBrowser; + throw new Error(noBrowser + ': ' + msg); + } + win.console.log('checked: ' + msg); + } + + assert('console', win.console && typeof win.console.log === 'function'); + assert('assign', win.Object && typeof win.Object.assign === 'function'); + assert('promise', win.Promise && typeof win.Promise === 'function'); + assert('history', win.history && typeof win.history.pushState === 'function'); + + assert('canvas', (function () { + var elem = win.document.createElement('canvas'); + return elem.getContext && elem.getContext('2d'); + }())); + + assert('storage', (function () { + var key = '#test#'; + try { + win.localStorage.setItem(key, key); + win.localStorage.removeItem(key); + return true; + } catch (e) { + return false; + } + }())); + + /* eslint-enable no-var */ +}(this)); + + +// @include "vendor/*.js" diff --git a/src/_h5ai/public/js/lib/core/modernizr.js b/src/_h5ai/public/js/lib/core/modernizr.js index abbc14b2..1ce99cdd 100644 --- a/src/_h5ai/public/js/lib/core/modernizr.js +++ b/src/_h5ai/public/js/lib/core/modernizr.js @@ -1,25 +1,5 @@ -const win = require('../win'); - -const hasCanvas = (() => { - const elem = win.document.createElement('canvas'); - return !!(elem.getContext && elem.getContext('2d')); -})(); - -const hasHistory = !!(win.history && win.history.pushState); - -const hasLocalStorage = (() => { - const key = '#test#'; - try { - win.localStorage.setItem(key, key); - win.localStorage.removeItem(key); - return true; - } catch (e) { - return false; - } -})(); - module.exports = { - canvas: hasCanvas, - history: hasHistory, - localstorage: hasLocalStorage + canvas: true, + history: true, + localstorage: true }; diff --git a/src/_h5ai/public/js/lib/win.js b/src/_h5ai/public/js/lib/win.js index e6798665..c131bc06 100644 --- a/src/_h5ai/public/js/lib/win.js +++ b/src/_h5ai/public/js/lib/win.js @@ -1,23 +1,5 @@ -const win = window; // eslint-disable-line no-undef +const win = module.exports = global.window; if (typeof win !== 'object' || win.window !== win || !win.document) { throw new Error('no-window'); } - -const noBrowser = 'no-browser'; -const doc = win.document; -const docEl = doc.documentElement; -docEl.className = ''; - -function assert(expr, hint) { - if (!expr) { - docEl.className = noBrowser; - throw new Error(`${noBrowser}: ${hint}`); - } -} - -assert(!doc.getElementById(noBrowser), 'ie<10'); -assert(typeof Object.assign === 'function', 'assign'); -assert(typeof Promise === 'function', 'promise'); - -module.exports = win;