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

Commit 19dee27

Browse files
committed
Add preprocess.
1 parent 26d4cd5 commit 19dee27

File tree

6 files changed

+18
-1
lines changed

6 files changed

+18
-1
lines changed

dist/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ const loader = {
122122
}
123123
else {
124124
let needExports = [];
125+
if (opt.preprocess) {
126+
code = opt.preprocess(code, path);
127+
}
125128
code = this.removeComment(code);
126129
let strict = '';
127130
if (code.includes('"use strict"')) {

dist/test-on-browser.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ loader.ready(function () {
104104
alert('The invoke function.');
105105
},
106106
'location': 'The override var.'
107+
},
108+
'preprocess': function (code, path) {
109+
console.log(`Replace file "${path}" content.`);
110+
return code.replace('nothing', 'none');
107111
}
108112
})[0];
109113
(_a = document.getElementById('clear')) === null || _a === void 0 ? void 0 : _a.addEventListener('click', function () {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@litert/loader",
3-
"version": "2.1.2-rc3",
3+
"version": "2.1.3-rc4",
44
"description": "Simple browser module loader.",
55
"keywords": [
66
"litert",

sources/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const loader: ILoader = {
6161
'dir'?: string;
6262
'style'?: string;
6363
'invoke'?: Record<string, any>;
64+
'preprocess'?: (code: string, path: string) => string;
6465
} = {}): any[] {
6566
if (typeof paths === 'string') {
6667
paths = [paths];
@@ -140,6 +141,10 @@ const loader: ILoader = {
140141
else {
141142
/** --- 代码末尾增加 exports.xxx = --- */
142143
let needExports: string[] = [];
144+
// --- 预处理代码 ---
145+
if (opt.preprocess) {
146+
code = opt.preprocess(code, path);
147+
}
143148
// --- 去除 // 注释、/* 注释 ---
144149
code = this.removeComment(code);
145150
// --- 先去除严格模式字符串 ---

sources/test-on-browser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ loader.ready(async function(): Promise<void> {
9999
alert('The invoke function.');
100100
},
101101
'location': 'The override var.'
102+
},
103+
'preprocess': function(code: string, path: string): string {
104+
console.log(`Replace file "${path}" content.`);
105+
return code.replace('nothing', 'none');
102106
}
103107
})[0];
104108

types/loader.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ interface ILoader {
2626
'dir'?: string;
2727
'style'?: string;
2828
'invoke'?: Record<string, any>;
29+
'preprocess'?: (code: string, path: string) => string;
2930
}): any[];
3031
/**
3132
* --- 简单 fetch 获取网络数据 ---

0 commit comments

Comments
 (0)