From 0df8ae636aafdb2b1403744e5dad0d35b30a6c76 Mon Sep 17 00:00:00 2001 From: Shin <2082119+shinsenter@users.noreply.github.com> Date: Wed, 7 Sep 2022 15:51:39 +0900 Subject: [PATCH] For v3.4.0 (#119) --- .build/.eslintrc | 2 +- .build/.minify | 12 +- README.md | 141 ++++++++++---------- demo/index.html | 107 ++++++++------- dist/defer.min.js | 4 +- dist/defer_plus.min.js | 4 +- dist/polyfill.min.js | 2 +- docs/about.md | 14 +- docs/apis.md | 127 +++++++++--------- docs/index.md | 141 ++++++++++---------- package-lock.json | 4 +- package.json | 6 +- src/defer.js | 294 ++++++++++++++++++++++++----------------- src/docs.js | 100 +++++++------- src/fallback.js | 15 ++- 15 files changed, 511 insertions(+), 462 deletions(-) diff --git a/.build/.eslintrc b/.build/.eslintrc index e8d6a97..5a472e2 100644 --- a/.build/.eslintrc +++ b/.build/.eslintrc @@ -55,7 +55,7 @@ "guard-for-in": "error", "handle-callback-err": "error", "id-blacklist": "error", - "id-length": "error", + "id-length": "off", "id-match": "error", "implicit-arrow-linebreak": "error", "indent": ["error", 2], diff --git a/.build/.minify b/.build/.minify index 77ebd9d..d753681 100644 --- a/.build/.minify +++ b/.build/.minify @@ -1,13 +1,15 @@ { "parse": {}, "mangle": { - "reserved": ["log", "debug"] + "reserved": ["log", "debug", "perf_begin", "perf_end"] }, "compress": { "drop_console": true, - "passes": 5, + "hoist_funs": true, + "passes": 10, "pure_getters": true, - "pure_funcs": ["log", "debug"] + "pure_funcs": ["log", "debug", "perf_begin", "perf_end"], + "reduce_funcs": true }, "output": { "comments": "/^!/", @@ -21,7 +23,7 @@ "nameCache": null, "sourceMap": false, "toplevel": false, - "v8": false, + "v8": true, "warnings": false, - "webkit": false + "webkit": true } \ No newline at end of file diff --git a/README.md b/README.md index c6a455b..464e2d6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Package @shinsenter/defer.js -🥇 A JavaScript micro-library that helps you lazy load (almost) anything. It's zero-dependency, super-efficient, and Web Vitals friendly. +🥇 A JavaScript micro-library that helps you lazy load (almost) anything. Defer.js is zero-dependency, super-efficient, and Web Vitals friendly. [![NPM](https://img.shields.io/npm/l/@shinsenter/defer.js)](https://code.shin.company/defer.js/blob/master/LICENSE) [![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@shinsenter/defer.js)](https://snyk.io/advisor/npm-package/@shinsenter/defer.js) @@ -16,7 +16,7 @@ [![NPM](https://nodei.co/npm/@shinsenter/defer.js.png?downloads=true)](https://www.npmjs.com/package/@shinsenter/defer.js) - **Package**: [@shinsenter/defer.js](https://www.npmjs.com/package/@shinsenter/defer.js) -- **Version**: 3.3.0 +- **Version**: 3.4.0 - **Author**: Mai Nhut Tan - **Copyright**: 2022 AppSeeds - **License**: [MIT](https://code.shin.company/defer.js/blob/master/LICENSE) @@ -80,7 +80,7 @@ Just put a ` + @@ -96,7 +96,7 @@ Because `defer.min.js` is optimized to a very tiny file size, you can even inlin My Awesome Page - + @@ -114,7 +114,7 @@ just use `defer_plus.min.js` instead of `defer.min.js`. My Awesome Page - + @@ -131,20 +131,20 @@ right after the `defer.min.js` script tag as the following example: - - + + ``` *HINT*: Modern browsers support `IntersectionObserver` feature, so you don't have to be concerned about it if you don't care about IE users. ## Functions -* [Defer(func, [delay], [waitForInteraction])](#Defer) ⇒ void +* [Defer(func, [delay], [waitForUserAction])](#Defer) ⇒ void * [.lazy](#Defer.lazy) : boolean - * [.all([selector], [delay], [waitForInteraction])](#Defer.all) ⇒ void + * [.all([selector], [delay], [waitForUserAction])](#Defer.all) ⇒ void * [.dom([selector], [delay], [unveiledClass], [resolver], [observeOptions])](#Defer.dom) ⇒ void - * [.css(fileUrl, [id], [delay], [onload], [waitForInteraction])](#Defer.css) ⇒ void - * [.js(fileUrl, [id], [delay], [onload], [waitForInteraction])](#Defer.js) ⇒ void + * [.css(fileUrl, [id], [delay], [onload], [waitForUserAction])](#Defer.css) ⇒ void + * [.js(fileUrl, [id], [delay], [onload], [waitForUserAction])](#Defer.js) ⇒ void * [.reveal(node, [unveiledClass])](#Defer.reveal) ⇒ void * ~~[defer(func, [delay])](#defer)~~ * ~~[deferimg([selector], [delay], [unveiledClass], [resolver], [observeOptions])](#deferimg)~~ @@ -160,7 +160,7 @@ so you don't have to be concerned about it if you don't care about IE users. -## Defer(func, [delay], [waitForInteraction]) ⇒ void +## Defer(func, [delay], [waitForUserAction]) ⇒ void Heavy DOM manipulations may cause render-blocking issues in real scenarios. Wrapping your script with `Defer()` may help your website prevent render-blocking issues. @@ -171,7 +171,7 @@ Wrapping your script with `Defer()` may help your website prevent render-blockin | --- | --- | --- | --- | | func | function | | A function to be executed after page fully loaded. | | [delay] | number | 0 | The time, in milliseconds that it should wait before the function is executed. | -| [waitForInteraction] | boolean | false | This argument tells `Defer()` to delay the execution and wait until there is a user interaction. | +| [waitForUserAction] | boolean | false | This argument tells `Defer()` to delay the execution and wait until there is a user interaction. | **Example** jQuery is used in this example to perform some DOM manipulations. @@ -220,12 +220,12 @@ and wait until the user starts interacting with your page. ``` -* [Defer(func, [delay], [waitForInteraction])](#Defer) ⇒ void +* [Defer(func, [delay], [waitForUserAction])](#Defer) ⇒ void * [.lazy](#Defer.lazy) : boolean - * [.all([selector], [delay], [waitForInteraction])](#Defer.all) ⇒ void + * [.all([selector], [delay], [waitForUserAction])](#Defer.all) ⇒ void * [.dom([selector], [delay], [unveiledClass], [resolver], [observeOptions])](#Defer.dom) ⇒ void - * [.css(fileUrl, [id], [delay], [onload], [waitForInteraction])](#Defer.css) ⇒ void - * [.js(fileUrl, [id], [delay], [onload], [waitForInteraction])](#Defer.js) ⇒ void + * [.css(fileUrl, [id], [delay], [onload], [waitForUserAction])](#Defer.css) ⇒ void + * [.js(fileUrl, [id], [delay], [onload], [waitForUserAction])](#Defer.js) ⇒ void * [.reveal(node, [unveiledClass])](#Defer.reveal) ⇒ void @@ -240,7 +240,7 @@ Setting `Defer.lazy=true` tells the library to delay the execution of deferred scripts until the user starts interacting with the page regardless of the page load event. -It will override the default behavior of `waitForInteraction` +It will override the default behavior of `waitForUserAction` argument of the `Defer()` method. Changing this variable will also affect the behavior of these functions: @@ -264,7 +264,7 @@ To override the default behavior of the `Defer()` method. -### Defer.all([selector], [delay], [waitForInteraction]) ⇒ void +### Defer.all([selector], [delay], [waitForUserAction]) ⇒ void Slow scripts (third-party libraries, add-ons, widgets, etc.) may cause [Web Vitals](https://web.dev/vitals/) issues in real scenarios. @@ -275,10 +275,10 @@ This trick also works perfectly with ` ``` **Example** Using your value for the type attribute, such as `type="my-magic"`: If you hate using the `type="deferjs"` attribute, -you can even choose yours by using the `Defer.all()` function. +you can even choose yours by using the `Defer.all()` method. Notice: To avoid unexpected behavior when using -the `Defer.all()` function to delay the execution of script tags, -you should call run `Defer.all()` with a regular script tag. +the `Defer.all()` method to delay the execution of script tags, +you should call run the `Defer.all()` method with a regular script tag. ```html ``` **Example** -Using `Defer.all()` with script tags with `src` attribute: +Using the `Defer.all()` method for script tags with `src` attribute: Your scripts will work perfectly when you mix inline scripts and script tags with an src attribute, like the below example. -The `waitForInteraction` argument (the fifth argument) is set to `true`, +The `waitForUserAction` argument (the fifth argument) is set to `true`, the library will defer the load of the tippy.js library until the user starts interacting, when the user moves his/her mouse on the button, a tooltip will show. Notice: To avoid unexpected behavior when using -the `Defer.all()` function to delay the execution of script tags, -you should call run `Defer.all()` with a regular script tag. +the `Defer.all()` method to delay the execution of script tags, +you should call run the `Defer.all()` method with a regular script tag. ```html @@ -379,7 +379,7 @@ The `Defer.dom()` method is useful in the below use cases: - Scroll-reveal features, such as handling AJAX updating when a block is entering the viewport. - An element that was deferred by Defer.dom() will be unveiled as soon as the page finished loading. -An element that was deferred by `Defer.dom()` will be unveiled +An element that was deferred by the `Defer.dom()` method will be unveiled when it going to enters the browser viewport. The `Defer.dom()` method also converts `data-*` attributes of the elements @@ -418,7 +418,6 @@ a very small placeholder image before the real image gets downloaded. ```html
A lazy image A lazy image with a low-resolution placeholder` tag animate. ```html @@ -577,7 +576,7 @@ but we can also use CSS class names instead of inline `style` attributes. **Example** Lazy load a video. -With `Defer.dom()`, we can easily defer the load of various media tags, such as a `
@@ -496,7 +495,7 @@

Lazy load an iframe:

Lazy load a Youtube video:

- This example uses Defer.dom to defer a load of a Youtube iframe. + This example uses the Defer.dom() method to defer a load of a Youtube iframe.
@@ -509,7 +508,7 @@

Lazy load a Youtube video:

@@ -517,7 +516,7 @@

Lazy load a Youtube video:

Lazy load a Facebook post:

- This example uses Defer.dom to defer a load of a Facebook post. + This example uses the Defer.dom() method to defer a load of a Facebook post.
@@ -529,7 +528,7 @@

Lazy load a Facebook post:

@@ -537,7 +536,7 @@

Lazy load a Facebook post:

Lazy load a Discord chat box:

- This example uses Defer.dom to defer a load of a Discord chat box. + This example uses the Defer.dom() method to defer a load of a Discord chat box.