# @litert/loader

> Simple browser module loader.

Latest version **3.5.9** (published 2025-06-18) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @litert/loader
pnpm add @litert/loader
yarn add @litert/loader
bun add @litert/loader
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.5.9 |
| Published | 2025-06-18 |
| First published | 2020-03-15 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12.0.0 |
| Dependencies | 0 |
| Unpacked size | 76.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | hanguoshuai |
| Maintainers | fenying, hanguoshuai |
| Keywords | litert, module, loader, require |

## Links

- npm: https://www.npmjs.com/package/@litert/loader
- Repository: https://github.com/litert/loader.js
- Homepage: https://github.com/litert/loader.js#readme
- Issues: https://github.com/litert/loader.js/issues
- npm.io page: https://npm.io/package/@litert/loader

## Recent versions

- 3.5.9 (latest) — 2025-06-18
- 4.0.4 (dev) — 2025-08-19
- 2.1.4-rc5 (rc) — 2021-07-04
- 2.0.2-beta2 (beta) — 2021-06-28
- 4.0.3 — 2025-08-19
- 4.0.2 — 2025-08-18
- 4.0.1 — 2025-08-18
- 4.0.0 — 2025-08-18
- 3.5.8 — 2024-10-09
- 3.5.7 — 2024-06-17
- 3.5.6 — 2024-06-17
- 3.5.5 — 2024-06-17
- 3.5.4 — 2024-06-14
- 3.5.3 — 2024-06-12
- 3.5.2 — 2024-06-11
- … 48 more at https://npm.io/package/@litert/loader/versions

## README

<p align="center"><img src="./doc/icon.svg" width="100" height="100" alt="Loader"></p>
<p align="center">
    <a href="https://github.com/litert/loader.js/blob/master/LICENSE">
        <img alt="License" src="https://img.shields.io/github/license/litert/loader.js?color=blue" />
    </a>
    <a href="https://www.npmjs.com/package/@litert/loader">
        <img alt="NPM stable version" src="https://img.shields.io/npm/v/@litert/loader?color=brightgreen&logo=npm" />
    </a>
    <a href="https://github.com/litert/loader.js/releases">
        <img alt="GitHub releases" src="https://img.shields.io/github/v/release/litert/loader.js?color=brightgreen&logo=github" />
    </a>
    <a href="https://github.com/litert/loader.js/issues">
        <img alt="GitHub issues" src="https://img.shields.io/github/issues/litert/loader.js?color=blue&logo=github" />
    </a>
</p>

Simple browser module loader.

## Languages

[简体中文](doc/README.sc.md) | [繁體中文](doc/README.tc.md)

## Features

- [x] Supports running code in memory.  
- [x] The configuration is simple and lightweight.  
- [x] No intrusion and does not affect the script label.  
- [x] Support the CommonJS / ES6 Module format.  
- [x] The fetch function is automatically supported without additional loading.  
- [x] Support TypeScript.

## Installation

This is a module for browsers and eventually needs to be referenced by the script tag.

### NPM

You can install directly using NPM:

```sh
$ npm i @litert/loader --save
```

Or install the developing (unstable) version for newest features:

```sh
$ npm i @litert/loader@dev --save
```

### CDN (recommend)

Recommended: https://cdn.jsdelivr.net/npm/@litert/loader@3.5.9/dist/loader.min.js, you can also find it here https://cdn.jsdelivr.net/npm/@litert/loader/.

Also available on [unpkg](https://unpkg.com/@litert/loader@3.5.9/dist/loader.min.js).

## Usage

Here's a general how to use it:

```html
<script src="https://cdn.jsdelivr.net/npm/@litert/loader@3.5.9/dist/loader.min.js"></script>
```

The code hint needs to be added in "tsconfig.json":

```json
{
    "compilerOptions": {
        ...
        "typeRoots": [
            "./node_modules/@types",
            "./node_modules/@litert/loader"
        ]
    }
}
```

All actions are written in the "ready" callback.

```typescript
loader.ready(function() {
    let files: Record<string, Blob | string> = { ... };
    let tmodule: any, module2: any;
    [tmodule, module2] = loader.require(['../dist/tmodule', './module2'], files);
});
```

Alternatively, use ?path= to load the ingress file directly, the js file extension can be omitted.

```html
<script src="https://cdn.jsdelivr.net/npm/@litert/loader@3.5.9/dist/index.min.js?path=../lib/test"></script>
```

Use the ?cdn= parameter to set the source address of the third library load, default is: https://cdn.jsdelivr.net.

```html
<script src="https://cdn.jsdelivr.net/npm/@litert/loader@3.5.9/dist/index.min.js?cdn=https://cdn.xxx.xxx"></script>
```

Use the ?map= parameter to set the path to the third-party library, a JSON string, that is valid only with the path parameter.

```html
<script src="https://cdn.jsdelivr.net/npm/@litert/loader@3.5.9/dist/index.min.js?&path=xxx&map={'xxx':'https://xx/npm/index'}"></script>
```

Using the ?npm= parameter loader will automatically go to npm to find the relevant library for sniffing loading, JSON string, module name and version number, only valid with the path parameter.

```html
<script src="https://cdn.jsdelivr.net/npm/@litert/loader@3.5.9/dist/index.min.js?&path=xxx&npm={'xxx':'1.0.0'}"></script>
```

You can use the fetchFiles method to load network files into memory.

```typescript
let files: Record<string, Blob | string> = await loader.fetchFiles([
    '../dist/tmodule.js',
    './module2.js',
    'https://cdn.jsdelivr.net/npm/seedrandom@3.0.5/seedrandom.min.js'
]);
```

Use sniffFiles to load network files into the memory, and sniff the inclusion relationship in the file, such as js import, require, etc., CSS url, etc.

```typescript
let files: Record<string, Blob | string> = {};
await loader.sniffFiles([
    'https://cdn.jsdelivr.net/npm/@juggle/resize-observer@3.2.0/lib/exports/resize-observer.js'
], {
    'files': files
});
```

Using the map option, you can specify the alias of the library, the alias of the import command is also based on this.

```typescript
let cache: Record<string, any> = {};
let files: Record<string, Blob | string> = {};
if (!Object.keys(files).includes('https://cdn.jsdelivr.net/npm/seedrandom@3.0.5/seedrandom.min.js')) {
    await loader.fetchFiles([
        'https://cdn.jsdelivr.net/npm/seedrandom@3.0.5/seedrandom.min.js'
    ], {
        'files': files
    });
}
let sr = loader.require('seedrandom', files, {
    'cache': cache,
    'map': {
        'seedrandom': 'https://cdn.jsdelivr.net/npm/seedrandom@3.0.5/seedrandom.min',
        '~/': './'
    }
})[0];
```

## Test

### Node

After compiling the TS code, execute: node dist/test-on-node to observe the execution results of the test code under node.

### Browser

Use the browser to visit "test/" to view the comparison results are the same as in the node environment.

[You can also click here to access the example online.](https://litert.github.io/loader.js/test/)

## License

This library is published under [Apache-2.0](./LICENSE) license.

---
_Source: https://npm.io/package/@litert/loader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
