项目作者: codewithkyle

项目描述 :
A lightweight Web Component based lazy loading library.
高级语言: TypeScript
项目地址: git://github.com/codewithkyle/lazy-loader.git
创建时间: 2021-03-28T14:23:53Z
项目社区:https://github.com/codewithkyle/lazy-loader

开源协议:MIT License

下载


Lazy Loader

A lightweight (~2kb) Web Component based lazy loading library.

Install

Install via NPM:

  1. npm i -S @codewithkyle/lazy-loader

Or via CDN:

  1. import { configure, update, mount, css } from "https://cdn.jsdelivr.net/npm/@codewithkyle/lazy-loader@1/lazy-loader.min.mjs";
  1. <script src="https://cdn.jsdelivr.net/npm/@codewithkyle/lazy-loader@1/lazy-loader.min.js">

Usage

ES Module

  1. import { configure, update, mount, css } from "https://cdn.jsdelivr.net/npm/@codewithkyle/lazy-loader@1/lazy-loader.min.mjs";
  2. // Start the lazy loading process by configuring the default locations for your JS and CSS files
  3. configure({
  4. jsDir: "/js",
  5. cssDir: "/css",
  6. default: "lazy", // optional
  7. lazierCSS: false, // optional
  8. });
  9. // Alternatively if the default settings (seen above) are okay you could simply call the update function instead
  10. // You can call the update function at any time
  11. update();
  12. // Manually mount new components
  13. import { MyCustomElement } from "./my-custom-element.js";
  14. mount("my-custom-element", MyCustomElement); // returns a promise
  15. // Alternatively if the components file name matches the tag name the library can dynamically import the script from the JS directory (see configure function)
  16. mount("my-custom-element");
  17. // Manually lazy load CSS files
  18. css("exmaple.css"); // returns a promise
  19. // Alternatively you can load multiple files at once
  20. css(["example-one", "examle-two.css", "https://cdn.example.com/example-two.css", "../../relative-path-example.css"]);

Common JS

  1. LazyLoader.configure({
  2. jsDir: "/",
  3. cssDir: "/",
  4. default: "lazy", // optional
  5. lazierCSS: false, // optional
  6. });
  7. LazyLoader.update();
  8. LazyLoader.mount("my-custom-element")

Interfaces

  1. type Loading = "eager" | "lazy";
  2. interface LazyLoaderSettings {
  3. cssDir?: string;
  4. jsDir?: string;
  5. default?: Loading;
  6. lazierCSS: boolean;
  7. };
  8. declare const configure: (settings?:Partial<LazyLoaderSettings>) => void;
  9. declare const update: () => void;
  10. declare const mount: (tagName:string, constructor?: CustomElementConstructor) => Promise<void>;
  11. declare const css: (files:string|string[]) => Promise<void>;

HTML Attributes

  1. <!-- Lazy load Web Components by tagging custom elements with the web-component attribute. -->
  2. <!-- In this example the custom-element.js file will be imported from the configured jsDir directory. -->
  3. <custom-element web-component></custom-element>
  4. <!-- You can override the default import behavior by providing a custom file name, relative path, or a URL. -->
  5. <custom-element web-component="custom-file-name.js"></custom-element>
  6. <!-- By default components are loaded and mounted when they enter the viewport. -->
  7. <!-- You can bypass the lazy loader by using the loading attribute. -->
  8. <custom-element web-component loading="eager"></custom-element>
  9. <!-- You can lazy load CSS by attaching the css attribute to any element within the documents body. -->
  10. <!-- You can load multiple files using a whitespace separator. Note that the .css file extenstion is optional. -->
  11. <div class="my-awesome-class" css="awesome-transitions awesome.css"></div>