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

Commit 408e731

Browse files
committed
v3.4.1
1 parent 2d63fe8 commit 408e731

17 files changed

+112
-46
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/loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
365365
code = 'let ' + ikey + ' = __invoke.' + ikey + ';' + code;
366366
}
367367
code = `${strict}
368-
var __dirname='${dirname}';var __filename='${path}';var module={exports:__cache['${path}']};var exports = module.exports;function importOverride(url){return loader.import(url,__files,{'cache':__cache,'map':__map,'dir':__filename,'style':${opt.style ? '\'' + opt.style + '\'' : 'undefined'}});}function require(path){var m=loader.require(path,__files,{'cache':__cache,'map':__map,'dir':__filename,'style':${opt.style ? '\'' + opt.style + '\'' : 'undefined'},'invoke':__invoke});if(m[0]){return m[0];}else{throw 'Failed require "'+path+'" on "'+__filename+'" (Maybe file not found).';}}require.cache=__cache;
368+
var __dirname='${dirname}';var __filename='${path}';var module={exports:__cache['${path}']};var exports = module.exports;function importOverride(url){return loader.import(url,__files,{'cache':__cache,'map':__map,'dir':__filename,'style':${opt.style ? '\'' + opt.style + '\'' : 'undefined'},'invoke':__invoke,'preprocess':__preprocess});}function require(path){var m=loader.require(path,__files,{'cache':__cache,'map':__map,'dir':__filename,'style':${opt.style ? '\'' + opt.style + '\'' : 'undefined'},'invoke':__invoke,'preprocess':__preprocess});if(m[0]){return m[0];}else{throw 'Failed require "'+path+'" on "'+__filename+'" (Maybe file not found).';}}require.cache=__cache;
369369
require.resolve=function(name){return loader.moduleNameResolve(name,__dirname,__map);};
370370
${code}
371371
${needExports.join('')}
372372
return module.exports;`;
373373
opt.cache[path] = {};
374-
const rtn = (new Function('__files', '__cache', '__map', '__invoke', code))(files, opt.cache, opt.map, opt.invoke);
374+
const rtn = (new Function('__files', '__cache', '__map', '__invoke', '__preprocess', code))(files, opt.cache, opt.map, opt.invoke, opt.preprocess);
375375
if (rtn !== opt.cache[path]) {
376376
opt.cache[path] = rtn;
377377
}

dist/loader.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,9 @@
438438
'cache': __cache,
439439
'map': __map,
440440
'dir': __filename,
441-
'style': ${opt.style ? '\'' + opt.style + '\'' : 'undefined'}
441+
'style': ${opt.style ? '\'' + opt.style + '\'' : 'undefined'},
442+
'invoke': __invoke,
443+
'preprocess': __preprocess
442444
});
443445
}
444446
@@ -448,7 +450,8 @@
448450
'map': __map,
449451
'dir': __filename,
450452
'style': ${opt.style ? '\'' + opt.style + '\'' : 'undefined'},
451-
'invoke': __invoke
453+
'invoke': __invoke,
454+
'preprocess': __preprocess
452455
});
453456
if (m[0]) {
454457
return m[0];
@@ -469,14 +472,14 @@
469472
return module.exports;`;
470473
*/
471474
code = `${strict}
472-
var __dirname='${dirname}';var __filename='${path}';var module={exports:__cache['${path}']};var exports = module.exports;function importOverride(url){return loader.import(url,__files,{'cache':__cache,'map':__map,'dir':__filename,'style':${opt.style ? '\'' + opt.style + '\'' : 'undefined'}});}function require(path){var m=loader.require(path,__files,{'cache':__cache,'map':__map,'dir':__filename,'style':${opt.style ? '\'' + opt.style + '\'' : 'undefined'},'invoke':__invoke});if(m[0]){return m[0];}else{throw 'Failed require "'+path+'" on "'+__filename+'" (Maybe file not found).';}}require.cache=__cache;
475+
var __dirname='${dirname}';var __filename='${path}';var module={exports:__cache['${path}']};var exports = module.exports;function importOverride(url){return loader.import(url,__files,{'cache':__cache,'map':__map,'dir':__filename,'style':${opt.style ? '\'' + opt.style + '\'' : 'undefined'},'invoke':__invoke,'preprocess':__preprocess});}function require(path){var m=loader.require(path,__files,{'cache':__cache,'map':__map,'dir':__filename,'style':${opt.style ? '\'' + opt.style + '\'' : 'undefined'},'invoke':__invoke,'preprocess':__preprocess});if(m[0]){return m[0];}else{throw 'Failed require "'+path+'" on "'+__filename+'" (Maybe file not found).';}}require.cache=__cache;
473476
require.resolve=function(name){return loader.moduleNameResolve(name,__dirname,__map);};
474477
${code}
475478
${needExports.join('')}
476479
return module.exports;`;
477480
/** --- 先创建本文件的 cache 对象,以防止不断重复创建,模拟 node 创建流程 --- */
478481
opt.cache[path] = {};
479-
const rtn = (new Function('__files', '__cache', '__map', '__invoke', code))(files, opt.cache, opt.map, opt.invoke);
482+
const rtn = (new Function('__files', '__cache', '__map', '__invoke', '__preprocess', code))(files, opt.cache, opt.map, opt.invoke, opt.preprocess);
480483
if (rtn !== opt.cache[path]) {
481484
opt.cache[path] = rtn;
482485
}

dist/test-on-browser.js

Lines changed: 32 additions & 15 deletions
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;
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;
1313
return __awaiter(this, void 0, void 0, function* () {
1414
const keyInput = document.getElementById('key');
1515
const consoleDiv = document.getElementById('console');
@@ -106,7 +106,8 @@ loader.ready(function () {
106106
'../dist/tjson.json',
107107
'../dist/tmodule.js',
108108
'../dist/tmodule2.js',
109-
'../dist/tmodule3.js'
109+
'../dist/tmodule3.js',
110+
'../dist/tmodule4.js'
110111
]);
111112
const tmodule = loader.require('../dist/tmodule', files, {
112113
'cache': cache,
@@ -248,13 +249,19 @@ loader.ready(function () {
248249
});
249250
})();
250251
});
251-
(_j = document.getElementById('getFiles')) === null || _j === void 0 ? void 0 : _j.addEventListener('click', function () {
252+
(_j = document.getElementById('loadDefault')) === null || _j === void 0 ? void 0 : _j.addEventListener('click', function () {
253+
const m = loader.require('../dist/tmodule4.js', files, {
254+
'cache': cache
255+
});
256+
console.log(m);
257+
});
258+
(_k = document.getElementById('getFiles')) === null || _k === void 0 ? void 0 : _k.addEventListener('click', function () {
252259
console.log(Object.keys(files));
253260
});
254-
(_k = document.getElementById('getCache')) === null || _k === void 0 ? void 0 : _k.addEventListener('click', function () {
261+
(_l = document.getElementById('getCache')) === null || _l === void 0 ? void 0 : _l.addEventListener('click', function () {
255262
console.log(cache);
256263
});
257-
(_l = document.getElementById('runTestOnNode')) === null || _l === void 0 ? void 0 : _l.addEventListener('click', function () {
264+
(_m = document.getElementById('runTestOnNode')) === null || _m === void 0 ? void 0 : _m.addEventListener('click', function () {
258265
(function () {
259266
return __awaiter(this, void 0, void 0, function* () {
260267
mask.style.display = 'flex';
@@ -271,7 +278,7 @@ loader.ready(function () {
271278
});
272279
})();
273280
});
274-
(_m = document.getElementById('runTestOnNodeLoop')) === null || _m === void 0 ? void 0 : _m.addEventListener('click', function () {
281+
(_o = document.getElementById('runTestOnNodeLoop')) === null || _o === void 0 ? void 0 : _o.addEventListener('click', function () {
275282
(function () {
276283
return __awaiter(this, void 0, void 0, function* () {
277284
mask.style.display = 'flex';
@@ -288,7 +295,17 @@ loader.ready(function () {
288295
});
289296
})();
290297
});
291-
(_o = document.getElementById('runTypeGuard')) === null || _o === void 0 ? void 0 : _o.addEventListener('click', function () {
298+
(_p = document.getElementById('runThe1ToThe4')) === null || _p === void 0 ? void 0 : _p.addEventListener('click', function () {
299+
const cache1 = {};
300+
const m = loader.require('../dist/tmodule.js', files, {
301+
'cache': cache1,
302+
'preprocess': (code) => {
303+
return code.replace(/'the1'/g, '\'the4\'');
304+
}
305+
});
306+
console.log('filename:', m[0].tm2fn, 'd1:', m[0].getData('d1'));
307+
});
308+
(_q = document.getElementById('runTypeGuard')) === null || _q === void 0 ? void 0 : _q.addEventListener('click', function () {
292309
(function () {
293310
return __awaiter(this, void 0, void 0, function* () {
294311
mask.style.display = 'flex';
@@ -326,7 +343,7 @@ loader.ready(function () {
326343
});
327344
})();
328345
});
329-
(_p = document.getElementById('runResizeObserverESM')) === null || _p === void 0 ? void 0 : _p.addEventListener('click', function () {
346+
(_r = document.getElementById('runResizeObserverESM')) === null || _r === void 0 ? void 0 : _r.addEventListener('click', function () {
330347
(function () {
331348
return __awaiter(this, void 0, void 0, function* () {
332349
mask.style.display = 'flex';
@@ -344,7 +361,7 @@ loader.ready(function () {
344361
});
345362
})();
346363
});
347-
(_q = document.getElementById('runResizeObserverUMD')) === null || _q === void 0 ? void 0 : _q.addEventListener('click', function () {
364+
(_s = document.getElementById('runResizeObserverUMD')) === null || _s === void 0 ? void 0 : _s.addEventListener('click', function () {
348365
(function () {
349366
return __awaiter(this, void 0, void 0, function* () {
350367
mask.style.display = 'flex';
@@ -362,7 +379,7 @@ loader.ready(function () {
362379
});
363380
})();
364381
});
365-
(_r = document.getElementById('runMonacoEditor')) === null || _r === void 0 ? void 0 : _r.addEventListener('click', function () {
382+
(_t = document.getElementById('runMonacoEditor')) === null || _t === void 0 ? void 0 : _t.addEventListener('click', function () {
366383
(function () {
367384
return __awaiter(this, void 0, void 0, function* () {
368385
const monacoDiv = document.getElementById('monacoDiv');
@@ -415,7 +432,7 @@ loader.ready(function () {
415432
});
416433
})();
417434
});
418-
(_s = document.getElementById('AddFetchFilesAdapter')) === null || _s === void 0 ? void 0 : _s.addEventListener('click', function () {
435+
(_u = document.getElementById('AddFetchFilesAdapter')) === null || _u === void 0 ? void 0 : _u.addEventListener('click', function () {
419436
(function () {
420437
return __awaiter(this, void 0, void 0, function* () {
421438
const tmpFiles = {};
@@ -445,7 +462,7 @@ loader.ready(function () {
445462
});
446463
})();
447464
});
448-
(_t = document.getElementById('AddSniffFilesAdapter')) === null || _t === void 0 ? void 0 : _t.addEventListener('click', function () {
465+
(_v = document.getElementById('AddSniffFilesAdapter')) === null || _v === void 0 ? void 0 : _v.addEventListener('click', function () {
449466
(function () {
450467
return __awaiter(this, void 0, void 0, function* () {
451468
const tmpFiles = {};
@@ -470,10 +487,10 @@ loader.ready(function () {
470487
});
471488
})();
472489
});
473-
(_u = document.getElementById('runRemoveComment')) === null || _u === void 0 ? void 0 : _u.addEventListener('click', function () {
490+
(_w = document.getElementById('runRemoveComment')) === null || _w === void 0 ? void 0 : _w.addEventListener('click', function () {
474491
document.getElementById('removeComment2').value = loader.removeComment(document.getElementById('removeComment1').value);
475492
});
476-
(_v = document.getElementById('runParseUrl')) === null || _v === void 0 ? void 0 : _v.addEventListener('click', function () {
493+
(_x = document.getElementById('runParseUrl')) === null || _x === void 0 ? void 0 : _x.addEventListener('click', function () {
477494
document.getElementById('runParseUrl2').value = JSON.stringify(loader.parseUrl(document.getElementById('runParseUrl1').value), null, 4);
478495
});
479496
const urlResolve1 = document.getElementById('urlResolve1');
@@ -498,7 +515,7 @@ loader.ready(function () {
498515
}
499516
}
500517
});
501-
(_w = document.getElementById('urlResolve')) === null || _w === void 0 ? void 0 : _w.addEventListener('click', function () {
518+
(_y = document.getElementById('urlResolve')) === null || _y === void 0 ? void 0 : _y.addEventListener('click', function () {
502519
document.getElementById('urlResolve3').innerText = loader.urlResolve(urlResolve1.value, urlResolve2.value);
503520
});
504521
});

dist/test-on-browser.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ loader.ready(async function(): Promise<void> {
102102
'../dist/tjson.json',
103103
'../dist/tmodule.js',
104104
'../dist/tmodule2.js',
105-
'../dist/tmodule3.js'
105+
'../dist/tmodule3.js',
106+
'../dist/tmodule4.js'
106107
]);
107108
const tmodule = loader.require('../dist/tmodule', files, {
108109
'cache': cache,
@@ -242,6 +243,12 @@ loader.ready(async function(): Promise<void> {
242243
console.log(`getData: ${m.getData(keyInput.value)}, getSubStr: ${m.getSubStr()}, getRand: ${m.getRand()}`);
243244
})() as unknown;
244245
});
246+
document.getElementById('loadDefault')?.addEventListener('click', function() {
247+
const m = loader.require('../dist/tmodule4.js', files, {
248+
'cache': cache
249+
});
250+
console.log(m);
251+
});
245252
document.getElementById('getFiles')?.addEventListener('click', function() {
246253
console.log(Object.keys(files));
247254
});
@@ -281,6 +288,17 @@ loader.ready(async function(): Promise<void> {
281288
})() as unknown;
282289
});
283290

291+
document.getElementById('runThe1ToThe4')?.addEventListener('click', function() {
292+
const cache1 = {};
293+
const m = loader.require('../dist/tmodule.js', files, {
294+
'cache': cache1,
295+
'preprocess': (code) => {
296+
return code.replace(/'the1'/g, '\'the4\'');
297+
}
298+
});
299+
console.log('filename:', m[0].tm2fn, 'd1:', m[0].getData('d1'));
300+
});
301+
284302
document.getElementById('runTypeGuard')?.addEventListener('click', function() {
285303
(async function() {
286304
mask.style.display = 'flex';

dist/test-on-node.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
const tmodule = require("./tmodule");
4+
const tmodule4 = require("./tmodule4");
45
console.log(`getData('d1'): ${tmodule.getData('d1')}`);
56
console.log(`getData('d2'): ${tmodule.getData('d2')}`);
67
console.log(`getData('d3'): ${tmodule.getData('d3')}`);
@@ -9,6 +10,7 @@ console.log(`getJson('d4'): ${tmodule.getJson('d4')}`);
910
console.log(`getJson('d5'): ${tmodule.getJson('d5')}`);
1011
console.log(`getJson('hello'): ${tmodule.getJson('hello')}`);
1112
console.log(`getJson('goodbye'): ${tmodule.getJson('goodbye')}`);
13+
console.log('tmodule4:', tmodule4);
1214
tmodule.requireModule3().then(function (n) {
1315
console.log(`requireModule3(): ${n}`);
1416
}).catch(function (e) {

dist/test-on-node.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as tmodule from './tmodule';
2+
import * as tmodule4 from './tmodule4';
23

34
console.log(`getData('d1'): ${tmodule.getData('d1')}`);
45

@@ -16,6 +17,8 @@ console.log(`getJson('hello'): ${tmodule.getJson('hello')}`);
1617

1718
console.log(`getJson('goodbye'): ${tmodule.getJson('goodbye')}`);
1819

20+
console.log('tmodule4:', tmodule4);
21+
1922
tmodule.requireModule3().then(function(n) {
2023
console.log(`requireModule3(): ${n}`);
2124
}).catch(function(e) {

dist/tmodule.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
99
});
1010
};
1111
Object.defineProperty(exports, "__esModule", { value: true });
12-
exports.runInvokeFunction = exports.requireModule3 = exports.getJson = exports.getData = void 0;
12+
exports.runInvokeFunction = exports.requireModule3 = exports.getJson = exports.getData = exports.tm2fn = void 0;
1313
const tmodule2 = require("./tmodule2");
1414
const tjson = require("./tjson.json");
15+
exports.tm2fn = tmodule2.fn;
1516
function getData(key) {
1617
var _a;
1718
return (_a = tmodule2.data[key]) !== null && _a !== void 0 ? _a : 'nothing';

dist/tmodule.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as tmodule2 from './tmodule2';
22
import * as tjson from './tjson.json';
33

4+
/** --- 获取 tmodule2 的 filename --- */
5+
export const tm2fn = tmodule2.fn;
6+
47
/**
58
* --- 根据 key 获取 tmodule2 的值 ---
69
* @param key 键

dist/tmodule2.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.data = void 0;
3+
exports.fn = exports.data = void 0;
44
exports.data = {
55
'd1': 'the1',
66
'd2': 'the2',
77
'd3': 'the3'
88
};
9+
exports.fn = __filename;

0 commit comments

Comments
 (0)