From 780e6d37aa8be9c4029ac5b520c95d9a179bea3f Mon Sep 17 00:00:00 2001 From: Timi Aiyemo Date: Fri, 8 Nov 2019 14:41:21 -0500 Subject: [PATCH] refactor window and Date object reference --- lazysizes-umd.js | 8 +++----- lazysizes.js | 8 +++----- src/common.wrapper | 2 +- src/lazysizes-core.js | 10 ++++++---- src/lazysizes-intersection.js | 8 +++----- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/lazysizes-umd.js b/lazysizes-umd.js index 035bed0f..3a0e94da 100644 --- a/lazysizes-umd.js +++ b/lazysizes-umd.js @@ -1,5 +1,5 @@ (function(window, factory) { - var lazySizes = factory(window, window.document); + var lazySizes = factory(window, window.document, Date); if(typeof module == 'object' && module.exports){ module.exports = lazySizes; } else if (typeof define == 'function' && define.amd) { @@ -7,7 +7,7 @@ } else { window.lazySizes = lazySizes; } -}(window, function l(window, document) { +}(window, function l(window, document, Date) { 'use strict'; /*jshint eqnull:true */ @@ -58,15 +58,13 @@ var docElem = document.documentElement; - var Date = window.Date; - var supportPicture = window.HTMLPictureElement; var _addEventListener = 'addEventListener'; var _getAttribute = 'getAttribute'; - var addEventListener = window[_addEventListener]; + var addEventListener = window[_addEventListener].bind(window); var setTimeout = window.setTimeout; diff --git a/lazysizes.js b/lazysizes.js index 8b42441c..7353240b 100644 --- a/lazysizes.js +++ b/lazysizes.js @@ -1,11 +1,11 @@ (function(window, factory) { - var lazySizes = factory(window, window.document); + var lazySizes = factory(window, window.document, Date); window.lazySizes = lazySizes; if(typeof module == 'object' && module.exports){ module.exports = lazySizes; } }(typeof window != 'undefined' ? - window : {}, function l(window, document) { + window : {}, function l(window, document, Date) { 'use strict'; /*jshint eqnull:true */ @@ -56,15 +56,13 @@ var docElem = document.documentElement; - var Date = window.Date; - var supportPicture = window.HTMLPictureElement; var _addEventListener = 'addEventListener'; var _getAttribute = 'getAttribute'; - var addEventListener = window[_addEventListener]; + var addEventListener = window[_addEventListener].bind(window); var setTimeout = window.setTimeout; diff --git a/src/common.wrapper b/src/common.wrapper index e4bee730..1b784d65 100644 --- a/src/common.wrapper +++ b/src/common.wrapper @@ -1,5 +1,5 @@ (function(window, factory) { - var lazySizes = factory(window, window.document); + var lazySizes = factory(window, window.document, Date); window.lazySizes = lazySizes; if(typeof module == 'object' && module.exports){ module.exports = lazySizes; diff --git a/src/lazysizes-core.js b/src/lazysizes-core.js index 982026c5..2e3a727f 100644 --- a/src/lazysizes-core.js +++ b/src/lazysizes-core.js @@ -1,4 +1,4 @@ -function l(window, document) { +function l(window, document, Date) { // Pass in the windoe Date function also for SSR because the Date class can be lost 'use strict'; /*jshint eqnull:true */ @@ -49,15 +49,17 @@ function l(window, document) { var docElem = document.documentElement; - var Date = window.Date; - var supportPicture = window.HTMLPictureElement; var _addEventListener = 'addEventListener'; var _getAttribute = 'getAttribute'; - var addEventListener = window[_addEventListener]; + /** + * Update to bind to window because 'this' becomes null during SSR + * builds. + */ + var addEventListener = window[_addEventListener].bind(window); var setTimeout = window.setTimeout; diff --git a/src/lazysizes-intersection.js b/src/lazysizes-intersection.js index 1491c12b..df7be0c4 100644 --- a/src/lazysizes-intersection.js +++ b/src/lazysizes-intersection.js @@ -2,9 +2,9 @@ if(typeof module == 'object' && module.exports){ module.exports = lazySizes; } else { - window.lazySizes = factory(window, window.document); + window.lazySizes = factory(window, window.document, Date); } -}(window, function l(window, document) { +}(window, function l(window, document, Date) { 'use strict'; /*jshint eqnull:true */ @@ -14,15 +14,13 @@ var docElem = document.documentElement; - var Date = window.Date; - var supportPicture = window.HTMLPictureElement; var _addEventListener = 'addEventListener'; var _getAttribute = 'getAttribute'; - var addEventListener = window[_addEventListener]; + var addEventListener = window[_addEventListener].bind(window); var setTimeout = window.setTimeout;