@oipage/cli 0.3.0-alpha.5 → 1.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG +30 -1
- package/README.md +134 -62
- package/bin/WebSocket/index.js +1 -1
- package/bin/data/mineTypes.json +5 -1
- package/bin/help.js +8 -0
- package/bin/notebook/data.js +185 -0
- package/bin/notebook/index.js +163 -4
- package/bin/run +2 -2
- package/bin/serve.js +191 -54
- package/bin/tools/moduleForm.js +548 -0
- package/bin/tools/resolve404.js +2 -2
- package/bin/tools/resolveImport.js +248 -67
- package/bin/tools/resolvePackage.js +412 -0
- package/bin/website-plugins/intercept/index.js +0 -4
- package/docs/css/base.css +122 -0
- package/docs/css/components.css +180 -0
- package/docs/css/layout.css +295 -0
- package/docs/css/responsive.css +49 -0
- package/docs/images/file-list.webp +0 -0
- package/docs/images/logo-topbar.svg +5 -0
- package/docs/images/logo.png +0 -0
- package/docs/images/logo.svg +6 -0
- package/docs/images/manual.webp +0 -0
- package/docs/images/terminal-serve.webp +0 -0
- package/docs/index.html +1392 -0
- package/docs/js/copy.js +37 -0
- package/docs/js/nav.js +111 -0
- package/docs/js/search.js +241 -0
- package/docs/js/theme.js +17 -0
- package/package.json +1 -1
- package/src/index.js +0 -3
- package/bin/website-plugins/intercept/oipage-vislite-intercept.js +0 -56
- package/bin/website-plugins/intercept/oipage-zipaper-intercept.js +0 -37
- package/docs/config.md +0 -107
package/bin/notebook/index.js
CHANGED
|
@@ -1,9 +1,168 @@
|
|
|
1
|
+
/* ============================================================
|
|
2
|
+
oipage-cli notebook:常用命令速查
|
|
3
|
+
不带参数看分类,给分类名看整组,给关键词就全量搜
|
|
4
|
+
============================================================ */
|
|
5
|
+
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
const groups = require("./data.js");
|
|
9
|
+
|
|
10
|
+
const useColor = !process.env.NO_COLOR && (process.stdout.isTTY || process.env.FORCE_COLOR);
|
|
11
|
+
|
|
12
|
+
const paint = function (code, text) {
|
|
13
|
+
return useColor ? "\x1b[" + code + "m" + text + "\x1b[0m" : String(text);
|
|
14
|
+
};
|
|
15
|
+
const accent = text => paint("36", text);
|
|
16
|
+
const strong = text => paint("1;36", text);
|
|
17
|
+
const faint = text => paint("2", text);
|
|
18
|
+
|
|
19
|
+
// 中文、全角字符在终端里占两格,补空格时要按显示宽度算
|
|
20
|
+
const WIDE = /[\u1100-\u115F\u2E80-\uA4CF\uAC00-\uD7A3\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFF60\uFFE0-\uFFE6\u{1F300}-\u{1FAFF}]/u;
|
|
21
|
+
|
|
22
|
+
const width = function (text) {
|
|
23
|
+
let value = 0;
|
|
24
|
+
for (const ch of text) value += WIDE.test(ch) ? 2 : 1;
|
|
25
|
+
return value;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const pad = function (text, size) {
|
|
29
|
+
return text + " ".repeat(Math.max(2, size - width(text) + 2));
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const total = groups.reduce((sum, group) => sum + group.items.length, 0);
|
|
33
|
+
|
|
34
|
+
const overview = function () {
|
|
35
|
+
console.log("");
|
|
36
|
+
console.log(strong("oipage-cli notebook") + faint(" · " + groups.length + " 类 / " + total + " 条常用命令"));
|
|
37
|
+
console.log("");
|
|
38
|
+
|
|
39
|
+
console.log(faint("用法"));
|
|
40
|
+
const usages = [
|
|
41
|
+
["oipage-cli notebook", "列出分类"],
|
|
42
|
+
["oipage-cli notebook git", "看某类的全部命令"],
|
|
43
|
+
["oipage-cli notebook 端口", "按关键词搜,命令和说明都搜"],
|
|
44
|
+
["oipage-cli notebook all", "全部打印出来"]
|
|
45
|
+
];
|
|
46
|
+
usages.forEach(function (row) {
|
|
47
|
+
console.log(" " + accent(pad(row[0], 26)) + faint(row[1]));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
console.log("");
|
|
51
|
+
console.log(faint("分类"));
|
|
52
|
+
groups.forEach(function (group) {
|
|
53
|
+
console.log(" " + accent(pad(group.key, 9)) +
|
|
54
|
+
faint(pad(group.items.length + " 条", 7)) +
|
|
55
|
+
faint(group.note));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
console.log("");
|
|
59
|
+
console.log(faint("想不起来命令怎么写,直接搜关键词,比如:") + " " +
|
|
60
|
+
accent("oipage-cli notebook 端口") + faint(" / ") + accent("oipage-cli notebook 撤销"));
|
|
61
|
+
console.log("");
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const printGroup = function (group, items) {
|
|
65
|
+
console.log("");
|
|
66
|
+
console.log(strong(group.title) + faint(" " + items.length + " 条 · " + group.note));
|
|
67
|
+
console.log("");
|
|
68
|
+
|
|
69
|
+
// 命令列按最长的一条对齐,太长就单独占一行,说明挪到下一行
|
|
70
|
+
let col = 0;
|
|
71
|
+
items.forEach(function (item) {
|
|
72
|
+
col = Math.max(col, width(item[0]));
|
|
73
|
+
});
|
|
74
|
+
col = Math.min(col, 44);
|
|
75
|
+
|
|
76
|
+
items.forEach(function (item) {
|
|
77
|
+
if (width(item[0]) > col) {
|
|
78
|
+
console.log(" " + accent(item[0]));
|
|
79
|
+
console.log(" " + faint(item[1]));
|
|
80
|
+
} else {
|
|
81
|
+
console.log(" " + accent(pad(item[0], col)) + faint(item[1]));
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
console.log("");
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// 命中位置越"像命令的用途"越靠前:说明 > 命令 > 分类名
|
|
88
|
+
const score = function (group, command, text, terms) {
|
|
89
|
+
const groupText = (group.title + " " + group.aliases.join(" ")).toLowerCase();
|
|
90
|
+
const commandText = command.toLowerCase();
|
|
91
|
+
const descText = text.toLowerCase();
|
|
92
|
+
|
|
93
|
+
let value = 0;
|
|
94
|
+
for (const term of terms) {
|
|
95
|
+
if (descText.indexOf(term) !== -1) value += 3;
|
|
96
|
+
else if (commandText.indexOf(term) !== -1) value += 2;
|
|
97
|
+
else if (groupText.indexOf(term) !== -1) value += 1;
|
|
98
|
+
else return 0;
|
|
99
|
+
}
|
|
100
|
+
return value;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const search = function (terms, query, scope) {
|
|
104
|
+
const pool = scope ? [scope] : groups;
|
|
105
|
+
const results = pool.map(function (group) {
|
|
106
|
+
return {
|
|
107
|
+
group: group,
|
|
108
|
+
items: group.items
|
|
109
|
+
.map(item => ({ item: item, score: score(group, item[0], item[1], terms) }))
|
|
110
|
+
.filter(row => row.score > 0)
|
|
111
|
+
.sort((a, b) => b.score - a.score)
|
|
112
|
+
.map(row => row.item)
|
|
113
|
+
};
|
|
114
|
+
}).filter(result => result.items.length);
|
|
115
|
+
|
|
116
|
+
console.log("");
|
|
117
|
+
console.log(strong("「" + query + "」") + faint(" 命中 " +
|
|
118
|
+
results.reduce((sum, result) => sum + result.items.length, 0) + " 条"));
|
|
119
|
+
|
|
120
|
+
if (!results.length) {
|
|
121
|
+
console.log("");
|
|
122
|
+
console.log(" 没找到。可以换个词,或者看全部:" + accent("oipage-cli notebook all"));
|
|
123
|
+
console.log("");
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
results.forEach(function (result) {
|
|
128
|
+
printGroup(result.group, result.items);
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
|
|
1
132
|
module.exports = function (config) {
|
|
2
133
|
|
|
3
|
-
|
|
134
|
+
// formatArgv 把位置参数统一放在 args 里
|
|
135
|
+
const args = config.args || [];
|
|
136
|
+
|
|
137
|
+
if (!args.length) {
|
|
138
|
+
overview();
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const first = args[0].toLowerCase();
|
|
143
|
+
|
|
144
|
+
if (first === "all" || first === "全部") {
|
|
145
|
+
groups.forEach(group => printGroup(group, group.items));
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const scope = groups.filter(function (group) {
|
|
150
|
+
return group.key === first || group.aliases.indexOf(first) !== -1;
|
|
151
|
+
})[0];
|
|
152
|
+
|
|
153
|
+
if (scope && args.length === 1) {
|
|
154
|
+
printGroup(scope, scope.items);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
4
157
|
|
|
5
|
-
|
|
158
|
+
const words = scope ? args.slice(1) : args;
|
|
159
|
+
const query = words.join(" ");
|
|
160
|
+
const terms = query.toLowerCase().split(/\s+/).filter(Boolean);
|
|
6
161
|
|
|
7
|
-
|
|
162
|
+
if (!terms.length) {
|
|
163
|
+
overview();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
8
166
|
|
|
9
|
-
|
|
167
|
+
search(terms, query, scope);
|
|
168
|
+
};
|
package/bin/run
CHANGED
|
@@ -56,8 +56,8 @@ if (process.argv[2] === "serve") {
|
|
|
56
56
|
|
|
57
57
|
let resolveBoolean = input => input[0] !== "false";
|
|
58
58
|
|
|
59
|
-
if (argvObj["--port"]) config.devServer.port =
|
|
60
|
-
if (argvObj["--baseUrl"]) config.devServer.baseUrl =
|
|
59
|
+
if (argvObj["--port"]) config.devServer.port = Number(argvObj["--port"][0]) || config.devServer.port;
|
|
60
|
+
if (argvObj["--baseUrl"]) config.devServer.baseUrl = argvObj["--baseUrl"][0];
|
|
61
61
|
if (argvObj["--cache"]) config.devServer.cache = resolveBoolean(argvObj["--cache"]);
|
|
62
62
|
if (argvObj["--gzip"]) config.devServer.gzip = resolveBoolean(argvObj["--gzip"]);
|
|
63
63
|
if (argvObj["--open"]) config.devServer.open = resolveBoolean(argvObj["--open"]);
|
package/bin/serve.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
const { join, resolve, sep } = require("path");
|
|
2
|
-
const { existsSync,
|
|
2
|
+
const { existsSync, statSync, createReadStream, readFileSync } = require("fs");
|
|
3
3
|
const { createServer } = require('http');
|
|
4
4
|
const { gzipSync, createGzip } = require("zlib");
|
|
5
5
|
const packageValue = require("../package.json");
|
|
6
6
|
const network = require("./tools/network.js");
|
|
7
7
|
const mineTypes = require("./data/mineTypes.json");
|
|
8
8
|
const resolveImportFactory = require("./tools/resolveImport.js");
|
|
9
|
+
const { replaceInCode } = require("./tools/moduleForm.js");
|
|
9
10
|
const openBrowser = require("./tools/openBrowser.js");
|
|
10
11
|
const { doIntercept } = require("./intercept.js");
|
|
11
12
|
const proxy = require("./proxy.js");
|
|
@@ -66,6 +67,35 @@ module.exports = function (config) {
|
|
|
66
67
|
const port = +config.devServer.port; // 端口号
|
|
67
68
|
const basePath = resolve((/^\./.test(config.devServer.baseUrl)) ? join(process.cwd(), config.devServer.baseUrl) : config.devServer.baseUrl); // 服务器根路径
|
|
68
69
|
|
|
70
|
+
// 允许访问的根目录:服务器根目录、命令行当前目录、命令行工具自身的 node_modules
|
|
71
|
+
const cliRoot = resolve(__dirname, "..");
|
|
72
|
+
const cliModules = join(cliRoot, "node_modules");
|
|
73
|
+
|
|
74
|
+
// /@modules/ 的候选根目录(先工程,再命令行工具自身,保证内置网站能拿到自带依赖)
|
|
75
|
+
const moduleRoots = [join(process.cwd(), "node_modules"), cliModules];
|
|
76
|
+
|
|
77
|
+
// 模块导出条件(对齐打包器的默认顺序)
|
|
78
|
+
const conditions = Array.isArray(config.devServer.conditions) && config.devServer.conditions.length
|
|
79
|
+
? config.devServer.conditions
|
|
80
|
+
: ["browser", "import", "default"];
|
|
81
|
+
|
|
82
|
+
// 类似 webpack DefinePlugin 的文本替换,false 表示关闭
|
|
83
|
+
const define = config.devServer.define === false ? {} : Object.assign({
|
|
84
|
+
"process.env.NODE_ENV": "\"development\""
|
|
85
|
+
}, config.devServer.define || {});
|
|
86
|
+
|
|
87
|
+
// 只在代码区替换,不误伤字符串与注释
|
|
88
|
+
const applyDefine = content => {
|
|
89
|
+
const keys = Object.keys(define);
|
|
90
|
+
if (!keys.length) return content;
|
|
91
|
+
|
|
92
|
+
let result = content;
|
|
93
|
+
for (const key of keys) result = replaceInCode(result, key, define[key] + "");
|
|
94
|
+
return result;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
resolveImportFactory.configure({ conditions });
|
|
98
|
+
|
|
69
99
|
const name = (config.name || "OIPage") + "-http-server";
|
|
70
100
|
const version = config.version || packageValue.version;
|
|
71
101
|
|
|
@@ -74,11 +104,11 @@ module.exports = function (config) {
|
|
|
74
104
|
// 全局错误捕获,防止服务器因任何未捕获的错误而停机
|
|
75
105
|
// 2026年8月8日 于南宁
|
|
76
106
|
process.on('uncaughtException', function (err) {
|
|
77
|
-
console.log("
|
|
107
|
+
console.log("\x1b[1m\x1b[31m[" + name + "] uncaughtException: " + err.message + "\x1b[0m " + new Date().toLocaleString());
|
|
78
108
|
console.log(err.stack);
|
|
79
109
|
});
|
|
80
110
|
process.on('unhandledRejection', function (reason) {
|
|
81
|
-
console.log("
|
|
111
|
+
console.log("\x1b[1m\x1b[31m[" + name + "] unhandledRejection: " + (reason && reason.message || reason) + "\x1b[0m " + new Date().toLocaleString());
|
|
82
112
|
});
|
|
83
113
|
|
|
84
114
|
let Server = createServer(function (request, response) {
|
|
@@ -110,6 +140,9 @@ module.exports = function (config) {
|
|
|
110
140
|
let urlArray = url.split("?");
|
|
111
141
|
url = urlArray[0];
|
|
112
142
|
|
|
143
|
+
// 查询串:CJS 门面标记、下载标记等都走这里
|
|
144
|
+
const query = urlArray.slice(1).join("?");
|
|
145
|
+
|
|
113
146
|
// 请求的文件路径
|
|
114
147
|
let filePath;
|
|
115
148
|
|
|
@@ -133,20 +166,37 @@ module.exports = function (config) {
|
|
|
133
166
|
|
|
134
167
|
// 请求拦截
|
|
135
168
|
if (doIntercept(url.replace(/^\/@website/, "").replace(/^\/@modules\//, ""), isWebsite ? websiteIntercept : config.devServer.intercept, request, response, wsHandler)) {
|
|
136
|
-
console.log("
|
|
169
|
+
console.log("\x1b[1m\x1b[32m[" + name + "] intercept: " + url + '\x1b[0m ' + new Date().toLocaleString() + " " + rawIp);
|
|
137
170
|
} else {
|
|
138
171
|
|
|
139
|
-
//
|
|
140
|
-
//
|
|
172
|
+
// 对应前置都没有找到的,默认去 node_modules 里找
|
|
173
|
+
// 默认先找命令行当前目录,再找命令行工具自身
|
|
174
|
+
// 带 oipage=cli 标记时反过来:内置网站的依赖必须取命令行工具自带的那一份
|
|
175
|
+
// 2026年4月13日 于南京 / 2026年11月25日 于南京
|
|
141
176
|
if (/^\/@modules\//.test(url)) {
|
|
142
|
-
|
|
177
|
+
const modulePath = url.replace(/^\/@modules\//, "");
|
|
178
|
+
const preferCliModule = /(^|&)oipage=cli($|&)/.test(query);
|
|
179
|
+
const roots = preferCliModule ? [cliModules].concat(moduleRoots) : moduleRoots;
|
|
180
|
+
|
|
181
|
+
filePath = null;
|
|
182
|
+
for (const root of roots) {
|
|
183
|
+
const candidate = resolve(join(root, modulePath));
|
|
184
|
+
if (existsSync(candidate)) {
|
|
185
|
+
filePath = candidate;
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// 都没找到时保留一个候选,交给后面的 404 逻辑
|
|
191
|
+
if (!filePath) filePath = resolve(join(roots[0], modulePath));
|
|
143
192
|
}
|
|
144
193
|
|
|
145
194
|
// 禁止访问沙箱外文件,路径穿越拦截
|
|
146
195
|
// 2026年8月8日 于南宁
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
196
|
+
const allowedRoots = [basePath, process.cwd(), cliModules];
|
|
197
|
+
const isAllowed = allowedRoots.some(root => filePath === root || filePath.startsWith(root + sep));
|
|
198
|
+
|
|
199
|
+
if (!isAllowed || filePath.includes('\0')) {
|
|
150
200
|
response.writeHead(403, {
|
|
151
201
|
'Content-Type': buildContentType("text/plain"),
|
|
152
202
|
'Access-Control-Allow-Origin': '*',
|
|
@@ -154,17 +204,40 @@ module.exports = function (config) {
|
|
|
154
204
|
});
|
|
155
205
|
response.write("Path Traversal attack");
|
|
156
206
|
response.end();
|
|
157
|
-
console.log("
|
|
207
|
+
console.log("\x1b[1m\x1b[31m[" + name + "] Path Traversal attack, path: " + url + "\x1b[0m " + new Date().toLocaleString() + " " + rawIp);
|
|
158
208
|
return;
|
|
159
209
|
}
|
|
160
210
|
|
|
211
|
+
// 目录请求:先尝试目录下的 index,再走 404(不能直接按文件流读取)
|
|
212
|
+
// 2026年11月25日 于南京
|
|
213
|
+
let fileStat = existsSync(filePath) ? statSync(filePath) : null;
|
|
214
|
+
|
|
215
|
+
if (fileStat && fileStat.isDirectory()) {
|
|
216
|
+
let indexFile = null;
|
|
217
|
+
|
|
218
|
+
for (const indexName of ["index.html", "index.js"]) {
|
|
219
|
+
const candidate = join(filePath, indexName);
|
|
220
|
+
if (existsSync(candidate) && statSync(candidate).isFile()) {
|
|
221
|
+
indexFile = candidate;
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
filePath = indexFile;
|
|
227
|
+
fileStat = indexFile ? statSync(filePath) : null;
|
|
228
|
+
}
|
|
229
|
+
|
|
161
230
|
// 如果存在且是文件
|
|
162
|
-
if (
|
|
231
|
+
if (fileStat && fileStat.isFile()) {
|
|
163
232
|
|
|
164
233
|
// 判断是否是请求而无需进一步解析
|
|
165
234
|
// 2025年12月5日 于南京
|
|
166
235
|
let isXHR = headers["sec-fetch-dest"] === "empty";
|
|
167
236
|
|
|
237
|
+
// 是否是 CommonJS / UMD 门面请求(由 import 改写时追加)
|
|
238
|
+
// 2026年11月25日 于南京
|
|
239
|
+
const isCjsShim = /(^|&)oipage=cjs($|&)/.test(query);
|
|
240
|
+
|
|
168
241
|
let dotName = (filePath.match(/\.([^.]+)$/) || [])[1] || "";
|
|
169
242
|
let fileType = mineTypes[dotName]; // 文件类型
|
|
170
243
|
let fileInfo = statSync(filePath);
|
|
@@ -183,7 +256,8 @@ module.exports = function (config) {
|
|
|
183
256
|
|
|
184
257
|
// 与if-none-match配合使用,做缓存验证
|
|
185
258
|
// ETag 包含 gzip 标志,确保 gzip 配置变化时缓存正确失效
|
|
186
|
-
|
|
259
|
+
// 同时包含 CJS 门面标志,避免门面与原文件共用缓存
|
|
260
|
+
'ETag': '"' + fileInfo.mtime.getTime() + (gzip ? '-gz' : '') + (isCjsShim ? '-cjs' : '') + '"',
|
|
187
261
|
|
|
188
262
|
// 告知代理/缓存:响应内容会根据 Accept-Encoding 不同而变化
|
|
189
263
|
'Vary': 'Accept-Encoding'
|
|
@@ -207,7 +281,7 @@ module.exports = function (config) {
|
|
|
207
281
|
if (lastModified === ifModifiedSince && etagMatch) {
|
|
208
282
|
response.writeHead(304, responseHeader);
|
|
209
283
|
response.end();
|
|
210
|
-
console.log("
|
|
284
|
+
console.log("\x1b[1m\x1b[32m[" + name + "] Cache File: " + url + "\x1b[0m " + new Date().toLocaleString() + " \x1b[33m\x1b[1m304\x1b[0m" + (isXHR ? " \x1b[33m\x1b[1m" + request.method + "\x1b[0m" : "") + " " + rawIp);
|
|
211
285
|
return;
|
|
212
286
|
}
|
|
213
287
|
}
|
|
@@ -219,38 +293,17 @@ module.exports = function (config) {
|
|
|
219
293
|
let acceptEncoding = headers["accept-encoding"] || "";
|
|
220
294
|
let shouldGzip = gzip && /\bgzip\b/.test(acceptEncoding) && compressibleTypes.has(fileType);
|
|
221
295
|
|
|
222
|
-
//
|
|
223
|
-
//
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
// 只处理非下载文件
|
|
228
|
-
// 过大的也不进行处理
|
|
229
|
-
// (对website无效)
|
|
230
|
-
if (urlArray[1] !== "download") {
|
|
231
|
-
let loaders = isWebsite ? websiteLoader : config.module;
|
|
232
|
-
|
|
233
|
-
for (let i = 0; i < loaders.rules.length; i++) {
|
|
234
|
-
if (loaders.rules[i].test.test(filePath)) {
|
|
235
|
-
source = loaders.rules[i].use.call({
|
|
236
|
-
root: basePath, // 服务器根路径
|
|
237
|
-
path: filePath.replace(basePath, ""), // 文件相对路径
|
|
238
|
-
entry, // 是否是浏览器地址栏直接访问
|
|
239
|
-
setFileType(_fileType) { // 设置文件类型
|
|
240
|
-
fileType = _fileType;
|
|
241
|
-
responseHeader['Content-Type'] = buildContentType(_fileType);
|
|
242
|
-
}
|
|
243
|
-
}, source);
|
|
244
|
-
break;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
296
|
+
// CommonJS / UMD 门面:把 module.exports 包成 ESM 的默认导出
|
|
297
|
+
// 这样 `import { x } from "pkg"` 在改写后可以从默认导出上解构
|
|
298
|
+
// 2026年11月25日 于南京
|
|
299
|
+
if (isCjsShim) {
|
|
300
|
+
sendType = "Read";
|
|
247
301
|
|
|
248
|
-
|
|
302
|
+
let code = readFileSync(filePath, "utf8");
|
|
303
|
+
code = applyDefine(code);
|
|
249
304
|
|
|
250
|
-
|
|
251
|
-
let body = isXHR ? source : resolveImport(source, fileType !== "application/javascript");
|
|
305
|
+
let body = "let module = { exports: {} };\nlet exports = module.exports;\n" + code + "\nexport default module.exports;\n";
|
|
252
306
|
|
|
253
|
-
// gzip 压缩后发送(需要重新判断 fileType,因为 loader 可能修改了它)
|
|
254
307
|
if (shouldGzip && compressibleTypes.has(fileType)) {
|
|
255
308
|
responseHeader['Content-Encoding'] = 'gzip';
|
|
256
309
|
body = gzipSync(Buffer.from(body));
|
|
@@ -259,26 +312,110 @@ module.exports = function (config) {
|
|
|
259
312
|
response.writeHead(200, responseHeader);
|
|
260
313
|
response.write(body);
|
|
261
314
|
response.end();
|
|
315
|
+
|
|
316
|
+
console.log("\x1b[1m\x1b[32m[" + name + "] " + sendType + " CJS File: " + url + '\x1b[0m ' + new Date().toLocaleString() + (shouldGzip ? " \x1b[33m\x1b[1mgzip\x1b[0m" : "") + " " + rawIp);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// 如果文件小于1M且不是二进制,认为不大,直接读取
|
|
321
|
+
// 如果是二进制文件,即使小于10M也使用流(避免不必要的加载器处理和import解析)
|
|
322
|
+
if (fileInfo.size < 10 * 1024 * 1024 && !isBinary(filePath)) {
|
|
323
|
+
const isDownload = query.split("&").includes("download");
|
|
324
|
+
|
|
325
|
+
try {
|
|
326
|
+
let { source, resolveImport } = resolveImportFactory(basePath, filePath, entry, isWebsite ? websiteIntercept : config.devServer.intercept, isDownload, isWebsite)
|
|
327
|
+
|
|
328
|
+
// 只处理非下载文件
|
|
329
|
+
// 过大的也不进行处理
|
|
330
|
+
// (对website无效)
|
|
331
|
+
if (!isDownload) {
|
|
332
|
+
let loaders = isWebsite ? websiteLoader : config.module;
|
|
333
|
+
|
|
334
|
+
for (let i = 0; i < loaders.rules.length; i++) {
|
|
335
|
+
if (loaders.rules[i].test.test(filePath)) {
|
|
336
|
+
source = loaders.rules[i].use.call({
|
|
337
|
+
root: basePath, // 服务器根路径
|
|
338
|
+
path: filePath.replace(basePath, ""), // 文件相对路径
|
|
339
|
+
entry, // 是否是浏览器地址栏直接访问
|
|
340
|
+
setFileType(_fileType) { // 设置文件类型
|
|
341
|
+
fileType = _fileType;
|
|
342
|
+
responseHeader['Content-Type'] = buildContentType(_fileType);
|
|
343
|
+
}
|
|
344
|
+
}, source);
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// 类似 DefinePlugin 的替换,只对 js 文本生效
|
|
352
|
+
if (fileType === "application/javascript") {
|
|
353
|
+
source = applyDefine(source + "");
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
sendType = "Read";
|
|
357
|
+
let body = isXHR ? source : resolveImport(source, fileType !== "application/javascript");
|
|
358
|
+
|
|
359
|
+
// gzip 压缩后发送(需要重新判断 fileType,因为 loader 可能修改了它)
|
|
360
|
+
if (shouldGzip && compressibleTypes.has(fileType)) {
|
|
361
|
+
responseHeader['Content-Encoding'] = 'gzip';
|
|
362
|
+
body = gzipSync(Buffer.from(body));
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
response.writeHead(200, responseHeader);
|
|
366
|
+
response.write(body);
|
|
367
|
+
response.end();
|
|
368
|
+
} catch (error) {
|
|
369
|
+
// 解析失败时给出可读的 500,而不是让客户端一直等待
|
|
370
|
+
// 2026年11月25日 于南京
|
|
371
|
+
response.writeHead(500, {
|
|
372
|
+
'Content-Type': buildContentType("text/plain"),
|
|
373
|
+
'Access-Control-Allow-Origin': '*',
|
|
374
|
+
'Server': 'Powered by ' + name + '@' + version
|
|
375
|
+
});
|
|
376
|
+
response.end("Module resolve error: " + (error && error.message || error));
|
|
377
|
+
console.log("\x1b[1m\x1b[31m[" + name + "] resolve error: " + url + " -> " + (error && error.message || error) + "\x1b[0m " + new Date().toLocaleString() + " " + rawIp);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
262
380
|
}
|
|
263
381
|
|
|
264
382
|
// 对于大文件,使用流读取
|
|
265
383
|
else {
|
|
266
384
|
sendType = "Stream";
|
|
267
385
|
|
|
386
|
+
const stream = createReadStream(filePath);
|
|
387
|
+
|
|
388
|
+
// 读流出错(文件被删除、符号链接指向目录等)时结束响应,避免客户端悬挂
|
|
389
|
+
// 2026年11月25日 于南京
|
|
390
|
+
stream.on("error", error => {
|
|
391
|
+
console.log("\x1b[1m\x1b[31m[" + name + "] stream error: " + url + " -> " + error.message + "\x1b[0m");
|
|
392
|
+
|
|
393
|
+
if (!response.headersSent) {
|
|
394
|
+
response.writeHead(500, {
|
|
395
|
+
'Content-Type': buildContentType("text/plain"),
|
|
396
|
+
'Access-Control-Allow-Origin': '*',
|
|
397
|
+
'Server': 'Powered by ' + name + '@' + version
|
|
398
|
+
});
|
|
399
|
+
response.end("Stream error: " + error.message);
|
|
400
|
+
} else {
|
|
401
|
+
response.destroy();
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
|
|
268
405
|
if (shouldGzip) {
|
|
269
406
|
// gzip 流式压缩:文件大小变化,不能设置 Content-Length
|
|
270
407
|
responseHeader['Content-Encoding'] = 'gzip';
|
|
271
408
|
response.writeHead(200, responseHeader);
|
|
272
|
-
|
|
409
|
+
stream.pipe(createGzip()).pipe(response);
|
|
273
410
|
} else {
|
|
274
411
|
responseHeader['Content-Length'] = fileInfo.size
|
|
275
412
|
|
|
276
413
|
response.writeHead(200, responseHeader);
|
|
277
|
-
|
|
414
|
+
stream.pipe(response);
|
|
278
415
|
}
|
|
279
416
|
}
|
|
280
417
|
|
|
281
|
-
console.log("
|
|
418
|
+
console.log("\x1b[1m\x1b[32m[" + name + "] " + sendType + " File: " + url + '\x1b[0m ' + new Date().toLocaleString() + (shouldGzip ? " \x1b[33m\x1b[1mgzip\x1b[0m" : "") + (isXHR ? " \x1b[33m\x1b[1m" + request.method + "\x1b[0m" : "") + " " + rawIp);
|
|
282
419
|
}
|
|
283
420
|
|
|
284
421
|
// 否则就是404
|
|
@@ -301,19 +438,19 @@ module.exports = function (config) {
|
|
|
301
438
|
let networkValue = network();
|
|
302
439
|
|
|
303
440
|
// 打印成功提示
|
|
304
|
-
console.log('
|
|
305
|
-
console.log('
|
|
306
|
-
for (let ipv4Item of networkValue.IPv4) console.log('
|
|
307
|
-
for (let ipv6Item of networkValue.IPv6) console.log('
|
|
308
|
-
console.log('
|
|
441
|
+
console.log('\x1b[1m\x1b[32m[' + name + '] Project is running at:\x1b[0m');
|
|
442
|
+
console.log('\x1b[1m\x1b[32m[' + name + '] Loopback: \x1b[36mhttp://localhost:' + port + '/\x1b[0m');
|
|
443
|
+
for (let ipv4Item of networkValue.IPv4) console.log('\x1b[1m\x1b[32m[' + name + '] On Your Network (IPv4):\x1b[36m http://' + ipv4Item.address + ':' + port + '/\x1b[0m');
|
|
444
|
+
for (let ipv6Item of networkValue.IPv6) console.log('\x1b[1m\x1b[32m[' + name + '] On Your Network (IPv6): \x1b[36mhttp://[' + ipv6Item.address + ']:' + port + '/\x1b[0m');
|
|
445
|
+
console.log('\x1b[1m\x1b[32m[' + name + '] Content not from ' + (config.name || "OIPage") + ' is served from \x1b[36m"' + basePath + '" \x1b[0mdirectory');
|
|
309
446
|
|
|
310
447
|
if (typeof config.devServer.open === "string" || config.devServer.open) {
|
|
311
448
|
let url = 'http://localhost:' + port + "/" + (typeof config.devServer.open === "string" ? config.devServer.open : "");
|
|
312
449
|
openBrowser(url);
|
|
313
|
-
console.log('
|
|
450
|
+
console.log('\x1b[1m\x1b[32m[' + name + '] Open: \x1b[36m' + url + '\x1b[0m');
|
|
314
451
|
}
|
|
315
452
|
|
|
316
|
-
if (!cache) console.log('
|
|
317
|
-
if (gzip) console.log('
|
|
453
|
+
if (!cache) console.log('\x1b[1m\x1b[32m[' + name + '] Cancel 304 Cache!\x1b[0m');
|
|
454
|
+
if (gzip) console.log('\x1b[1m\x1b[32m[' + name + '] Gzip Compression: \x1b[36menabled\x1b[0m');
|
|
318
455
|
console.log('\n' + (config.name || "OIPage") + ' ' + version + ' compiled \x1b[1m\x1b[32msuccessfully\x1b[0m in ' + (new Date().valueOf() - startTime) + ' ms\n');
|
|
319
456
|
};
|