TinyLoader is a TypeScript library that simplifies the dynamic embedding of JavaScript and CSS files into HTML documents. With its simple and intuitive API, developers can effortlessly embed single or multiple files into the <head>
section of the HTML document.
The embedded files are included in the <head>
section of the HTML document, like this:
<head>
...
<script src="/js/demo1.js"></script>
<script src="/js/demo2.js"></script>
<link rel="stylesheet" href="/css/demo2.css">
<link rel="stylesheet" href="/css/demo3.css">
<script src="/js/demo3.js"></script>
...
</head>
To embed a single file, use the TinyLoader.embedFile()
method:
TinyLoader.embedFile("/js/demo3.js")
.then((result) => {
// Code to execute on successful embedding
})
.catch((error) => {
// Code to handle errors
});
To embed multiple files, use the TinyLoader.embedFileList()
method and pass a JSON string containing an array of file objects:
const jsonString = `[
{
"jspath": "/js/demo1.js"
},
{
"jspath": "/js/demo2.js",
"csspath": "/css/demo2.css"
},
{
"csspath": "/css/demo3.css"
}
]`;
TinyLoader.embedFileList(jsonString)
.then((results) => {
// Code to execute on successful embedding
})
.catch((error) => {
// Code to handle errors
});
Each file object in the JSON array should have a "jspath"
property specifying the path to the JavaScript file, and/or a "csspath"
property specifying the path to the corresponding CSS file.
To load the content of a file as a string, use the TinyLoader.loadFile()
method:
TinyLoader.loadFile("/path/to/file.txt")
.then((fileContent) => {
// Code to handle the loaded file content
})
.catch((error) => {
// Code to handle errors
});
Embeds a list of JavaScript and CSS files specified in a JSON string.
jsonInput
: A JSON string or object containing an array of objects withjspath
andcsspath
properties.- Returns a Promise that resolves to
true
if all files were embedded successfully, orfalse
otherwise.
Embeds a JavaScript or CSS file from the specified path.
path
: The path to the file to be embedded.- Returns a Promise that resolves to
true
if the file was embedded successfully, orfalse
otherwise.
Loads a file from the specified path and returns its content as a string.
path
: The path of the file to be loaded.- Returns a Promise that resolves to the content of the file as a string if the file was loaded successfully, or rejects with an error otherwise.
This project is licensed under the MIT License.