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

Commit 39fb10e

Browse files
committed
v3.4.4
1 parent 2ef7e91 commit 39fb10e

File tree

13 files changed

+160
-79
lines changed

13 files changed

+160
-79
lines changed

README.md

Lines changed: 9 additions & 8 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].3/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].4/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].3/dist/loader.min.js).
49+
Also available on [unpkg](https://unpkg.com/@litert/[email protected].4/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].3/dist/loader.min.js"></script>
56+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].4/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].3/dist/index.min.js?path=../lib/test"></script>
86+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].4/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].3/dist/index.min.js?cdn=https://cdn.xxx.xxx"></script>
92+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].4/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].3/dist/index.min.js?&path=xxx&map={'xxx':'https://xx/npm/index'}"></script>
98+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].4/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].3/dist/index.min.js?&path=xxx&npm={'xxx':'1.0.0'}"></script>
104+
<script src="https://cdn.jsdelivr.net/npm/@litert/[email protected].4/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.
@@ -140,7 +140,8 @@ if (!Object.keys(files).includes('https://cdn.jsdelivr.net/npm/[email protected]/
140140
let sr = loader.require('seedrandom', files, {
141141
'cache': cache,
142142
'map': {
143-
'seedrandom': 'https://cdn.jsdelivr.net/npm/[email protected]/seedrandom.min'
143+
'seedrandom': 'https://cdn.jsdelivr.net/npm/[email protected]/seedrandom.min',
144+
'~/': './'
144145
}
145146
})[0];
146147
```

dist/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ interface ILoader {
121121
* @param to 相对路径
122122
*/
123123
urlResolve(from: string, to: string): string;
124+
/**
125+
* --- 将路径中的 ../ ./ 都按规范妥善处理 ---
126+
* @param url 要处理的地址
127+
*/
128+
urlAtom(url: string): string;
124129
/**
125130
* --- 判断一个代码字符串中,某个字符是否是被转义的 ---
126131
* @param index 字符的位置

dist/loader.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
1111
(function () {
1212
const temp = document.querySelectorAll('script');
1313
const scriptEle = temp[temp.length - 1];
14+
let location = window.location.href;
15+
if (!location.endsWith('/')) {
16+
const lio = location.lastIndexOf('/');
17+
location = location.slice(0, lio + 1);
18+
}
1419
const loader = {
1520
'isReady': false,
1621
'readys': [],
@@ -139,7 +144,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
139144
opt.cache = {};
140145
}
141146
if (opt.dir === undefined) {
142-
opt.dir = location.href;
147+
opt.dir = location;
143148
}
144149
if (opt.invoke === undefined) {
145150
opt.invoke = {};
@@ -428,7 +433,7 @@ return module.exports;`;
428433
opt.init = {};
429434
}
430435
if (opt.dir === undefined) {
431-
opt.dir = location.href;
436+
opt.dir = location;
432437
}
433438
if (opt.before === undefined) {
434439
opt.before = '';
@@ -663,7 +668,7 @@ return module.exports;`;
663668
import: function (url, files, opt = {}) {
664669
return __awaiter(this, void 0, void 0, function* () {
665670
if (opt.dir === undefined) {
666-
opt.dir = location.href;
671+
opt.dir = location;
667672
}
668673
url = this.moduleNameResolve(url, opt.dir, opt.map);
669674
if (files[url]) {
@@ -679,8 +684,15 @@ return module.exports;`;
679684
});
680685
},
681686
moduleNameResolve: function (path, dir, map = {}) {
682-
if (map[path]) {
683-
path = map[path];
687+
for (const key in map) {
688+
if (!path.startsWith(key)) {
689+
continue;
690+
}
691+
let val = map[key];
692+
if (val.startsWith('.')) {
693+
val = location + val;
694+
}
695+
path = val + path.slice(key.length);
684696
}
685697
path = this.urlResolve(dir, path);
686698
if (path.endsWith('/')) {
@@ -767,26 +779,26 @@ return module.exports;`;
767779
from = from.replace(/\\/g, '/');
768780
to = to.replace(/\\/g, '/');
769781
if (to === '') {
770-
return from;
782+
return this.urlAtom(from);
771783
}
772784
const f = this.parseUrl(from);
773785
if (to.startsWith('//')) {
774-
return f.protocol ? f.protocol + to : to;
786+
return this.urlAtom(f.protocol ? f.protocol + to : to);
775787
}
776788
if (f.protocol) {
777789
from = f.protocol + from.slice(f.protocol.length);
778790
}
779791
const t = this.parseUrl(to);
780792
if (t.protocol) {
781-
return t.protocol + to.slice(t.protocol.length);
793+
return this.urlAtom(t.protocol + to.slice(t.protocol.length));
782794
}
783795
if (to.startsWith('#') || to.startsWith('?')) {
784796
const sp = from.indexOf(to[0]);
785797
if (sp !== -1) {
786-
return from.slice(0, sp) + to;
798+
return this.urlAtom(from.slice(0, sp) + to);
787799
}
788800
else {
789-
return from + to;
801+
return this.urlAtom(from + to);
790802
}
791803
}
792804
let abs = (f.auth ? f.auth + '@' : '') + (f.host ? f.host : '');
@@ -797,17 +809,22 @@ return module.exports;`;
797809
const path = f.pathname.replace(/\/[^/]*$/g, '');
798810
abs += path + '/' + to;
799811
}
800-
abs = abs.replace(/\/\.\//g, '/');
801-
while (/\/(?!\.\.)[^/]+\/\.\.\//.test(abs)) {
802-
abs = abs.replace(/\/(?!\.\.)[^/]+\/\.\.\//g, '/');
803-
}
804-
abs = abs.replace(/\.\.\//g, '');
805812
if (f.protocol && (f.protocol !== 'file:') && !f.host) {
806-
return f.protocol + abs;
813+
return this.urlAtom(f.protocol + abs);
807814
}
808815
else {
809-
return (f.protocol ? f.protocol + '//' : '') + abs;
816+
return this.urlAtom((f.protocol ? f.protocol + '//' : '') + abs);
817+
}
818+
},
819+
urlAtom: function (url) {
820+
while (url.includes('/./')) {
821+
url = url.replace(/\/\.\//g, '/');
822+
}
823+
while (/\/(?!\.\.)[^/]+\/\.\.\//.test(url)) {
824+
url = url.replace(/\/(?!\.\.)[^/]+\/\.\.\//g, '/');
810825
}
826+
url = url.replace(/\.\.\//g, '');
827+
return url;
811828
},
812829
isEscapeChar: function (index, code) {
813830
let preChar = code[index - 1];

dist/loader.ts

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
/** --- 获取当前 js 基路径 --- */
1313
const temp = document.querySelectorAll('script');
1414
const scriptEle = temp[temp.length - 1];
15+
/** --- 浏览器 location 的网址目录,以 / 结尾 --- */
16+
let location = window.location.href;
17+
if (!location.endsWith('/')) {
18+
const lio = location.lastIndexOf('/');
19+
location = location.slice(0, lio + 1);
20+
}
1521
const loader: ILoader = {
1622
'isReady': false,
1723
'readys': [],
@@ -157,7 +163,7 @@
157163
opt.cache = {};
158164
}
159165
if (opt.dir === undefined) {
160-
opt.dir = location.href;
166+
opt.dir = location;
161167
}
162168
if (opt.invoke === undefined) {
163169
opt.invoke = {};
@@ -545,7 +551,7 @@ return module.exports;`;
545551
opt.init = {};
546552
}
547553
if (opt.dir === undefined) {
548-
opt.dir = location.href;
554+
opt.dir = location;
549555
}
550556
if (opt.before === undefined) {
551557
opt.before = '';
@@ -798,7 +804,7 @@ return module.exports;`;
798804
'preprocess'?: (code: string, path: string) => string;
799805
} = {}): Promise<any> {
800806
if (opt.dir === undefined) {
801-
opt.dir = location.href;
807+
opt.dir = location;
802808
}
803809
url = this.moduleNameResolve(url, opt.dir, opt.map);
804810
if (files[url]) {
@@ -818,8 +824,15 @@ return module.exports;`;
818824

819825
moduleNameResolve: function(path: string, dir: string, map: Record<string, string> = {}): string {
820826
// --- 查询是否有映射 ---
821-
if (map[path]) {
822-
path = map[path];
827+
for (const key in map) {
828+
if (!path.startsWith(key)) {
829+
continue;
830+
}
831+
let val = map[key];
832+
if (val.startsWith('.')) {
833+
val = location + val;
834+
}
835+
path = val + path.slice(key.length);
823836
}
824837
path = this.urlResolve(dir, path);
825838
// --- 是否自动加 index ---
@@ -923,13 +936,13 @@ return module.exports;`;
923936
to = to.replace(/\\/g, '/');
924937
// --- to 为空,直接返回 form ---
925938
if (to === '') {
926-
return from;
939+
return this.urlAtom(from);
927940
}
928941
// --- 获取 from 的 scheme, host, path ---
929942
const f = this.parseUrl(from);
930943
// --- 以 // 开头的,加上 from 的 protocol 返回 ---
931944
if (to.startsWith('//')) {
932-
return f.protocol ? f.protocol + to : to;
945+
return this.urlAtom(f.protocol ? f.protocol + to : to);
933946
}
934947
if (f.protocol) {
935948
// --- 获取小写的 protocol ---
@@ -940,16 +953,16 @@ return module.exports;`;
940953
// --- 已经是绝对路径,直接返回 ---
941954
if (t.protocol) {
942955
// --- 获取小写的 protocol ---
943-
return t.protocol + to.slice(t.protocol.length);
956+
return this.urlAtom(t.protocol + to.slice(t.protocol.length));
944957
}
945958
// --- # 或 ? 替换后返回 ---
946959
if (to.startsWith('#') || to.startsWith('?')) {
947960
const sp = from.indexOf(to[0]);
948961
if (sp !== -1) {
949-
return from.slice(0, sp) + to;
962+
return this.urlAtom(from.slice(0, sp) + to);
950963
}
951964
else {
952-
return from + to;
965+
return this.urlAtom(from + to);
953966
}
954967
}
955968
// --- 处理后面的尾随路径 ---
@@ -965,23 +978,28 @@ return module.exports;`;
965978
// --- abs 是 /xx/xx 了,因为如果 path 是空,则跟上了 /,如果 path 不为空,也是 / 开头 ---
966979
abs += path + '/' + to;
967980
}
968-
// --- 删掉 ./ ---
969-
abs = abs.replace(/\/\.\//g, '/');
970-
// --- 删掉 ../ ---
971-
while (/\/(?!\.\.)[^/]+\/\.\.\//.test(abs)) {
972-
abs = abs.replace(/\/(?!\.\.)[^/]+\/\.\.\//g, '/');
973-
}
974-
// --- 剩下的 ../ 就是无效的直接替换为空 ---
975-
abs = abs.replace(/\.\.\//g, '');
976981
// --- 返回最终结果 ---
977982
if (f.protocol && (f.protocol !== 'file:') && !f.host) {
978983
// --- 类似 c:/ ---
979-
return f.protocol + abs;
984+
return this.urlAtom(f.protocol + abs);
980985
}
981986
else {
982987
// --- 类似 http:// ---
983-
return (f.protocol ? f.protocol + '//' : '') + abs;
988+
return this.urlAtom((f.protocol ? f.protocol + '//' : '') + abs);
989+
}
990+
},
991+
992+
urlAtom: function(url: string): string {
993+
// --- 删掉 ./ ---
994+
while (url.includes('/./')) {
995+
url = url.replace(/\/\.\//g, '/');
996+
}
997+
// --- 删掉 ../ ---
998+
while (/\/(?!\.\.)[^/]+\/\.\.\//.test(url)) {
999+
url = url.replace(/\/(?!\.\.)[^/]+\/\.\.\//g, '/');
9841000
}
1001+
url = url.replace(/\.\.\//g, '');
1002+
return url;
9851003
},
9861004

9871005
isEscapeChar: function(index: number, code: string): boolean {

0 commit comments

Comments
 (0)