@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
|
@@ -1,75 +1,250 @@
|
|
|
1
|
-
const { readFileSync,
|
|
2
|
-
const { join } = require("path");
|
|
1
|
+
const { readFileSync, statSync } = require("fs");
|
|
2
|
+
const { join, resolve } = require("path");
|
|
3
3
|
const { testIntercept } = require("../intercept.js");
|
|
4
|
+
const { scanSites } = require("./moduleForm.js");
|
|
5
|
+
const resolvePackage = require("./resolvePackage.js");
|
|
4
6
|
|
|
7
|
+
// 允许并保持原样的协议/内置模块前缀
|
|
8
|
+
const EXTERNAL_PROTOCOL = /^(node:|http:|https:|data:|blob:|file:|ws:|wss:)/;
|
|
9
|
+
|
|
10
|
+
// 已经打印过告警的说明符,避免刷屏
|
|
11
|
+
const warned = new Set();
|
|
12
|
+
|
|
13
|
+
let defaultOptions = {
|
|
14
|
+
conditions: ["browser", "import", "default"]
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 配置解析选项(由 serve 在启动时调用)
|
|
19
|
+
* @param {object} options 选项
|
|
20
|
+
*/
|
|
21
|
+
function configure(options) {
|
|
22
|
+
defaultOptions = Object.assign({}, defaultOptions, options || {});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 是否是可解析的裸包说明符
|
|
27
|
+
*/
|
|
28
|
+
function isBareSpecifier(specifier) {
|
|
29
|
+
if (!specifier) return false;
|
|
30
|
+
if (specifier.startsWith(".") || specifier.startsWith("/")) return false;
|
|
31
|
+
if (EXTERNAL_PROTOCOL.test(specifier)) return false;
|
|
32
|
+
if (/\s/.test(specifier)) return false;
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function warnOnce(message) {
|
|
37
|
+
if (warned.has(message)) return;
|
|
38
|
+
warned.add(message);
|
|
39
|
+
console.log("\x1b[1m\x1b[33m[resolveImport] " + message + "\x1b[0m");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 统计文本里的换行数量,替换整条语句时用来补齐行号
|
|
44
|
+
*/
|
|
45
|
+
function countNewlines(text) {
|
|
46
|
+
let count = 0;
|
|
47
|
+
for (let i = 0; i < text.length; i++) if (text[i] === "\n") count++;
|
|
48
|
+
return count;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 给模块 URL 追加查询标记
|
|
53
|
+
* oipage=cli 表示这份文件来自命令行工具自带的依赖,服务端按标记优先去 CLI 目录取
|
|
54
|
+
* oipage=cjs 表示需要包一层 CommonJS 门面
|
|
55
|
+
* @param {string} url 模块地址
|
|
56
|
+
* @param {object} resolved 解析结果
|
|
57
|
+
* @param {boolean} cjs 是否需要门面
|
|
58
|
+
*/
|
|
59
|
+
function withMarkers(url, resolved, cjs) {
|
|
60
|
+
const markers = [];
|
|
61
|
+
|
|
62
|
+
if (resolved.fromCli) markers.push("oipage=cli");
|
|
63
|
+
if (cjs) markers.push("oipage=cjs");
|
|
64
|
+
|
|
65
|
+
if (!markers.length) return url;
|
|
66
|
+
|
|
67
|
+
return url + (url.includes("?") ? "&" : "?") + markers.join("&");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 生成 CommonJS / UMD 的引用点改写代码
|
|
72
|
+
*
|
|
73
|
+
* 核心思路:目标模块不是 ESM 时,先以默认导入拿到 module.exports,
|
|
74
|
+
* 再把原本的命名导入改写成解构/属性取值(和打包器的做法一致)。
|
|
75
|
+
* @returns {{start: number, end: number, text: string}|null}
|
|
76
|
+
*/
|
|
77
|
+
function buildCjsReplacement(content, site, shimUrl, id) {
|
|
78
|
+
const valueName = "__oipage_cjs" + id;
|
|
79
|
+
const quote = content[site.specStart];
|
|
80
|
+
const shimLiteral = quote + shimUrl + quote;
|
|
81
|
+
|
|
82
|
+
// 动态导入只能换地址,模块对象形如 { default: exports }
|
|
83
|
+
if (site.kind === "dynamic") {
|
|
84
|
+
return { start: site.specStart, end: site.specEnd, text: shimLiteral };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// export * from 无法枚举,保持原样并告警
|
|
88
|
+
if (site.kind === "export-star" && !site.starAs) {
|
|
89
|
+
warnOnce("\"export * from " + site.specifier + "\" 无法自动展开 CommonJS 导出,请改用具名导出");
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let code = "";
|
|
94
|
+
|
|
95
|
+
if (site.kind === "import") {
|
|
96
|
+
code = "import " + valueName + " from " + shimLiteral + ";";
|
|
97
|
+
|
|
98
|
+
if (site.form === "clause") {
|
|
99
|
+
const bindings = site.bindings || { defaults: [], named: [], ns: [] };
|
|
100
|
+
|
|
101
|
+
if (bindings.named.length) {
|
|
102
|
+
const parts = bindings.named
|
|
103
|
+
.map(item => item.alias ? item.name + ": " + item.alias : item.name)
|
|
104
|
+
.join(", ");
|
|
105
|
+
code += "const {" + parts + "} = " + valueName + ";";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
for (const name of bindings.defaults) code += "const " + name + " = " + valueName + ";";
|
|
109
|
+
for (const name of bindings.ns) code += "const " + name + " = " + valueName + ";";
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
} else if (site.kind === "export-named") {
|
|
113
|
+
code = "import " + valueName + " from " + shimLiteral + ";";
|
|
114
|
+
|
|
115
|
+
for (const pair of site.pairs) {
|
|
116
|
+
const exportName = pair.alias || pair.name;
|
|
117
|
+
|
|
118
|
+
if (pair.name === "default" && !pair.alias) {
|
|
119
|
+
code += "export default " + valueName + ";";
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(exportName)) {
|
|
124
|
+
warnOnce("\"" + site.specifier + "\" 的导出名 " + exportName + " 不是合法标识符,已跳过");
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
code += "export const " + exportName + " = " + valueName + "[" + JSON.stringify(pair.name) + "];";
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
} else if (site.kind === "export-star") {
|
|
132
|
+
if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(site.starAs)) {
|
|
133
|
+
warnOnce("\"" + site.specifier + "\" 的命名空间导出名不合法,已跳过");
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
code = "import " + valueName + " from " + shimLiteral + ";export const " + site.starAs + " = " + valueName + ";";
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (!code) return null;
|
|
141
|
+
|
|
142
|
+
return { start: site.stmtStart, end: site.stmtEnd, text: code };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 导入解析工厂
|
|
147
|
+
* @param {string} basePath 服务器根目录
|
|
148
|
+
* @param {string} filePath 当前文件
|
|
149
|
+
* @param {boolean} entry 是否是浏览器地址栏直接访问
|
|
150
|
+
* @param {Array} intercept 拦截规则
|
|
151
|
+
* @param {boolean} isDownload 是否下载
|
|
152
|
+
* @param {boolean} isWebsite 是否是内置网站
|
|
153
|
+
*/
|
|
5
154
|
module.exports = function (basePath, filePath, entry, intercept, isDownload, isWebsite) {
|
|
6
|
-
basePath =
|
|
155
|
+
basePath = resolve(basePath, "./");
|
|
156
|
+
|
|
157
|
+
if (statSync(filePath).isDirectory()) {
|
|
158
|
+
return { source: "", resolveImport: content => content };
|
|
159
|
+
}
|
|
7
160
|
|
|
8
|
-
if (statSync(filePath).isDirectory()) { return { source: "", resolveImport: content => content }; }
|
|
9
161
|
let source = readFileSync(filePath);
|
|
10
162
|
let resolveImport = content => content;
|
|
11
163
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (node_modulesRootPath === basePath) {
|
|
47
|
-
|
|
48
|
-
// 如果命令行根目录是一个项目
|
|
49
|
-
let packagePath = join(basePath, "./package.json");
|
|
50
|
-
|
|
51
|
-
// statSync代替lstatSync,修复符号链接指向文件夹时无法正确判断文件
|
|
52
|
-
// 2026年09月10日 于南宁
|
|
53
|
-
if (existsSync(packagePath) && !statSync(packagePath).isDirectory()) {
|
|
54
|
-
let bundlePackage = require(packagePath);
|
|
55
|
-
if (!/\//.test(_importUrl)) {
|
|
56
|
-
return "/" + bundlePackage.main;
|
|
57
|
-
} else {
|
|
58
|
-
return "/"
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return "/@modules/" + npmName;
|
|
63
|
-
} else {
|
|
64
|
-
node_modulesRootPath = join(node_modulesRootPath, "../");
|
|
65
|
-
prePath = "../" + prePath;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
}));
|
|
164
|
+
const context = {
|
|
165
|
+
basePath,
|
|
166
|
+
conditions: defaultOptions.conditions,
|
|
167
|
+
// 内置网站固定使用命令行工具自带的依赖,避免被工程里装的同名包顶掉
|
|
168
|
+
preferCli: !!isWebsite
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
let counter = 0;
|
|
172
|
+
|
|
173
|
+
let __resolveImport = function (rawContent) {
|
|
174
|
+
let content = rawContent + "";
|
|
175
|
+
|
|
176
|
+
const sites = scanSites(content).sort((a, b) => a.stmtStart - b.stmtStart);
|
|
177
|
+
const replacements = [];
|
|
178
|
+
|
|
179
|
+
for (const site of sites) {
|
|
180
|
+
const specifier = site.specifier;
|
|
181
|
+
|
|
182
|
+
if (!isBareSpecifier(specifier)) continue;
|
|
183
|
+
|
|
184
|
+
const packageName = specifier.startsWith("@")
|
|
185
|
+
? specifier.split("/").slice(0, 2).join("/")
|
|
186
|
+
: specifier.split("/")[0];
|
|
187
|
+
|
|
188
|
+
// 命中了拦截规则:交给拦截器自己提供内容,这里只改地址
|
|
189
|
+
if (testIntercept(packageName, intercept)) {
|
|
190
|
+
const url = (isWebsite ? "/@website/@modules/" : "/@modules/") + packageName;
|
|
191
|
+
const quote = content[site.specStart];
|
|
192
|
+
replacements.push({
|
|
193
|
+
start: site.specStart,
|
|
194
|
+
end: site.specEnd,
|
|
195
|
+
text: quote + url + quote
|
|
196
|
+
});
|
|
197
|
+
continue;
|
|
71
198
|
}
|
|
72
|
-
|
|
199
|
+
|
|
200
|
+
const resolved = resolvePackage.resolveSpecifier(specifier, context);
|
|
201
|
+
|
|
202
|
+
if (!resolved || !resolved.url) {
|
|
203
|
+
warnOnce("\"" + specifier + "\" 解析失败(" + resolvePackage.explain(specifier, context) + ")");
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
counter++;
|
|
208
|
+
|
|
209
|
+
// ESM 目标:只换地址
|
|
210
|
+
if (resolved.form === "esm") {
|
|
211
|
+
const quote = content[site.specStart];
|
|
212
|
+
replacements.push({
|
|
213
|
+
start: site.specStart,
|
|
214
|
+
end: site.specEnd,
|
|
215
|
+
text: quote + withMarkers(resolved.url, resolved, false) + quote
|
|
216
|
+
});
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// CommonJS / UMD 目标:换成默认导入 + 解构
|
|
221
|
+
const shimUrl = withMarkers(resolved.url, resolved, true);
|
|
222
|
+
const replacement = buildCjsReplacement(content, site, shimUrl, counter);
|
|
223
|
+
|
|
224
|
+
if (!replacement) continue;
|
|
225
|
+
|
|
226
|
+
// 原语句自带的分号会被保留,这里去掉重复的一个
|
|
227
|
+
if (content[replacement.end] === ";") {
|
|
228
|
+
replacement.text = replacement.text.replace(/;$/, "");
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// 整条语句被压缩成一行,补齐换行保证后续行号不变
|
|
232
|
+
const removed = countNewlines(content.slice(replacement.start, replacement.end));
|
|
233
|
+
replacements.push({
|
|
234
|
+
start: replacement.start,
|
|
235
|
+
end: replacement.end,
|
|
236
|
+
text: replacement.text + "\n".repeat(removed)
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// 从后往前替换,避免偏移错乱
|
|
241
|
+
replacements.sort((a, b) => b.start - a.start);
|
|
242
|
+
|
|
243
|
+
for (const item of replacements) {
|
|
244
|
+
content = content.slice(0, item.start) + item.text + content.slice(item.end);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return content;
|
|
73
248
|
};
|
|
74
249
|
|
|
75
250
|
// 如果是下载
|
|
@@ -85,8 +260,12 @@ module.exports = function (basePath, filePath, entry, intercept, isDownload, isW
|
|
|
85
260
|
|
|
86
261
|
// 如果是.html或.htm结尾
|
|
87
262
|
else if (/\.html{0,1}$/.test(filePath)) {
|
|
88
|
-
source = (source + "").replace(/<script
|
|
89
|
-
|
|
263
|
+
source = (source + "").replace(/<script\b([^>]*)>([\s\S]*?)<\/script>/gi, function (all, attrs, body) {
|
|
264
|
+
// 只处理内联的 type="module" 脚本
|
|
265
|
+
if (!/type\s*=\s*["']module["']/i.test(attrs)) return all;
|
|
266
|
+
if (/\bsrc\s*=/.test(attrs)) return all;
|
|
267
|
+
|
|
268
|
+
return all.replace(body, __resolveImport(body));
|
|
90
269
|
});
|
|
91
270
|
}
|
|
92
271
|
|
|
@@ -94,4 +273,6 @@ module.exports = function (basePath, filePath, entry, intercept, isDownload, isW
|
|
|
94
273
|
source,
|
|
95
274
|
resolveImport
|
|
96
275
|
};
|
|
97
|
-
};
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
module.exports.configure = configure;
|