@oipage/cli 0.3.0 → 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.
Files changed (92) hide show
  1. package/.github/FUNDING.yml +11 -11
  2. package/AUTHORS.txt +6 -6
  3. package/CHANGELOG +76 -47
  4. package/LICENSE +20 -20
  5. package/README.md +204 -132
  6. package/bin/WebSocket/decodeWsFrame.js +43 -43
  7. package/bin/WebSocket/encodeWsFrame.js +28 -28
  8. package/bin/WebSocket/headersToJSON.js +26 -26
  9. package/bin/WebSocket/index.js +106 -106
  10. package/bin/data/mineTypes.json +108 -104
  11. package/bin/disk.js +42 -42
  12. package/bin/help.js +63 -55
  13. package/bin/intercept.js +15 -15
  14. package/bin/network.js +21 -21
  15. package/bin/notebook/data.js +185 -0
  16. package/bin/notebook/index.js +168 -9
  17. package/bin/proxy.js +82 -82
  18. package/bin/run +146 -146
  19. package/bin/serve.js +455 -318
  20. package/bin/template/404.html +184 -184
  21. package/bin/tools/deleteEmptyFolder.js +33 -33
  22. package/bin/tools/format.js +75 -75
  23. package/bin/tools/isBinary.js +83 -83
  24. package/bin/tools/moduleForm.js +548 -0
  25. package/bin/tools/network.js +42 -42
  26. package/bin/tools/openBrowser.js +34 -34
  27. package/bin/tools/resolve404.js +98 -98
  28. package/bin/tools/resolveImport.js +278 -97
  29. package/bin/tools/resolvePackage.js +412 -0
  30. package/bin/website-htmls/components/ui-select-file/index.html +8 -8
  31. package/bin/website-htmls/components/ui-select-file/index.js +42 -42
  32. package/bin/website-htmls/components/ui-select-file/index.scss +44 -44
  33. package/bin/website-htmls/dialogs/imageSize/index.html +54 -54
  34. package/bin/website-htmls/dialogs/imageSize/index.js +53 -53
  35. package/bin/website-htmls/dialogs/imageSize/index.scss +140 -140
  36. package/bin/website-htmls/dialogs/index.js +52 -52
  37. package/bin/website-htmls/images/addFile.svg +1 -1
  38. package/bin/website-htmls/images/img-to-pdf.svg +1 -1
  39. package/bin/website-htmls/index.html +17 -17
  40. package/bin/website-htmls/main.js +13 -13
  41. package/bin/website-htmls/pages/App/index.html +6 -6
  42. package/bin/website-htmls/pages/App/index.js +22 -22
  43. package/bin/website-htmls/pages/App/index.scss +43 -43
  44. package/bin/website-htmls/pages/appStore/index.html +7 -7
  45. package/bin/website-htmls/pages/appStore/index.js +19 -19
  46. package/bin/website-htmls/pages/appStore/index.scss +54 -54
  47. package/bin/website-htmls/pages/chart/index.html +8 -8
  48. package/bin/website-htmls/pages/chart/index.js +72 -72
  49. package/bin/website-htmls/pages/chart/index.scss +71 -71
  50. package/bin/website-htmls/pages/home/index.html +19 -19
  51. package/bin/website-htmls/pages/home/index.js +19 -19
  52. package/bin/website-htmls/pages/home/index.scss +87 -87
  53. package/bin/website-htmls/pages/image-editor/index.html +27 -27
  54. package/bin/website-htmls/pages/image-editor/index.js +106 -106
  55. package/bin/website-htmls/pages/image-editor/index.scss +69 -69
  56. package/bin/website-htmls/pages/img-to-pdf/index.html +3 -3
  57. package/bin/website-htmls/pages/img-to-pdf/index.js +44 -44
  58. package/bin/website-htmls/pages/img-to-pdf/index.scss +9 -9
  59. package/bin/website-htmls/pages/recorder-screen/index.html +15 -15
  60. package/bin/website-htmls/pages/recorder-screen/index.js +227 -227
  61. package/bin/website-htmls/pages/recorder-screen/index.scss +52 -52
  62. package/bin/website-htmls/router.config.js +29 -29
  63. package/bin/website-htmls/styles/common.css +44 -44
  64. package/bin/website-htmls/styles/normalize.css +94 -94
  65. package/bin/website-plugins/intercept/chart.js +33 -33
  66. package/bin/website-plugins/intercept/getPackage.js +17 -17
  67. package/bin/website-plugins/intercept/head.js +8 -8
  68. package/bin/website-plugins/intercept/index.js +4 -8
  69. package/bin/website-plugins/loader/index.js +11 -11
  70. package/bin/website-plugins/loader/oipage-html-loader.js +7 -7
  71. package/bin/website-plugins/loader/oipage-scss-loader.js +149 -149
  72. package/docs/css/base.css +122 -0
  73. package/docs/css/components.css +180 -0
  74. package/docs/css/layout.css +295 -0
  75. package/docs/css/responsive.css +49 -0
  76. package/docs/images/file-list.webp +0 -0
  77. package/docs/images/logo-topbar.svg +5 -0
  78. package/docs/images/logo.png +0 -0
  79. package/docs/images/logo.svg +6 -0
  80. package/docs/images/manual.webp +0 -0
  81. package/docs/images/terminal-serve.webp +0 -0
  82. package/docs/index.html +1392 -0
  83. package/docs/js/copy.js +37 -0
  84. package/docs/js/nav.js +111 -0
  85. package/docs/js/search.js +241 -0
  86. package/docs/js/theme.js +17 -0
  87. package/package.json +42 -42
  88. package/src/index.js +26 -29
  89. package/types/serve.d.ts +83 -83
  90. package/bin/website-plugins/intercept/oipage-vislite-intercept.js +0 -56
  91. package/bin/website-plugins/intercept/oipage-zipaper-intercept.js +0 -37
  92. package/docs/config.md +0 -107
@@ -1,99 +1,99 @@
1
- const { readdirSync, lstatSync, readFileSync, statSync } = require("fs");
2
- const { join } = require("path");
3
- const { formatByte, formatTime } = require("./format");
4
- const mineTypes = require("../data/mineTypes.json");
5
-
6
- // HTML 实体转义,防止 XSS
7
- function escapeHtml(str) {
8
- return String(str)
9
- .replace(/&/g, "&")
10
- .replace(/</g, "&lt;")
11
- .replace(/>/g, "&gt;")
12
- .replace(/"/g, "&quot;")
13
- .replace(/'/g, "&#39;");
14
- }
15
-
16
- // URL 路径编码,防止 href 属性注入
17
- function encodePath(str) {
18
- return encodeURIComponent(str);
19
- }
20
-
21
- const img_folder = "data:image/png;base64," + readFileSync(join(__dirname, "../images/folder.png")).toString('base64');
22
- const img_file = "data:image/png;base64," + readFileSync(join(__dirname, "../images/file.png")).toString('base64');
23
-
24
- const template_404 = readFileSync(join(__dirname, "../template/404.html"), {
25
- encoding: "utf8"
26
- })
27
- .replace("${img_folder}", img_folder)
28
- .replace("${img_file}", img_file);
29
-
30
- module.exports = function (filePath, url, website, name, rawIp) {
31
-
32
- let subItems = [];
33
-
34
- try {
35
- subItems = readdirSync(filePath);
36
- console.log("<i> \x1b[1m\x1b[32m[" + name + "-http-server] Read Folder: " + url + '\x1b[0m ' + new Date().toLocaleString() + " " + rawIp);
37
- } catch (e) {
38
- console.log("<i> \x1b[1m\x1b[32m[" + name + "-http-server] Read " + (/\/$/.test(url) ? "Folder" : "File") + ": \x1b[35m" + url + ' 404 Not Found\x1b[0m ' + new Date().toLocaleString() + " " + rawIp);
39
- try {
40
- if (!/\/$/.test(url) || url === "/") {
41
- filePath = join(filePath, "../");
42
- subItems = readdirSync(filePath);
43
- }
44
- } catch (e) { }
45
- }
46
-
47
- let template = "";
48
- for (let i in subItems) {
49
- let isDirectory = lstatSync(join(filePath, subItems[i])).isDirectory();
50
- let statObj = statSync(join(filePath, subItems[i]));
51
-
52
- // 文件夹
53
- if (isDirectory) {
54
- template += `<tr class="folder">
55
- <th class="name">
56
- <a href='./${encodePath(subItems[i])}/'>${escapeHtml(subItems[i])}</a>
57
- </th>
58
- <th>
59
- ${formatTime(statObj.mtime)}
60
- </th>
61
- <th>
62
- 文件夹
63
- </th>
64
- <th>
65
- -
66
- </th>
67
- <th>
68
- <a href='./${encodePath(subItems[i])}/' class="btn">打开</a>
69
- </th>
70
- </tr>`;
71
- }
72
-
73
- // 文件
74
- else {
75
- let dotName = (subItems[i].match(/\.([^.]+)$/) || [])[1] || "";
76
-
77
- template += `<tr class="file">
78
- <th class="name">
79
- <a href='./${encodePath(subItems[i])}'>${escapeHtml(subItems[i])}</a>
80
- </th>
81
- <th>
82
- ${formatTime(statObj.mtime)}
83
- </th>
84
- <th>
85
- ${mineTypes[dotName] || "text/plain"}
86
- </th>
87
- <th>
88
- ${formatByte(statObj.size)}
89
- </th>
90
- <th>
91
- <a href='./${encodePath(subItems[i])}' class="btn">访问</a>
92
- <a href='./${encodePath(subItems[i])}?download' class="btn" download='${escapeHtml(subItems[i])}'>下载</a>
93
- </th>
94
- </tr>`;
95
- }
96
- }
97
-
98
- return template_404.replace("${website}", website ? '<a class="website-link" href="/@website/index.html">打开应用市场</a>' : '').replace("${current}", escapeHtml(filePath)).replace("${template}", template);
1
+ const { readdirSync, lstatSync, readFileSync, statSync } = require("fs");
2
+ const { join } = require("path");
3
+ const { formatByte, formatTime } = require("./format");
4
+ const mineTypes = require("../data/mineTypes.json");
5
+
6
+ // HTML 实体转义,防止 XSS
7
+ function escapeHtml(str) {
8
+ return String(str)
9
+ .replace(/&/g, "&amp;")
10
+ .replace(/</g, "&lt;")
11
+ .replace(/>/g, "&gt;")
12
+ .replace(/"/g, "&quot;")
13
+ .replace(/'/g, "&#39;");
14
+ }
15
+
16
+ // URL 路径编码,防止 href 属性注入
17
+ function encodePath(str) {
18
+ return encodeURIComponent(str);
19
+ }
20
+
21
+ const img_folder = "data:image/png;base64," + readFileSync(join(__dirname, "../images/folder.png")).toString('base64');
22
+ const img_file = "data:image/png;base64," + readFileSync(join(__dirname, "../images/file.png")).toString('base64');
23
+
24
+ const template_404 = readFileSync(join(__dirname, "../template/404.html"), {
25
+ encoding: "utf8"
26
+ })
27
+ .replace("${img_folder}", img_folder)
28
+ .replace("${img_file}", img_file);
29
+
30
+ module.exports = function (filePath, url, website, name, rawIp) {
31
+
32
+ let subItems = [];
33
+
34
+ try {
35
+ subItems = readdirSync(filePath);
36
+ console.log("\x1b[1m\x1b[32m[" + name + "-http-server] Read Folder: " + url + '\x1b[0m ' + new Date().toLocaleString() + " " + rawIp);
37
+ } catch (e) {
38
+ console.log("\x1b[1m\x1b[32m[" + name + "-http-server] Read " + (/\/$/.test(url) ? "Folder" : "File") + ": \x1b[35m" + url + ' 404 Not Found\x1b[0m ' + new Date().toLocaleString() + " " + rawIp);
39
+ try {
40
+ if (!/\/$/.test(url) || url === "/") {
41
+ filePath = join(filePath, "../");
42
+ subItems = readdirSync(filePath);
43
+ }
44
+ } catch (e) { }
45
+ }
46
+
47
+ let template = "";
48
+ for (let i in subItems) {
49
+ let isDirectory = lstatSync(join(filePath, subItems[i])).isDirectory();
50
+ let statObj = statSync(join(filePath, subItems[i]));
51
+
52
+ // 文件夹
53
+ if (isDirectory) {
54
+ template += `<tr class="folder">
55
+ <th class="name">
56
+ <a href='./${encodePath(subItems[i])}/'>${escapeHtml(subItems[i])}</a>
57
+ </th>
58
+ <th>
59
+ ${formatTime(statObj.mtime)}
60
+ </th>
61
+ <th>
62
+ 文件夹
63
+ </th>
64
+ <th>
65
+ -
66
+ </th>
67
+ <th>
68
+ <a href='./${encodePath(subItems[i])}/' class="btn">打开</a>
69
+ </th>
70
+ </tr>`;
71
+ }
72
+
73
+ // 文件
74
+ else {
75
+ let dotName = (subItems[i].match(/\.([^.]+)$/) || [])[1] || "";
76
+
77
+ template += `<tr class="file">
78
+ <th class="name">
79
+ <a href='./${encodePath(subItems[i])}'>${escapeHtml(subItems[i])}</a>
80
+ </th>
81
+ <th>
82
+ ${formatTime(statObj.mtime)}
83
+ </th>
84
+ <th>
85
+ ${mineTypes[dotName] || "text/plain"}
86
+ </th>
87
+ <th>
88
+ ${formatByte(statObj.size)}
89
+ </th>
90
+ <th>
91
+ <a href='./${encodePath(subItems[i])}' class="btn">访问</a>
92
+ <a href='./${encodePath(subItems[i])}?download' class="btn" download='${escapeHtml(subItems[i])}'>下载</a>
93
+ </th>
94
+ </tr>`;
95
+ }
96
+ }
97
+
98
+ return template_404.replace("${website}", website ? '<a class="website-link" href="/@website/index.html">打开应用市场</a>' : '').replace("${current}", escapeHtml(filePath)).replace("${template}", template);
99
99
  };
@@ -1,97 +1,278 @@
1
- const { readFileSync, existsSync, statSync } = require("fs");
2
- const { join } = require("path");
3
- const { testIntercept } = require("../intercept.js");
4
-
5
- module.exports = function (basePath, filePath, entry, intercept, isDownload, isWebsite) {
6
- basePath = join(basePath, "./");
7
-
8
- if (statSync(filePath).isDirectory()) { return { source: "", resolveImport: content => content }; }
9
- let source = readFileSync(filePath);
10
- let resolveImport = content => content;
11
-
12
- let __resolveImport = function (content) {
13
- return content.replace(/import [^'"]* from (['"])([^'"]*)['"]/sg, function (_importCode, _, _importUrl) {
14
- if (/^[./]/.test(_importUrl)) {
15
- return _importCode;
16
- } else {
17
- return _importCode.replace(_importUrl, _importUrl.replace(/((@[^/]+\/){0,1}[^/])+/s, function (npmName) {
18
-
19
- if (testIntercept(npmName, intercept)) {
20
- return (isWebsite ? "/@website/@modules/" : "/@modules/") + npmName;
21
- } else {
22
-
23
- let node_modulesRootPath = join(filePath, "../");
24
- let prePath = "";
25
- while (true) {
26
- let npmBundlePath = join(node_modulesRootPath, "./node_modules/", npmName);
27
-
28
- // 如果存在
29
- if (existsSync(npmBundlePath) && statSync(npmBundlePath).isDirectory()) {
30
- let npmNameValue = npmName;
31
-
32
- // 对于类似 import VISLite from "vislite"
33
- // 需要把包名解析成具体的文件
34
- if (!/\//.test(_importUrl
35
-
36
- // 兼容 "@oipage/browser.js" 类似的包
37
- // 2026年09月11日 于南宁
38
- .replace(/@[^/]+\//, ""))) {
39
- let bundlePackage = require(join(npmBundlePath, "./package.json"));
40
- npmNameValue = npmName + "/" + bundlePackage.main;
41
- }
42
-
43
- return (prePath ? prePath : "./") + "node_modules/" + npmNameValue;
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
- }));
71
- }
72
- });
73
- };
74
-
75
- // 如果是下载
76
- if (isDownload) {
77
- // todo
78
- }
79
-
80
- // 如果不是直接访问的
81
- else if (!entry) {
82
- source += "";
83
- resolveImport = (content, notResolve) => notResolve ? content : __resolveImport(content);
84
- }
85
-
86
- // 如果是.html或.htm结尾
87
- else if (/\.html{0,1}$/.test(filePath)) {
88
- source = (source + "").replace(/<script type="module">(.*)<\/script>/sg, function (_, _matchCode) {
89
- return `<script type="module">${__resolveImport(_matchCode)}</script>`;
90
- });
91
- }
92
-
93
- return {
94
- source,
95
- resolveImport
96
- };
97
- };
1
+ const { readFileSync, statSync } = require("fs");
2
+ const { join, resolve } = require("path");
3
+ const { testIntercept } = require("../intercept.js");
4
+ const { scanSites } = require("./moduleForm.js");
5
+ const resolvePackage = require("./resolvePackage.js");
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
+ */
154
+ module.exports = function (basePath, filePath, entry, intercept, isDownload, isWebsite) {
155
+ basePath = resolve(basePath, "./");
156
+
157
+ if (statSync(filePath).isDirectory()) {
158
+ return { source: "", resolveImport: content => content };
159
+ }
160
+
161
+ let source = readFileSync(filePath);
162
+ let resolveImport = content => content;
163
+
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;
198
+ }
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;
248
+ };
249
+
250
+ // 如果是下载
251
+ if (isDownload) {
252
+ // todo
253
+ }
254
+
255
+ // 如果不是直接访问的
256
+ else if (!entry) {
257
+ source += "";
258
+ resolveImport = (content, notResolve) => notResolve ? content : __resolveImport(content);
259
+ }
260
+
261
+ // 如果是.html或.htm结尾
262
+ else if (/\.html{0,1}$/.test(filePath)) {
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));
269
+ });
270
+ }
271
+
272
+ return {
273
+ source,
274
+ resolveImport
275
+ };
276
+ };
277
+
278
+ module.exports.configure = configure;