Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit 896b273

Browse files
committed
v3.5.1
1 parent 1b9132d commit 896b273

File tree

10 files changed

+226
-74
lines changed

10 files changed

+226
-74
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ $ npm i @litert/loader@dev --save
4444

4545
### CDN (recommend)
4646

47-
Recommended: https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/loader.min.js, you can also find it here https://cdn.jsdelivr.net/npm/@litert/loader/.
47+
Recommended: https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/loader.min.js, you can also find it here https://cdn.jsdelivr.net/npm/@litert/loader/.
4848

49-
Also available on [unpkg](https://unpkg.com/@litert/[email protected].0/dist/loader.min.js).
49+
Also available on [unpkg](https://unpkg.com/@litert/[email protected].1/dist/loader.min.js).
5050

5151
## Usage
5252

5353
Here's a general how to use it:
5454

5555
```html
56-
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/loader.min.js"></script>
56+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/loader.min.js"></script>
5757
```
5858

5959
The code hint needs to be added in "tsconfig.json":
@@ -83,25 +83,25 @@ loader.ready(function() {
8383
Alternatively, use ?path= to load the ingress file directly, the js file extension can be omitted.
8484

8585
```html
86-
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/index.min.js?path=../lib/test"></script>
86+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/index.min.js?path=../lib/test"></script>
8787
```
8888

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

9191
```html
92-
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/index.min.js?cdn=https://cdn.xxx.xxx"></script>
92+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/index.min.js?cdn=https://cdn.xxx.xxx"></script>
9393
```
9494

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

9797
```html
98-
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/index.min.js?&path=xxx&map={'xxx':'https://xx/npm/index'}"></script>
98+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/index.min.js?&path=xxx&map={'xxx':'https://xx/npm/index'}"></script>
9999
```
100100

101101
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.
102102

103103
```html
104-
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/index.min.js?&path=xxx&npm={'xxx':'1.0.0'}"></script>
104+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/index.min.js?&path=xxx&npm={'xxx':'1.0.0'}"></script>
105105
```
106106

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

dist/index.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ interface ILoader {
6666
'afterIgnore'?: RegExp;
6767
'adapter'?: (url: string) => string | Blob | null | Promise<string | Blob | null>;
6868
}): Promise<Record<string, Blob | string>>;
69+
/**
70+
* --- 嗅探 NPM 包为文件序列 ---
71+
* @param npms 包名/版本键值对
72+
* @param opt 配置项
73+
*/
74+
sniffNpm(npms: Record<string, string>, opt?: {
75+
'init'?: RequestInit;
76+
'load'?: (url: string) => void;
77+
'loaded'?: (url: string, state: number) => void;
78+
'dir'?: string;
79+
'files'?: Record<string, Blob | string>;
80+
'map'?: Record<string, string>;
81+
'before'?: string;
82+
'after'?: string;
83+
'afterIgnore'?: RegExp;
84+
'adapter'?: (url: string) => string | Blob | null | Promise<string | Blob | null>;
85+
}): Promise<Record<string, Blob | string>>;
6986
/**
7087
* --- 嗅探文件序列 ---
7188
* @param urls 网址或网址列表

dist/loader.js

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
6363
if (match) {
6464
try {
6565
match[1] = match[1].replace(/'/g, '"');
66-
const ls = JSON.parse(match[1]);
67-
const npms = [];
68-
for (const name in ls) {
69-
npms.push(`${this.cdn}/npm/${name}@${ls[name]}/package.json`);
70-
}
71-
const npmFiles = yield this.fetchFiles(npms, {
72-
'files': files
73-
});
74-
const sniffFiles = [];
75-
for (const name in ls) {
76-
const file = npmFiles[`${this.cdn}/npm/${name}@${ls[name]}/package.json`];
77-
if (typeof file !== 'string') {
78-
continue;
79-
}
80-
try {
81-
const json = JSON.parse(file);
82-
const main = json.jsdelivr ? `${this.cdn}/npm/${name}@${ls[name]}/${json.jsdelivr}` : `${this.cdn}/npm/${name}@${ls[name]}/${json.main}`;
83-
sniffFiles.push(main);
84-
map[name] = main;
85-
}
86-
catch (e) {
87-
console.log(e);
88-
}
89-
}
90-
yield this.sniffFiles(sniffFiles, {
66+
const npms = JSON.parse(match[1]);
67+
yield this.sniffNpm(npms, {
9168
'files': files,
9269
'map': map
9370
});
@@ -572,6 +549,59 @@ return module.exports;`;
572549
});
573550
});
574551
},
552+
sniffNpm: function (npms, opt = {}) {
553+
return __awaiter(this, void 0, void 0, function* () {
554+
if (!opt.map) {
555+
opt.map = {};
556+
}
557+
if (!opt.files) {
558+
opt.files = {};
559+
}
560+
const packages = [];
561+
for (const name in npms) {
562+
packages.push(`${this.cdn}/npm/${name}@${npms[name]}/package.json`);
563+
}
564+
const npmFiles = yield this.fetchFiles(packages, {
565+
'init': opt.init,
566+
'load': opt.load,
567+
'loaded': opt.loaded,
568+
'dir': opt.dir,
569+
'files': opt.files,
570+
'before': opt.before,
571+
'after': opt.after,
572+
'afterIgnore': opt.afterIgnore,
573+
'adapter': opt.adapter
574+
});
575+
const sniffFiles = [];
576+
for (const name in npms) {
577+
const file = npmFiles[`${this.cdn}/npm/${name}@${npms[name]}/package.json`];
578+
if (typeof file !== 'string') {
579+
continue;
580+
}
581+
try {
582+
const json = JSON.parse(file);
583+
const main = json.jsdelivr ? `${this.cdn}/npm/${name}@${npms[name]}/${json.jsdelivr}` : `${this.cdn}/npm/${name}@${npms[name]}/${json.main}`;
584+
sniffFiles.push(main);
585+
opt.map[name] = main;
586+
}
587+
catch (e) {
588+
console.log(e);
589+
}
590+
}
591+
yield this.sniffFiles(sniffFiles, {
592+
'init': opt.init,
593+
'load': opt.load,
594+
'loaded': opt.loaded,
595+
'dir': opt.dir,
596+
'files': opt.files,
597+
'before': opt.before,
598+
'after': opt.after,
599+
'afterIgnore': opt.afterIgnore,
600+
'adapter': opt.adapter
601+
});
602+
return opt.files;
603+
});
604+
},
575605
sniffFiles: function (urls, opt = {}) {
576606
return __awaiter(this, void 0, void 0, function* () {
577607
if (typeof urls === 'string') {

dist/loader.ts

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,8 @@
7272
if (match) {
7373
try {
7474
match[1] = match[1].replace(/'/g, '"');
75-
const ls = JSON.parse(match[1]);
76-
const npms: string[] = [];
77-
for (const name in ls) {
78-
npms.push(`${this.cdn}/npm/${name}@${ls[name]}/package.json`);
79-
}
80-
const npmFiles = await this.fetchFiles(npms, {
81-
'files': files
82-
});
83-
/** --- 将要嗅探的文件 --- */
84-
const sniffFiles: string[] = [];
85-
for (const name in ls) {
86-
const file = npmFiles[`${this.cdn}/npm/${name}@${ls[name]}/package.json`];
87-
if (typeof file !== 'string') {
88-
continue;
89-
}
90-
try {
91-
const json = JSON.parse(file);
92-
const main = json.jsdelivr ? `${this.cdn}/npm/${name}@${ls[name]}/${json.jsdelivr}` : `${this.cdn}/npm/${name}@${ls[name]}/${json.main}`;
93-
sniffFiles.push(main);
94-
map[name] = main;
95-
}
96-
catch (e) {
97-
console.log(e);
98-
}
99-
}
100-
await this.sniffFiles(sniffFiles, {
75+
const npms = JSON.parse(match[1]);
76+
await this.sniffNpm(npms, {
10177
'files': files,
10278
'map': map
10379
});
@@ -692,6 +668,70 @@ return module.exports;`;
692668
});
693669
},
694670

671+
sniffNpm: async function(npms: Record<string, string>, opt: {
672+
'init'?: RequestInit;
673+
'load'?: (url: string) => void;
674+
'loaded'?: (url: string, state: number) => void;
675+
'dir'?: string;
676+
'files'?: Record<string, Blob | string>;
677+
'map'?: Record<string, string>;
678+
'before'?: string;
679+
'after'?: string;
680+
'afterIgnore'?: RegExp;
681+
'adapter'?: (url: string) => string | Blob | null | Promise<string | Blob | null>;
682+
} = {}): Promise<Record<string, Blob | string>> {
683+
if (!opt.map) {
684+
opt.map = {};
685+
}
686+
if (!opt.files) {
687+
opt.files = {};
688+
}
689+
const packages: string[] = [];
690+
for (const name in npms) {
691+
packages.push(`${this.cdn}/npm/${name}@${npms[name]}/package.json`);
692+
}
693+
const npmFiles = await this.fetchFiles(packages, {
694+
'init': opt.init,
695+
'load': opt.load,
696+
'loaded': opt.loaded,
697+
'dir': opt.dir,
698+
'files': opt.files,
699+
'before': opt.before,
700+
'after': opt.after,
701+
'afterIgnore': opt.afterIgnore,
702+
'adapter': opt.adapter
703+
});
704+
/** --- 将要嗅探的文件 --- */
705+
const sniffFiles: string[] = [];
706+
for (const name in npms) {
707+
const file = npmFiles[`${this.cdn}/npm/${name}@${npms[name]}/package.json`];
708+
if (typeof file !== 'string') {
709+
continue;
710+
}
711+
try {
712+
const json = JSON.parse(file);
713+
const main = json.jsdelivr ? `${this.cdn}/npm/${name}@${npms[name]}/${json.jsdelivr}` : `${this.cdn}/npm/${name}@${npms[name]}/${json.main}`;
714+
sniffFiles.push(main);
715+
opt.map[name] = main;
716+
}
717+
catch (e) {
718+
console.log(e);
719+
}
720+
}
721+
await this.sniffFiles(sniffFiles, {
722+
'init': opt.init,
723+
'load': opt.load,
724+
'loaded': opt.loaded,
725+
'dir': opt.dir,
726+
'files': opt.files,
727+
'before': opt.before,
728+
'after': opt.after,
729+
'afterIgnore': opt.afterIgnore,
730+
'adapter': opt.adapter
731+
});
732+
return opt.files;
733+
},
734+
695735
sniffFiles: async function(urls: string | string[], opt: {
696736
'init'?: RequestInit;
697737
'load'?: (url: string) => void;

dist/test-on-browser.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
99
});
1010
};
1111
loader.ready(function () {
12-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1;
12+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2;
1313
return __awaiter(this, void 0, void 0, function* () {
1414
const keyInput = document.getElementById('key');
1515
const consoleDiv = document.getElementById('console');
@@ -626,5 +626,32 @@ loader.ready(function () {
626626
});
627627
})();
628628
});
629+
(_2 = document.getElementById('testSniffNpm')) === null || _2 === void 0 ? void 0 : _2.addEventListener('click', function () {
630+
(function () {
631+
return __awaiter(this, void 0, void 0, function* () {
632+
const tmpFiles = {};
633+
const map = {};
634+
mask.style.display = 'flex';
635+
yield loader.sniffNpm({
636+
'clickgo': '3.3.5',
637+
'compressorjs': '1.2.1'
638+
}, {
639+
'files': tmpFiles,
640+
'after': '?' + Math.random().toString(),
641+
'dir': '/',
642+
'map': map,
643+
load: function (url) {
644+
mask.innerHTML = url + '<br>Loading...';
645+
},
646+
loaded: function (url) {
647+
mask.innerHTML = url + '<br>Loaded.';
648+
}
649+
});
650+
mask.style.display = 'none';
651+
console.log('tmpFiles', Object.keys(tmpFiles));
652+
console.log('map', map);
653+
});
654+
})();
655+
});
629656
});
630657
});

dist/test-on-browser.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,4 +619,32 @@ loader.ready(async function(): Promise<void> {
619619
console.log('Post done: ' + typeof r);
620620
})() as unknown;
621621
});
622+
623+
// --- testSniffNpm ---
624+
625+
document.getElementById('testSniffNpm')?.addEventListener('click', function() {
626+
(async function() {
627+
const tmpFiles: Record<string, string | Blob> = {};
628+
const map = {};
629+
mask.style.display = 'flex';
630+
await loader.sniffNpm({
631+
'clickgo': '3.3.5',
632+
'compressorjs': '1.2.1'
633+
}, {
634+
'files': tmpFiles,
635+
'after': '?' + Math.random().toString(),
636+
'dir': '/',
637+
'map': map,
638+
load: function(url: string) {
639+
mask.innerHTML = url + '<br>Loading...';
640+
},
641+
loaded: function(url: string) {
642+
mask.innerHTML = url + '<br>Loaded.';
643+
}
644+
});
645+
mask.style.display = 'none';
646+
console.log('tmpFiles', Object.keys(tmpFiles));
647+
console.log('map', map);
648+
})() as unknown;
649+
});
622650
});

doc/README.sc.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ $ npm i @litert/loader@dev --save
4343

4444
### CDN(推荐)
4545

46-
推荐引用地址:https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/index.min.js,也可以此处查找:https://cdn.jsdelivr.net/npm/@litert/loader/。
46+
推荐引用地址:https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/index.min.js,也可以此处查找:https://cdn.jsdelivr.net/npm/@litert/loader/。
4747

48-
同样可使用 [unpkg](https://unpkg.com/@litert/[email protected].0/dist/index.min.js)
48+
同样可使用 [unpkg](https://unpkg.com/@litert/[email protected].1/dist/index.min.js)
4949

5050
## Usage
5151

5252
通常的使用方式:
5353

5454
```html
55-
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/index.min.js"></script>
55+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/index.min.js"></script>
5656
```
5757

5858
代码提示需要在“tsconfig.json”中添加:
@@ -82,25 +82,25 @@ loader.ready(function() {
8282
或者使用 ?path= 直接加载入口 js 文件,js 后缀可省略。
8383

8484
```html
85-
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/index.min.js?path=../lib/test"></script>
85+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/index.min.js?path=../lib/test"></script>
8686
```
8787

8888
使用 ?cdn= 参数设置第三库加载的源地址,默认为:https://cdn.jsdelivr.net。
8989

9090
```html
91-
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/index.min.js?cdn=https://cdn.xxx.xxx"></script>
91+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/index.min.js?cdn=https://cdn.xxx.xxx"></script>
9292
```
9393

9494
使用 ?map= 参数设置第三方库的路径,JSON 字符串,仅在含有 path 参数下有效。
9595

9696
```html
97-
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/index.min.js?&path=xxx&map={'xxx':'https://xx/npm/index'}"></script>
97+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/index.min.js?&path=xxx&map={'xxx':'https://xx/npm/index'}"></script>
9898
```
9999

100100
使用 ?npm= 参数 loader 将自动去 npm 查找相关的库进行嗅探加载,JSON 字符串,模块名跟版本号,仅在含有 path 参数下有效。
101101

102102
```html
103-
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].0/dist/index.min.js?&path=xxx&npm={'xxx':'1.0.0'}"></script>
103+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].1/dist/index.min.js?&path=xxx&npm={'xxx':'1.0.0'}"></script>
104104
```
105105

106106
你可以使用 fetchFiles 方法加载网络文件到内存。

0 commit comments

Comments
 (0)