@morlay/dsh-client-modules 0.2.11-alpha.3 → 0.2.12
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/dist/client.cjs +2483 -1481
- package/dist/index.cjs +359 -115
- package/dist/index.mjs +361 -117
- package/package.json +8 -8
- package/src/__tests__/bundle.spec.ts +305 -60
- package/src/__tests__/client-meta.spec.ts +114 -0
- package/src/__tests__/resolve.spec.ts +20 -2
- package/src/bundle.ts +303 -141
- package/src/client-meta.ts +128 -0
- package/src/index.ts +8 -1
- package/src/resolve.ts +2 -1
- package/src/serve-vite.ts +6 -5
package/dist/index.cjs
CHANGED
|
@@ -8,6 +8,8 @@ let node_crypto = require("node:crypto");
|
|
|
8
8
|
let _morlay_dsh_cordis_assets_styles = require("@morlay/dsh-cordis-assets/styles");
|
|
9
9
|
let rolldown = require("rolldown");
|
|
10
10
|
let vite = require("vite");
|
|
11
|
+
let node_child_process = require("node:child_process");
|
|
12
|
+
let node_util = require("node:util");
|
|
11
13
|
// src/trace.ts
|
|
12
14
|
/**
|
|
13
15
|
* 诊断细节(逐包预编译、逐请求那一类)的具名 logger。
|
|
@@ -86,6 +88,122 @@ async function devExcluded(profileDir) {
|
|
|
86
88
|
function isAggregated(name, patterns) {
|
|
87
89
|
return patterns.some((pattern) => pattern.test(name));
|
|
88
90
|
}
|
|
91
|
+
// src/client-meta.ts
|
|
92
|
+
/**
|
|
93
|
+
* 浏览器构建里烘焙的版本元数据。
|
|
94
|
+
*
|
|
95
|
+
* 上游的 client 构建(`scripts/client-build-environment.ts`)把一组 `DSH_CLIENT_*` 值当 `define` 注入产物:
|
|
96
|
+
* - `DSH_CLIENT_VERSION`:设置页「当前版本」那一行;
|
|
97
|
+
* - `DSH_CLIENT_COMMIT_HASH` + `DSH_CLIENT_GIT_DIRTY`:侧栏显示的 `<commit>` / `<commit>-dirty`;
|
|
98
|
+
* - `DSH_CLIENT_BUILD_PROFILE`:`official` / `local`(官方品牌只在 official 下出现)。
|
|
99
|
+
*
|
|
100
|
+
* 这条链以前把 `process.env` 整个替换成 `{ env: {} }`,于是这些值全是 `undefined`:版本行不显示、
|
|
101
|
+
* `ui-settings-account` 还会因为 `DSH_CLIENT_VERSION` 缺失直接抛错。取值顺序(越靠前越权威):
|
|
102
|
+
*
|
|
103
|
+
* 1. 宿主进程的 `DSH_CLIENT_*`(`dshdev` 与桌面壳在起宿主时注入);
|
|
104
|
+
* 2. 仓库根的 `.dsh-build/client-build-environment.json`(`just prepare` 里的 `dshdev client-meta` 写);
|
|
105
|
+
* 3. 现算:仓库根的 `package.json` 版本 + `git rev-parse` / `git status`(dev 常见,代价是几次 git 调用)。
|
|
106
|
+
*/
|
|
107
|
+
/** `prepare` 写、构建读的那份记录(形状与上游 `client-build-environment.json` 一致)。 */
|
|
108
|
+
const CLIENT_BUILD_RECORD = (0, node_path.join)(".dsh-build", "client-build-environment.json");
|
|
109
|
+
const VERSION = "DSH_CLIENT_VERSION";
|
|
110
|
+
const COMMIT = "DSH_CLIENT_COMMIT_HASH";
|
|
111
|
+
const DIRTY = "DSH_CLIENT_GIT_DIRTY";
|
|
112
|
+
const PROFILE = "DSH_CLIENT_BUILD_PROFILE";
|
|
113
|
+
/** 环境里那组 `DSH_CLIENT_*` → 元数据。 */
|
|
114
|
+
function metaOfEnvironment(environment) {
|
|
115
|
+
const version = environment[VERSION];
|
|
116
|
+
const commit = environment[COMMIT];
|
|
117
|
+
const dirty = environment[DIRTY];
|
|
118
|
+
const profile = environment[PROFILE];
|
|
119
|
+
return {
|
|
120
|
+
...version === void 0 || version === "" ? {} : { version },
|
|
121
|
+
...commit === void 0 || commit === "" ? {} : { commit },
|
|
122
|
+
...dirty === "true" ? { dirty: true } : {},
|
|
123
|
+
...profile === void 0 || profile === "" ? {} : { profile }
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const runFile = (0, node_util.promisify)(node_child_process.execFile);
|
|
127
|
+
/** `git` 的输出;不在 git 工作树里、或 git 不可用时 undefined。 */
|
|
128
|
+
async function gitOutput(root, args) {
|
|
129
|
+
try {
|
|
130
|
+
const { stdout } = await runFile("git", args, {
|
|
131
|
+
cwd: root,
|
|
132
|
+
encoding: "utf8"
|
|
133
|
+
});
|
|
134
|
+
return stdout.trim();
|
|
135
|
+
} catch {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* 现算:**装的是哪一版 harness**(`DEEPSEEK_HARNESS_VERSION`,与同步链同一个真源)+ 本仓库的 git 元数据。
|
|
141
|
+
*
|
|
142
|
+
* 正常路径拿不到这里:`dshdev` 会注入环境、`prepare` 会写记录。只有"绕过 dshdev 直接构建 client 产物"
|
|
143
|
+
* 时才走到,所以版本只能取环境或仓库根的 `package.json`(后者不是权威值,缺 env 时用它兜底)。
|
|
144
|
+
*/
|
|
145
|
+
async function computeMeta(repoRoot) {
|
|
146
|
+
const explicit = process.env["DEEPSEEK_HARNESS_VERSION"];
|
|
147
|
+
let version = explicit === void 0 || explicit === "" ? void 0 : explicit;
|
|
148
|
+
if (version === void 0) {
|
|
149
|
+
const manifest = await (0, node_fs_promises.readFile)((0, node_path.join)(repoRoot, "package.json"), "utf8").catch(() => void 0);
|
|
150
|
+
if (manifest !== void 0) {
|
|
151
|
+
const parsed = JSON.parse(manifest);
|
|
152
|
+
if (typeof parsed.version === "string" && parsed.version !== "") version = parsed.version;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const commit = await gitOutput(repoRoot, [
|
|
156
|
+
"rev-parse",
|
|
157
|
+
"--short=7",
|
|
158
|
+
"HEAD"
|
|
159
|
+
]);
|
|
160
|
+
const status = await gitOutput(repoRoot, [
|
|
161
|
+
"status",
|
|
162
|
+
"--porcelain",
|
|
163
|
+
"--untracked-files=normal"
|
|
164
|
+
]);
|
|
165
|
+
return {
|
|
166
|
+
...version === void 0 ? {} : { version },
|
|
167
|
+
...commit === void 0 ? {} : { commit },
|
|
168
|
+
...status === void 0 || status === "" ? {} : { dirty: true }
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/** 读 `prepare` 写下的那份记录(缺失 / 坏掉都给 undefined,不挡构建)。 */
|
|
172
|
+
async function recordedMeta(repoRoot) {
|
|
173
|
+
const raw = await (0, node_fs_promises.readFile)((0, node_path.join)(repoRoot, CLIENT_BUILD_RECORD), "utf8").catch(() => void 0);
|
|
174
|
+
if (raw === void 0) return void 0;
|
|
175
|
+
try {
|
|
176
|
+
return metaOfEnvironment(JSON.parse(raw).environment ?? {});
|
|
177
|
+
} catch {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* 这个仓库在浏览器产物里该带的版本元数据。
|
|
183
|
+
* @param repoRoot - 仓库根(dev 是工作区,打包形态是 profile:那里通常读不到记录,靠宿主注入的环境)。
|
|
184
|
+
*/
|
|
185
|
+
async function clientBuildMeta(repoRoot) {
|
|
186
|
+
const inherited = metaOfEnvironment(process.env);
|
|
187
|
+
if (inherited.version !== void 0 && inherited.commit !== void 0) return inherited;
|
|
188
|
+
return {
|
|
189
|
+
...await recordedMeta(repoRoot) ?? await computeMeta(repoRoot),
|
|
190
|
+
...inherited
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* `define` 用的一组键:`process.env.DSH_CLIENT_*` → 字面量。
|
|
195
|
+
*
|
|
196
|
+
* `browserProcessPlugin` 会跳过这些键(见那里的负向前瞻),所以 define 这一层能把它们换成真实值;
|
|
197
|
+
* 其余 `process.env` 仍是空壳对象。
|
|
198
|
+
*/
|
|
199
|
+
function clientMetaDefines(meta) {
|
|
200
|
+
return {
|
|
201
|
+
[`process.env.${VERSION}`]: meta.version === void 0 ? "undefined" : JSON.stringify(meta.version),
|
|
202
|
+
[`process.env.${COMMIT}`]: meta.commit === void 0 ? "undefined" : JSON.stringify(meta.commit),
|
|
203
|
+
[`process.env.${DIRTY}`]: meta.dirty === true ? JSON.stringify("true") : "undefined",
|
|
204
|
+
[`process.env.${PROFILE}`]: meta.profile === void 0 ? "undefined" : JSON.stringify(meta.profile)
|
|
205
|
+
};
|
|
206
|
+
}
|
|
89
207
|
// src/resolve.ts
|
|
90
208
|
/** 路由前缀(沿用上游的 `/plugins`,页面上不改 URL 形态)。 */
|
|
91
209
|
const PLUGIN_ROUTE = "/plugins";
|
|
@@ -386,7 +504,7 @@ function clientModulesAlias(options) {
|
|
|
386
504
|
* 实例)。内容变了**不换 URL**,靠 `cache-control: no-cache` + `ETag` 协商(vite dev 对它自己的模块
|
|
387
505
|
* 也是这套)。
|
|
388
506
|
*/
|
|
389
|
-
const BUNDLER_REV = (0, node_crypto.createHash)("sha1").update(`client-modules:rules:
|
|
507
|
+
const BUNDLER_REV = (0, node_crypto.createHash)("sha1").update(`client-modules:rules:36`).digest("hex").slice(0, 8);
|
|
390
508
|
/** 包 → 它在浏览器里的地址(联邦式)。 */
|
|
391
509
|
function depsUrlOf(specifier, version, file = "index.js") {
|
|
392
510
|
const name = packageNameOf(specifier) || specifier;
|
|
@@ -513,8 +631,13 @@ function urlOf(specifier, tables) {
|
|
|
513
631
|
* **只替换 `process.env` 系列**:裸的 `process` 不能碰——它可能只是个普通标识符(`zod` 里就有一个叫
|
|
514
632
|
* `process` 的函数,替换掉直接把源码变成语法错误)。
|
|
515
633
|
*/
|
|
516
|
-
function browserProcessPlugin() {
|
|
517
|
-
const
|
|
634
|
+
function browserProcessPlugin(meta = {}) {
|
|
635
|
+
const metaDefines = Object.entries(clientMetaDefines(meta));
|
|
636
|
+
const replace = (code) => {
|
|
637
|
+
let next = code.replaceAll(/\bprocess\.env\.NODE_ENV\b/gu, JSON.stringify("development"));
|
|
638
|
+
for (const [key, value] of metaDefines) next = next.replaceAll(key, value);
|
|
639
|
+
return next.replaceAll(/\bprocess\.env\b/gu, "({ env: {} })");
|
|
640
|
+
};
|
|
518
641
|
return {
|
|
519
642
|
name: "dsh-browser-process",
|
|
520
643
|
enforce: "pre",
|
|
@@ -528,118 +651,205 @@ function browserProcessPlugin() {
|
|
|
528
651
|
};
|
|
529
652
|
}
|
|
530
653
|
/**
|
|
531
|
-
*
|
|
654
|
+
* `export const <名>` 用不了的键:JS 的保留字(`zod@4` 的 CJS 面有 `function` / `void` / `null`
|
|
655
|
+
* 这些键,原样拼出去就是 `SyntaxError`)。
|
|
656
|
+
*/
|
|
657
|
+
const RESERVED_EXPORT_NAMES = /* @__PURE__ */ new Set([
|
|
658
|
+
"await",
|
|
659
|
+
"break",
|
|
660
|
+
"case",
|
|
661
|
+
"catch",
|
|
662
|
+
"class",
|
|
663
|
+
"const",
|
|
664
|
+
"continue",
|
|
665
|
+
"debugger",
|
|
666
|
+
"default",
|
|
667
|
+
"delete",
|
|
668
|
+
"do",
|
|
669
|
+
"else",
|
|
670
|
+
"enum",
|
|
671
|
+
"eval",
|
|
672
|
+
"export",
|
|
673
|
+
"extends",
|
|
674
|
+
"false",
|
|
675
|
+
"finally",
|
|
676
|
+
"for",
|
|
677
|
+
"function",
|
|
678
|
+
"if",
|
|
679
|
+
"implements",
|
|
680
|
+
"import",
|
|
681
|
+
"in",
|
|
682
|
+
"instanceof",
|
|
683
|
+
"interface",
|
|
684
|
+
"let",
|
|
685
|
+
"new",
|
|
686
|
+
"null",
|
|
687
|
+
"package",
|
|
688
|
+
"private",
|
|
689
|
+
"protected",
|
|
690
|
+
"public",
|
|
691
|
+
"return",
|
|
692
|
+
"static",
|
|
693
|
+
"super",
|
|
694
|
+
"switch",
|
|
695
|
+
"this",
|
|
696
|
+
"throw",
|
|
697
|
+
"true",
|
|
698
|
+
"try",
|
|
699
|
+
"typeof",
|
|
700
|
+
"undefined",
|
|
701
|
+
"var",
|
|
702
|
+
"void",
|
|
703
|
+
"while",
|
|
704
|
+
"with",
|
|
705
|
+
"yield"
|
|
706
|
+
]);
|
|
707
|
+
/** ESM 的顶层语法节点:`import` / `export` 声明。 */
|
|
708
|
+
const ESM_STATEMENT_TYPES = /* @__PURE__ */ new Set([
|
|
709
|
+
"ImportDeclaration",
|
|
710
|
+
"ExportNamedDeclaration",
|
|
711
|
+
"ExportDefaultDeclaration",
|
|
712
|
+
"ExportAllDeclaration"
|
|
713
|
+
]);
|
|
714
|
+
/**
|
|
715
|
+
* `.js` 且没有 `type` 时,按 **Node 的语法检测**判:有 ESM 语法(顶层 `import`/`export`)就是 ESM,
|
|
716
|
+
* 否则 CJS(Node 23+ 默认开启 `--experimental-detect-module`)。
|
|
717
|
+
*
|
|
718
|
+
* 用 rolldown 自带的解析器(`rolldown/parseAst`),不自己写词法扫描。
|
|
719
|
+
*
|
|
720
|
+
* @param entry - `.js` 入口的绝对路径。
|
|
721
|
+
* @returns 是否含 ESM 语法。
|
|
722
|
+
*/
|
|
723
|
+
async function hasModuleSyntax(entry) {
|
|
724
|
+
try {
|
|
725
|
+
const [{ parseAst }, source] = await Promise.all([import("rolldown/parseAst"), (0, node_fs_promises.readFile)(entry, "utf8")]);
|
|
726
|
+
return parseAst(source, null, entry).body.some((node) => ESM_STATEMENT_TYPES.has(node.type));
|
|
727
|
+
} catch {
|
|
728
|
+
return false;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* 一个入口的模块类型:**扩展名 + 最近的 `package.json`**,不猜源码。
|
|
532
733
|
*
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
*
|
|
536
|
-
*
|
|
734
|
+
* | 入口 | 类型 |
|
|
735
|
+
* | --- | --- |
|
|
736
|
+
* | `.mjs` / `.mts` | `esm` |
|
|
737
|
+
* | `.cjs` / `.cts` | `cjs` |
|
|
738
|
+
* | `.js` + `type: "module"` | `esm` |
|
|
739
|
+
* | `.js` + `module` 字段指向它 | `esm`(打包器约定) |
|
|
740
|
+
* | 其余 `.js` | `cjs` |
|
|
741
|
+
* | `.ts` / `.tsx` / `.jsx` | `source`(转译面,原样) |
|
|
742
|
+
* | `.css` | `style` |
|
|
743
|
+
* | 其他(图片 / 字体 / `.json` …) | `asset` |
|
|
537
744
|
*
|
|
538
|
-
*
|
|
539
|
-
*
|
|
745
|
+
* 这是 `exports` 条件(`import` / `require`)选出入口之后的**第二步**:条件决定"哪个文件",这里决定
|
|
746
|
+
* "那个文件是什么模块"。只有 `cjs` 需要导出面适配,`esm` / `source` 一律原样。
|
|
747
|
+
*
|
|
748
|
+
* @param entry - 入口的绝对路径。
|
|
749
|
+
* @returns 模块类型。
|
|
540
750
|
*/
|
|
541
|
-
async function
|
|
542
|
-
|
|
543
|
-
if (
|
|
544
|
-
if (
|
|
751
|
+
async function moduleKindOf(entry) {
|
|
752
|
+
const extension = (0, node_path.extname)(entry).toLowerCase();
|
|
753
|
+
if (extension === ".css") return "style";
|
|
754
|
+
if (extension === ".mjs" || extension === ".mts") return "esm";
|
|
755
|
+
if (extension === ".cjs" || extension === ".cts") return "cjs";
|
|
756
|
+
if (extension === ".ts" || extension === ".tsx" || extension === ".jsx") return "source";
|
|
757
|
+
if (extension !== ".js") return "asset";
|
|
545
758
|
const manifestPath = await packageJsonNear(entry);
|
|
546
759
|
const manifest = JSON.parse(await (0, node_fs_promises.readFile)(manifestPath, "utf8").catch(() => "{}"));
|
|
547
|
-
if (manifest.type === "module") return
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
return (0, node_path.resolve)((0, node_path.dirname)(manifestPath), manifestModule) !== (0, node_path.resolve)(entry);
|
|
760
|
+
if (manifest.type === "module") return "esm";
|
|
761
|
+
if (manifest.type === "commonjs") return "cjs";
|
|
762
|
+
return await hasModuleSyntax(entry) ? "esm" : "cjs";
|
|
551
763
|
}
|
|
552
764
|
/**
|
|
553
|
-
* CJS 入口的**具名导出包装**(`react` 这类)。
|
|
765
|
+
* CJS 入口的**具名导出包装**(`react` / `@xterm/xterm` 这类)。
|
|
766
|
+
*
|
|
767
|
+
* rolldown 对 CJS 入口的产物的具名导出只能靠静态分析;`module.exports = require(…)` 与 UMD 包装它都分析
|
|
768
|
+
* 不出来,产物只剩 `export default …`——而浏览器侧要 `import { Fragment } from "react"`、`import { Terminal }
|
|
769
|
+
* from "@xterm/xterm"`,整个 import 阶段就失败。ESM 的导出必须**静态可枚举**,所以这里求值入口拿到它的键,
|
|
770
|
+
* 生成一份把它们写成 `export const` 的包装文件,拿包装文件当入口。
|
|
554
771
|
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
* 阶段就失败**(入口一行都执行不了)。ESM 的导出必须**静态可枚举**(不能 `export * from` 一个运行期对象),
|
|
558
|
-
* 所以这里求值入口拿到它的键,生成一份把它们写成 `export const` 的包装文件,拿包装文件当入口。
|
|
772
|
+
* 类型由 {@link moduleKindOf} 先按 **exports 落点 + 扩展名/type** 静态判定,只有 CJS 才进来;
|
|
773
|
+
* 这里求值一次只为**拿键**(`export const <键>` + `default = module.exports`)。ESM 原封不动。
|
|
559
774
|
*
|
|
560
|
-
* @returns 包装文件路径;入口不是 CJS
|
|
775
|
+
* @returns 包装文件路径;入口不是 node 能求值的 CJS、或没有可静态导出的键时 undefined(保持原样)。
|
|
561
776
|
*/
|
|
562
777
|
async function cjsShimOf(options) {
|
|
563
|
-
|
|
564
|
-
let keys;
|
|
778
|
+
let loaded;
|
|
565
779
|
try {
|
|
566
|
-
|
|
567
|
-
keys = Object.keys(loaded ?? {});
|
|
780
|
+
loaded = (0, node_module.createRequire)(options.entry)(options.entry);
|
|
568
781
|
} catch {
|
|
569
782
|
return;
|
|
570
783
|
}
|
|
571
|
-
const named = keys.filter((key) => key !== "default" && /^[A-Za-z_$][A-Za-z0-9_$]*$/u.test(key));
|
|
784
|
+
const named = Object.keys(loaded ?? {}).filter((key) => key !== "default" && /^[A-Za-z_$][A-Za-z0-9_$]*$/u.test(key) && !RESERVED_EXPORT_NAMES.has(key));
|
|
572
785
|
if (named.length === 0) return void 0;
|
|
573
|
-
const
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
`import mod from ${JSON.stringify(options.entry)};`,
|
|
786
|
+
const id = `\0dsh-cjs-shim:${options.name}:${options.exportName}`;
|
|
787
|
+
const code = [
|
|
788
|
+
`import mod from ${JSON.stringify(options.from)};`,
|
|
577
789
|
"const __cjs = mod ?? {};",
|
|
578
790
|
"export default __cjs;",
|
|
579
791
|
...named.map((key) => `export const ${key} = __cjs[${JSON.stringify(key)}];`),
|
|
580
792
|
""
|
|
581
|
-
].join("\n")
|
|
793
|
+
].join("\n");
|
|
582
794
|
options.log?.(`[bundle-cjs] ${options.name}/${options.exportName}: 补 ${String(named.length)} 个具名导出\n`);
|
|
583
|
-
return file;
|
|
584
|
-
}
|
|
585
|
-
/** 根出口的产物名(`urlOf` 对非 client 行写的就是它)。 */
|
|
586
|
-
const ROOT_EXPORT = "index";
|
|
587
|
-
/** 具名导出的合法标识符:补 default 时按它挑键(非标识符的导出名用不了简写)。 */
|
|
588
|
-
const EXPORT_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/u;
|
|
589
|
-
/**
|
|
590
|
-
* 这个入口是否需要「CJS 包的 ESM 便利入口」的 default 补偿。判据**只看 package.json 的声明**:
|
|
591
|
-
*
|
|
592
|
-
* - `type: "module"` 的包:Node 与我们都按 `exports`(或 `main` 本身就是 ESM)解析,两边一致,不补;
|
|
593
|
-
* - 其余是 CJS 包:`module` 只是打包器约定(视为 ESM),`main` 才是 CJS。这条链选 `module`(更小、没有
|
|
594
|
-
* webpack 垫片),而 Node 的 `import pkg from 'pkg'` 落到 `main`、default 是它的 `module.exports`——
|
|
595
|
-
* 所以浏览器侧得补一个 default,否则消费方那句 default import 直接报
|
|
596
|
-
* `does not provide an export named 'default'`(`rrule` 就是这一例)。
|
|
597
|
-
* - 有 `exports` 的包两边都按条件解析,不涉及这条约定;入口不是 `module` 的(选了 `main`)由
|
|
598
|
-
* {@link cjsShimOf} 处理。`main`/`module` 只描述根出口,子路径不在此列。
|
|
599
|
-
*/
|
|
600
|
-
async function needsCjsInterop(entry, exportName) {
|
|
601
|
-
if (exportName !== ROOT_EXPORT) return false;
|
|
602
|
-
const manifestPath = await packageJsonNear(entry);
|
|
603
|
-
const manifest = JSON.parse(await (0, node_fs_promises.readFile)(manifestPath, "utf8").catch(() => "{}"));
|
|
604
|
-
if (manifest.type === "module" || manifest.exports !== void 0) return false;
|
|
605
|
-
const manifestModule = manifest.module;
|
|
606
|
-
if (manifestModule === void 0 || manifest.main === void 0) return false;
|
|
607
|
-
const modulePath = (0, node_path.resolve)((0, node_path.dirname)(manifestPath), manifestModule);
|
|
608
|
-
return await (0, node_fs_promises.realpath)(modulePath).catch(() => modulePath) === await (0, node_fs_promises.realpath)(entry).catch(() => (0, node_path.resolve)(entry)) || modulePath === (0, node_path.resolve)(entry);
|
|
609
|
-
}
|
|
610
|
-
/**
|
|
611
|
-
* 给「CJS 包的 ESM 便利入口」补 default:**看产物的真实导出面**(`chunk.exports`),不猜源码。
|
|
612
|
-
* 已经有 default 的(`jsbi` 的 `export default JSBI`)原样保留——补成对象会把它顶掉,
|
|
613
|
-
* `@js-temporal/polyfill` 那句 `import e from "jsbi"` 于是拿到命名空间、运行期 `e.BigInt is not a function`;
|
|
614
|
-
* 没有的补成 `{ 具名导出… }`,与 CJS 的 `module.exports` 同形(`rrule` 的 `ALL_WEEKDAYS` / `rrulestr` 那批)。
|
|
615
|
-
*/
|
|
616
|
-
function cjsInteropPlugin(exportName) {
|
|
617
795
|
return {
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
for (const chunk of Object.values(bundle)) {
|
|
621
|
-
if (chunk.type !== "chunk" || chunk.isEntry !== true || chunk.name !== exportName) continue;
|
|
622
|
-
if (chunk.exports.includes("default")) continue;
|
|
623
|
-
const named = chunk.exports.filter((name) => EXPORT_IDENTIFIER.test(name));
|
|
624
|
-
if (named.length === 0) continue;
|
|
625
|
-
chunk.code += `\nexport default { ${named.join(", ")} };\n`;
|
|
626
|
-
}
|
|
627
|
-
}
|
|
796
|
+
id,
|
|
797
|
+
code
|
|
628
798
|
};
|
|
629
799
|
}
|
|
630
800
|
/**
|
|
631
|
-
* CSS
|
|
632
|
-
*
|
|
633
|
-
*
|
|
801
|
+
* CSS 出口的产物:**就是 CSS 文本**(不包装成 JS、不注入 `<style>`)。
|
|
802
|
+
*
|
|
803
|
+
* JS 里的 `import "x.css"` 是另一条路(`cssInlinePlugins` 在构建期内联成注入样式的模块),与本函数无关。
|
|
804
|
+
* 这里的 `url()` 指向包内资产:读到字节、按内容 hash 落到 `assets/`、改写成联邦地址——否则浏览器会按
|
|
805
|
+
* CSS 的相对路径去请求 `.pnpm` 里的文件,路由按清单服务就是 404。
|
|
806
|
+
*
|
|
807
|
+
* `lightningcss` 的 visitor 是同步回调,所以分两遍:第一遍只收集相对 `url()`,异步读资产/算 hash;
|
|
808
|
+
* 第二遍按映射改写(全程异步 IO)。
|
|
634
809
|
*
|
|
635
|
-
* @
|
|
810
|
+
* @param options - 入口文件、出口名、缓存目录、包键、产物清单。
|
|
636
811
|
*/
|
|
637
|
-
async function
|
|
638
|
-
|
|
639
|
-
const
|
|
812
|
+
async function bundleStyleOf(options) {
|
|
813
|
+
const { transform } = await import("lightningcss");
|
|
814
|
+
const source = await (0, node_fs_promises.readFile)(options.entry);
|
|
815
|
+
const relativeUrl = (ref) => ref !== "" && !ref.startsWith("#") && !ref.startsWith("/") && !/^[a-z][a-z0-9+.-]*:/iu.test(ref);
|
|
816
|
+
const refs = /* @__PURE__ */ new Set();
|
|
817
|
+
transform({
|
|
818
|
+
filename: options.entry,
|
|
819
|
+
code: source,
|
|
820
|
+
minify: false,
|
|
821
|
+
visitor: { Url(node) {
|
|
822
|
+
if (relativeUrl(node.url)) refs.add(node.url);
|
|
823
|
+
} }
|
|
824
|
+
});
|
|
825
|
+
const mapping = /* @__PURE__ */ new Map();
|
|
826
|
+
for (const ref of refs) {
|
|
827
|
+
const absolute = (0, node_path.join)((0, node_path.dirname)(options.entry), ref);
|
|
828
|
+
const bytes = await (0, node_fs_promises.readFile)(absolute).catch(() => void 0);
|
|
829
|
+
if (bytes === void 0) continue;
|
|
830
|
+
const extension = (0, node_path.extname)(absolute);
|
|
831
|
+
const name = `assets/${(0, node_path.basename)(absolute, extension)}-${(0, node_crypto.createHash)("sha256").update(bytes).digest("hex").slice(0, 8)}${extension}`;
|
|
832
|
+
mapping.set(ref, `${CLIENT_MODULES_PREFIX}/${options.key}/${name}`);
|
|
833
|
+
await (0, node_fs_promises.mkdir)((0, node_path.dirname)((0, node_path.join)(options.outDir, name)), { recursive: true });
|
|
834
|
+
await (0, node_fs_promises.writeFile)((0, node_path.join)(options.outDir, name), bytes);
|
|
835
|
+
options.files[name] = `${options.key}/${name}`;
|
|
836
|
+
}
|
|
837
|
+
const result = transform({
|
|
838
|
+
filename: options.entry,
|
|
839
|
+
code: source,
|
|
840
|
+
minify: false,
|
|
841
|
+
visitor: { Url(node) {
|
|
842
|
+
const rewritten = mapping.get(node.url);
|
|
843
|
+
return rewritten === void 0 ? void 0 : {
|
|
844
|
+
...node,
|
|
845
|
+
url: rewritten
|
|
846
|
+
};
|
|
847
|
+
} }
|
|
848
|
+
});
|
|
849
|
+
const file = (0, node_path.join)(options.outDir, options.exportName);
|
|
640
850
|
await (0, node_fs_promises.mkdir)((0, node_path.dirname)(file), { recursive: true });
|
|
641
|
-
await (0, node_fs_promises.writeFile)(file,
|
|
642
|
-
|
|
851
|
+
await (0, node_fs_promises.writeFile)(file, result.code);
|
|
852
|
+
options.files[options.exportName] = `${options.key}/${options.exportName}`;
|
|
643
853
|
}
|
|
644
854
|
/**
|
|
645
855
|
* 确保该包的产物在(指纹没变、且**这次要的出口**都在就直接用)。
|
|
@@ -724,7 +934,8 @@ async function bundlePackageOnce(options, key) {
|
|
|
724
934
|
const outDir = (0, node_path.join)(options.cacheRoot, key);
|
|
725
935
|
const manifestPath = (0, node_path.join)(outDir, ".files.json");
|
|
726
936
|
options.log?.(`[bundle-rev] ${options.name}: hash-start watch=${options.watch.join(",")}\n`);
|
|
727
|
-
const
|
|
937
|
+
const rootRev = (0, node_crypto.createHash)("sha1").update((0, node_path.dirname)(options.watch[0] ?? options.manifestOf ?? "")).digest("hex").slice(0, 8);
|
|
938
|
+
const rev = `${BUNDLER_REV}:${rootRev}:${await fingerprint(options.watch)}`;
|
|
728
939
|
options.log?.(`[bundle-rev] ${options.name}: hash=${rev}\n`);
|
|
729
940
|
const cached = JSON.parse(await (0, node_fs_promises.readFile)(manifestPath, "utf8").catch(() => "{}"));
|
|
730
941
|
const fresh = cached.rev === rev;
|
|
@@ -741,34 +952,63 @@ async function bundlePackageOnce(options, key) {
|
|
|
741
952
|
for (const { find, replacement } of options.alias) if (typeof find === "string" ? source.startsWith(find) : find.test(source)) return source.replace(find, replacement);
|
|
742
953
|
}
|
|
743
954
|
},
|
|
744
|
-
browserProcessPlugin(),
|
|
955
|
+
browserProcessPlugin(await clientBuildMeta(options.repoRoot)),
|
|
956
|
+
{
|
|
957
|
+
name: "dsh-css-specifiers",
|
|
958
|
+
resolveId(source, importer) {
|
|
959
|
+
if (!source.endsWith(".css") || source.startsWith(".") || source.startsWith("/")) return;
|
|
960
|
+
try {
|
|
961
|
+
return (0, node_module.createRequire)(importer ?? (0, node_path.join)(options.repoRoot, "package.json")).resolve(source);
|
|
962
|
+
} catch {
|
|
963
|
+
return;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
},
|
|
745
967
|
(0, _morlay_dsh_cordis_assets_styles.cssInlinePlugins)({
|
|
746
968
|
name: key,
|
|
747
969
|
assets: "threshold",
|
|
748
970
|
assetUrl: (file) => `${CLIENT_MODULES_PREFIX}/${key}/${file}`
|
|
749
971
|
}),
|
|
750
972
|
(0, _morlay_dsh_cordis_assets_assets.clientAssetPlugin)({ assets: "threshold" }),
|
|
751
|
-
(0, vite.esmExternalRequirePlugin)({ external: [...resolvable.keys()].filter((name) => !_morlay_dsh_cordis_assets_assets.NODE_BUILTIN.test(name)).map((name) => barePackageRegExp(name)) })
|
|
973
|
+
(0, vite.esmExternalRequirePlugin)({ external: [...resolvable.keys()].filter((name) => !_morlay_dsh_cordis_assets_assets.NODE_BUILTIN.test(name) && name !== options.name).map((name) => barePackageRegExp(name)) })
|
|
752
974
|
];
|
|
753
975
|
const startedAt = Date.now();
|
|
754
976
|
options.log?.(`[bundle-build] ${options.name}: rolldown start\n`);
|
|
755
977
|
const input = {};
|
|
756
|
-
|
|
978
|
+
const entryShims = /* @__PURE__ */ new Map();
|
|
979
|
+
const styleEntries = /* @__PURE__ */ new Map();
|
|
757
980
|
for (const [exportName, entry] of Object.entries(options.input)) {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
981
|
+
const kind = await moduleKindOf(entry);
|
|
982
|
+
if (kind === "cjs") {
|
|
983
|
+
const shim = await cjsShimOf({
|
|
984
|
+
name: options.name,
|
|
985
|
+
exportName,
|
|
986
|
+
entry,
|
|
987
|
+
from: entry,
|
|
988
|
+
...options.log === void 0 ? {} : { log: options.log }
|
|
989
|
+
});
|
|
990
|
+
if (shim !== void 0) entryShims.set(shim.id, shim.code);
|
|
991
|
+
input[exportName] = shim?.id ?? entry;
|
|
992
|
+
continue;
|
|
993
|
+
}
|
|
994
|
+
if (kind === "style") {
|
|
995
|
+
styleEntries.set(exportName, entry);
|
|
996
|
+
const styleId = `\0dsh-style:${options.name}:${exportName}:entry`;
|
|
997
|
+
entryShims.set(styleId, "export {};\n");
|
|
998
|
+
input[exportName] = styleId;
|
|
999
|
+
continue;
|
|
1000
|
+
}
|
|
1001
|
+
input[exportName] = entry;
|
|
770
1002
|
}
|
|
771
|
-
if (
|
|
1003
|
+
if (entryShims.size !== 0) plugins.push({
|
|
1004
|
+
name: "dsh-entry-shims",
|
|
1005
|
+
resolveId(source) {
|
|
1006
|
+
return entryShims.has(source) ? source : void 0;
|
|
1007
|
+
},
|
|
1008
|
+
load(source) {
|
|
1009
|
+
return entryShims.get(source);
|
|
1010
|
+
}
|
|
1011
|
+
});
|
|
772
1012
|
const bundle = await (0, rolldown.rolldown)({
|
|
773
1013
|
input,
|
|
774
1014
|
plugins,
|
|
@@ -785,10 +1025,7 @@ async function bundlePackageOnce(options, key) {
|
|
|
785
1025
|
"safari16.4"
|
|
786
1026
|
],
|
|
787
1027
|
decorator: { legacy: true },
|
|
788
|
-
define: {
|
|
789
|
-
"process.env": "{ env: {} }",
|
|
790
|
-
process: "{ env: {} }"
|
|
791
|
-
}
|
|
1028
|
+
define: { process: "{ env: {} }" }
|
|
792
1029
|
}
|
|
793
1030
|
});
|
|
794
1031
|
const { output: items } = await bundle.generate({
|
|
@@ -808,8 +1045,10 @@ async function bundlePackageOnce(options, key) {
|
|
|
808
1045
|
return;
|
|
809
1046
|
}
|
|
810
1047
|
await (0, node_fs_promises.mkdir)(outDir, { recursive: true });
|
|
1048
|
+
const styleExports = new Set(styleEntries.keys());
|
|
811
1049
|
for (const item of items) {
|
|
812
1050
|
if (item.type !== "chunk") continue;
|
|
1051
|
+
if (item.isEntry && styleExports.has(item.name)) continue;
|
|
813
1052
|
await (0, node_fs_promises.mkdir)((0, node_path.dirname)((0, node_path.join)(outDir, item.fileName)), { recursive: true });
|
|
814
1053
|
await (0, node_fs_promises.writeFile)((0, node_path.join)(outDir, item.fileName), item.code, "utf8");
|
|
815
1054
|
files[item.isEntry ? item.name : item.fileName] = `${key}/${item.fileName}`;
|
|
@@ -820,6 +1059,13 @@ async function bundlePackageOnce(options, key) {
|
|
|
820
1059
|
await (0, node_fs_promises.writeFile)((0, node_path.join)(outDir, asset.fileName), asset.source ?? "");
|
|
821
1060
|
files[asset.fileName] = `${key}/${asset.fileName}`;
|
|
822
1061
|
}
|
|
1062
|
+
for (const [exportName, entry] of styleEntries) await bundleStyleOf({
|
|
1063
|
+
entry,
|
|
1064
|
+
exportName,
|
|
1065
|
+
outDir,
|
|
1066
|
+
key,
|
|
1067
|
+
files
|
|
1068
|
+
});
|
|
823
1069
|
await (0, node_fs_promises.writeFile)(manifestPath, `${JSON.stringify({
|
|
824
1070
|
rev,
|
|
825
1071
|
files
|
|
@@ -1085,11 +1331,7 @@ async function startViteDev(ctx, options) {
|
|
|
1085
1331
|
const server = await (0, vite.createServer)({
|
|
1086
1332
|
root: appRoot,
|
|
1087
1333
|
base: "/",
|
|
1088
|
-
define: {
|
|
1089
|
-
"process.env.NODE_ENV": JSON.stringify(process.env["NODE_ENV"] ?? "development"),
|
|
1090
|
-
"process.env": "{ env: {} }",
|
|
1091
|
-
process: "{ env: {} }"
|
|
1092
|
-
},
|
|
1334
|
+
define: { process: "{ env: {} }" },
|
|
1093
1335
|
publicDir: false,
|
|
1094
1336
|
appType: "mpa",
|
|
1095
1337
|
configFile: false,
|
|
@@ -1114,7 +1356,7 @@ async function startViteDev(ctx, options) {
|
|
|
1114
1356
|
noDiscovery: true,
|
|
1115
1357
|
include: []
|
|
1116
1358
|
},
|
|
1117
|
-
plugins: [dshClientRowsPlugin({
|
|
1359
|
+
plugins: [browserProcessPlugin(await clientBuildMeta(repoRoot)), dshClientRowsPlugin({
|
|
1118
1360
|
repoRoot,
|
|
1119
1361
|
rows: () => rowsOf(options.rows),
|
|
1120
1362
|
resolvePluginUrl: options.resolvePluginUrl,
|
|
@@ -1352,6 +1594,7 @@ function apply(ctx) {
|
|
|
1352
1594
|
*/
|
|
1353
1595
|
const depsUrlOfSpecifier = async (specifier, importer) => {
|
|
1354
1596
|
if (specifier.startsWith("@/")) return void 0;
|
|
1597
|
+
if (specifier.endsWith(".css")) return void 0;
|
|
1355
1598
|
if (_morlay_dsh_cordis_assets_assets.NODE_BUILTIN.test(specifier)) return void 0;
|
|
1356
1599
|
const bare = packageNameOf(specifier) || specifier;
|
|
1357
1600
|
const root = await rootOf(bare);
|
|
@@ -1438,8 +1681,9 @@ function apply(ctx) {
|
|
|
1438
1681
|
if (code === void 0) return false;
|
|
1439
1682
|
const cacheControl = root !== void 0 && !root.includes("node_modules") ? "no-cache" : "public, max-age=31536000, immutable";
|
|
1440
1683
|
const etag = `"${bundled.rev}"`;
|
|
1684
|
+
const extension = target.slice(target.lastIndexOf(".") + 1).toLowerCase();
|
|
1441
1685
|
res.writeHead(200, {
|
|
1442
|
-
"content-type": "text/javascript; charset=utf-8",
|
|
1686
|
+
"content-type": extension === "css" ? "text/css; charset=utf-8" : "text/javascript; charset=utf-8",
|
|
1443
1687
|
"cache-control": cacheControl,
|
|
1444
1688
|
etag
|
|
1445
1689
|
});
|