🐼 Tiny (~300B) library to dynamically load js files only when they're needed.
MIT License
🐼 Tiny (~300B) package to dynamically import Javascript files only when needed
Functionalities:
Node:
npm i -S @pandasekh/dynamic-script-loader
Yarn:
yarn add @pandasekh/dynamic-script-loader
import loader from 'https://cdn.skypack.dev/@pandasekh/dynamic-script-loader';
<script src="https://unpkg.com/@pandasekh/dynamic-script-loader@latest">
// Import using JS Modules
import load from "@pandasekh/dynamic-script-loader"
// or using CommonJS
const load = require("@pandasekh/dynamic-script-loader")
// Using a CDN (Node not required)
import load from 'https://cdn.skypack.dev/@pandasekh/dynamic-script-loader';
// Then import a library only when needed
load("https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js")
// Promise based API
load("https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js").then(() => console.log("Loaded!"))
// Returns the generated DOM element
load("https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js").then(element => console.log(element.src))
Test this library on Codepen.
load(src: string) => Promise<HTMLScriptElement>
Default function, it will load a script asynchronously and return the HTMLScriptElement just created.
MIT License (c) 2021 Alessio Franceschi