@morlay/dsh-client-modules 0.2.11-alpha.2 → 0.2.11

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/index.mjs CHANGED
@@ -1,12 +1,14 @@
1
1
  import { createRequire, findPackageJSON } from "node:module";
2
2
  import { appendFile, mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
3
- import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
3
+ import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
4
4
  import { fileURLToPath, pathToFileURL } from "node:url";
5
5
  import { ASSET_MIME, NODE_BUILTIN, clientAssetPlugin } from "@morlay/dsh-cordis-assets/assets";
6
6
  import { createHash } from "node:crypto";
7
7
  import { cssInlinePlugins } from "@morlay/dsh-cordis-assets/styles";
8
8
  import { rolldown } from "rolldown";
9
9
  import { createServer, esmExternalRequirePlugin } from "vite";
10
+ import { execFile } from "node:child_process";
11
+ import { promisify } from "node:util";
10
12
  // src/trace.ts
11
13
  /**
12
14
  * 诊断细节(逐包预编译、逐请求那一类)的具名 logger。
@@ -85,6 +87,122 @@ async function devExcluded(profileDir) {
85
87
  function isAggregated(name, patterns) {
86
88
  return patterns.some((pattern) => pattern.test(name));
87
89
  }
90
+ // src/client-meta.ts
91
+ /**
92
+ * 浏览器构建里烘焙的版本元数据。
93
+ *
94
+ * 上游的 client 构建(`scripts/client-build-environment.ts`)把一组 `DSH_CLIENT_*` 值当 `define` 注入产物:
95
+ * - `DSH_CLIENT_VERSION`:设置页「当前版本」那一行;
96
+ * - `DSH_CLIENT_COMMIT_HASH` + `DSH_CLIENT_GIT_DIRTY`:侧栏显示的 `<commit>` / `<commit>-dirty`;
97
+ * - `DSH_CLIENT_BUILD_PROFILE`:`official` / `local`(官方品牌只在 official 下出现)。
98
+ *
99
+ * 这条链以前把 `process.env` 整个替换成 `{ env: {} }`,于是这些值全是 `undefined`:版本行不显示、
100
+ * `ui-settings-account` 还会因为 `DSH_CLIENT_VERSION` 缺失直接抛错。取值顺序(越靠前越权威):
101
+ *
102
+ * 1. 宿主进程的 `DSH_CLIENT_*`(`dshdev` 与桌面壳在起宿主时注入);
103
+ * 2. 仓库根的 `.dsh-build/client-build-environment.json`(`just prepare` 里的 `dshdev client-meta` 写);
104
+ * 3. 现算:仓库根的 `package.json` 版本 + `git rev-parse` / `git status`(dev 常见,代价是几次 git 调用)。
105
+ */
106
+ /** `prepare` 写、构建读的那份记录(形状与上游 `client-build-environment.json` 一致)。 */
107
+ const CLIENT_BUILD_RECORD = join(".dsh-build", "client-build-environment.json");
108
+ const VERSION = "DSH_CLIENT_VERSION";
109
+ const COMMIT = "DSH_CLIENT_COMMIT_HASH";
110
+ const DIRTY = "DSH_CLIENT_GIT_DIRTY";
111
+ const PROFILE = "DSH_CLIENT_BUILD_PROFILE";
112
+ /** 环境里那组 `DSH_CLIENT_*` → 元数据。 */
113
+ function metaOfEnvironment(environment) {
114
+ const version = environment[VERSION];
115
+ const commit = environment[COMMIT];
116
+ const dirty = environment[DIRTY];
117
+ const profile = environment[PROFILE];
118
+ return {
119
+ ...version === void 0 || version === "" ? {} : { version },
120
+ ...commit === void 0 || commit === "" ? {} : { commit },
121
+ ...dirty === "true" ? { dirty: true } : {},
122
+ ...profile === void 0 || profile === "" ? {} : { profile }
123
+ };
124
+ }
125
+ const runFile = promisify(execFile);
126
+ /** `git` 的输出;不在 git 工作树里、或 git 不可用时 undefined。 */
127
+ async function gitOutput(root, args) {
128
+ try {
129
+ const { stdout } = await runFile("git", args, {
130
+ cwd: root,
131
+ encoding: "utf8"
132
+ });
133
+ return stdout.trim();
134
+ } catch {
135
+ return;
136
+ }
137
+ }
138
+ /**
139
+ * 现算:**装的是哪一版 harness**(`DEEPSEEK_HARNESS_VERSION`,与同步链同一个真源)+ 本仓库的 git 元数据。
140
+ *
141
+ * 正常路径拿不到这里:`dshdev` 会注入环境、`prepare` 会写记录。只有"绕过 dshdev 直接构建 client 产物"
142
+ * 时才走到,所以版本只能取环境或仓库根的 `package.json`(后者不是权威值,缺 env 时用它兜底)。
143
+ */
144
+ async function computeMeta(repoRoot) {
145
+ const explicit = process.env["DEEPSEEK_HARNESS_VERSION"];
146
+ let version = explicit === void 0 || explicit === "" ? void 0 : explicit;
147
+ if (version === void 0) {
148
+ const manifest = await readFile(join(repoRoot, "package.json"), "utf8").catch(() => void 0);
149
+ if (manifest !== void 0) {
150
+ const parsed = JSON.parse(manifest);
151
+ if (typeof parsed.version === "string" && parsed.version !== "") version = parsed.version;
152
+ }
153
+ }
154
+ const commit = await gitOutput(repoRoot, [
155
+ "rev-parse",
156
+ "--short=7",
157
+ "HEAD"
158
+ ]);
159
+ const status = await gitOutput(repoRoot, [
160
+ "status",
161
+ "--porcelain",
162
+ "--untracked-files=normal"
163
+ ]);
164
+ return {
165
+ ...version === void 0 ? {} : { version },
166
+ ...commit === void 0 ? {} : { commit },
167
+ ...status === void 0 || status === "" ? {} : { dirty: true }
168
+ };
169
+ }
170
+ /** 读 `prepare` 写下的那份记录(缺失 / 坏掉都给 undefined,不挡构建)。 */
171
+ async function recordedMeta(repoRoot) {
172
+ const raw = await readFile(join(repoRoot, CLIENT_BUILD_RECORD), "utf8").catch(() => void 0);
173
+ if (raw === void 0) return void 0;
174
+ try {
175
+ return metaOfEnvironment(JSON.parse(raw).environment ?? {});
176
+ } catch {
177
+ return;
178
+ }
179
+ }
180
+ /**
181
+ * 这个仓库在浏览器产物里该带的版本元数据。
182
+ * @param repoRoot - 仓库根(dev 是工作区,打包形态是 profile:那里通常读不到记录,靠宿主注入的环境)。
183
+ */
184
+ async function clientBuildMeta(repoRoot) {
185
+ const inherited = metaOfEnvironment(process.env);
186
+ if (inherited.version !== void 0 && inherited.commit !== void 0) return inherited;
187
+ return {
188
+ ...await recordedMeta(repoRoot) ?? await computeMeta(repoRoot),
189
+ ...inherited
190
+ };
191
+ }
192
+ /**
193
+ * `define` 用的一组键:`process.env.DSH_CLIENT_*` → 字面量。
194
+ *
195
+ * `browserProcessPlugin` 会跳过这些键(见那里的负向前瞻),所以 define 这一层能把它们换成真实值;
196
+ * 其余 `process.env` 仍是空壳对象。
197
+ */
198
+ function clientMetaDefines(meta) {
199
+ return {
200
+ [`process.env.${VERSION}`]: meta.version === void 0 ? "undefined" : JSON.stringify(meta.version),
201
+ [`process.env.${COMMIT}`]: meta.commit === void 0 ? "undefined" : JSON.stringify(meta.commit),
202
+ [`process.env.${DIRTY}`]: meta.dirty === true ? JSON.stringify("true") : "undefined",
203
+ [`process.env.${PROFILE}`]: meta.profile === void 0 ? "undefined" : JSON.stringify(meta.profile)
204
+ };
205
+ }
88
206
  // src/resolve.ts
89
207
  /** 路由前缀(沿用上游的 `/plugins`,页面上不改 URL 形态)。 */
90
208
  const PLUGIN_ROUTE = "/plugins";
@@ -385,7 +503,7 @@ function clientModulesAlias(options) {
385
503
  * 实例)。内容变了**不换 URL**,靠 `cache-control: no-cache` + `ETag` 协商(vite dev 对它自己的模块
386
504
  * 也是这套)。
387
505
  */
388
- const BUNDLER_REV = createHash("sha1").update(`client-modules:rules:22`).digest("hex").slice(0, 8);
506
+ const BUNDLER_REV = createHash("sha1").update(`client-modules:rules:36`).digest("hex").slice(0, 8);
389
507
  /** 包 → 它在浏览器里的地址(联邦式)。 */
390
508
  function depsUrlOf(specifier, version, file = "index.js") {
391
509
  const name = packageNameOf(specifier) || specifier;
@@ -512,8 +630,13 @@ function urlOf(specifier, tables) {
512
630
  * **只替换 `process.env` 系列**:裸的 `process` 不能碰——它可能只是个普通标识符(`zod` 里就有一个叫
513
631
  * `process` 的函数,替换掉直接把源码变成语法错误)。
514
632
  */
515
- function browserProcessPlugin() {
516
- const replace = (code) => code.replaceAll(/\bprocess\.env\.NODE_ENV\b/gu, JSON.stringify("development")).replaceAll(/\bprocess\.env\b/gu, "({ env: {} })");
633
+ function browserProcessPlugin(meta = {}) {
634
+ const metaDefines = Object.entries(clientMetaDefines(meta));
635
+ const replace = (code) => {
636
+ let next = code.replaceAll(/\bprocess\.env\.NODE_ENV\b/gu, JSON.stringify("development"));
637
+ for (const [key, value] of metaDefines) next = next.replaceAll(key, value);
638
+ return next.replaceAll(/\bprocess\.env\b/gu, "({ env: {} })");
639
+ };
517
640
  return {
518
641
  name: "dsh-browser-process",
519
642
  enforce: "pre",
@@ -527,52 +650,205 @@ function browserProcessPlugin() {
527
650
  };
528
651
  }
529
652
  /**
530
- * CJS 入口的**具名导出包装**(`react` 这类)。
653
+ * `export const <名>` 用不了的键:JS 的保留字(`zod@4` 的 CJS 面有 `function` / `void` / `null`
654
+ * 这些键,原样拼出去就是 `SyntaxError`)。
655
+ */
656
+ const RESERVED_EXPORT_NAMES = /* @__PURE__ */ new Set([
657
+ "await",
658
+ "break",
659
+ "case",
660
+ "catch",
661
+ "class",
662
+ "const",
663
+ "continue",
664
+ "debugger",
665
+ "default",
666
+ "delete",
667
+ "do",
668
+ "else",
669
+ "enum",
670
+ "eval",
671
+ "export",
672
+ "extends",
673
+ "false",
674
+ "finally",
675
+ "for",
676
+ "function",
677
+ "if",
678
+ "implements",
679
+ "import",
680
+ "in",
681
+ "instanceof",
682
+ "interface",
683
+ "let",
684
+ "new",
685
+ "null",
686
+ "package",
687
+ "private",
688
+ "protected",
689
+ "public",
690
+ "return",
691
+ "static",
692
+ "super",
693
+ "switch",
694
+ "this",
695
+ "throw",
696
+ "true",
697
+ "try",
698
+ "typeof",
699
+ "undefined",
700
+ "var",
701
+ "void",
702
+ "while",
703
+ "with",
704
+ "yield"
705
+ ]);
706
+ /** ESM 的顶层语法节点:`import` / `export` 声明。 */
707
+ const ESM_STATEMENT_TYPES = /* @__PURE__ */ new Set([
708
+ "ImportDeclaration",
709
+ "ExportNamedDeclaration",
710
+ "ExportDefaultDeclaration",
711
+ "ExportAllDeclaration"
712
+ ]);
713
+ /**
714
+ * `.js` 且没有 `type` 时,按 **Node 的语法检测**判:有 ESM 语法(顶层 `import`/`export`)就是 ESM,
715
+ * 否则 CJS(Node 23+ 默认开启 `--experimental-detect-module`)。
716
+ *
717
+ * 用 rolldown 自带的解析器(`rolldown/parseAst`),不自己写词法扫描。
718
+ *
719
+ * @param entry - `.js` 入口的绝对路径。
720
+ * @returns 是否含 ESM 语法。
721
+ */
722
+ async function hasModuleSyntax(entry) {
723
+ try {
724
+ const [{ parseAst }, source] = await Promise.all([import("rolldown/parseAst"), readFile(entry, "utf8")]);
725
+ return parseAst(source, null, entry).body.some((node) => ESM_STATEMENT_TYPES.has(node.type));
726
+ } catch {
727
+ return false;
728
+ }
729
+ }
730
+ /**
731
+ * 一个入口的模块类型:**扩展名 + 最近的 `package.json`**,不猜源码。
732
+ *
733
+ * | 入口 | 类型 |
734
+ * | --- | --- |
735
+ * | `.mjs` / `.mts` | `esm` |
736
+ * | `.cjs` / `.cts` | `cjs` |
737
+ * | `.js` + `type: "module"` | `esm` |
738
+ * | `.js` + `module` 字段指向它 | `esm`(打包器约定) |
739
+ * | 其余 `.js` | `cjs` |
740
+ * | `.ts` / `.tsx` / `.jsx` | `source`(转译面,原样) |
741
+ * | `.css` | `style` |
742
+ * | 其他(图片 / 字体 / `.json` …) | `asset` |
743
+ *
744
+ * 这是 `exports` 条件(`import` / `require`)选出入口之后的**第二步**:条件决定"哪个文件",这里决定
745
+ * "那个文件是什么模块"。只有 `cjs` 需要导出面适配,`esm` / `source` 一律原样。
746
+ *
747
+ * @param entry - 入口的绝对路径。
748
+ * @returns 模块类型。
749
+ */
750
+ async function moduleKindOf(entry) {
751
+ const extension = extname(entry).toLowerCase();
752
+ if (extension === ".css") return "style";
753
+ if (extension === ".mjs" || extension === ".mts") return "esm";
754
+ if (extension === ".cjs" || extension === ".cts") return "cjs";
755
+ if (extension === ".ts" || extension === ".tsx" || extension === ".jsx") return "source";
756
+ if (extension !== ".js") return "asset";
757
+ const manifestPath = await packageJsonNear(entry);
758
+ const manifest = JSON.parse(await readFile(manifestPath, "utf8").catch(() => "{}"));
759
+ if (manifest.type === "module") return "esm";
760
+ if (manifest.type === "commonjs") return "cjs";
761
+ return await hasModuleSyntax(entry) ? "esm" : "cjs";
762
+ }
763
+ /**
764
+ * CJS 入口的**具名导出包装**(`react` / `@xterm/xterm` 这类)。
765
+ *
766
+ * rolldown 对 CJS 入口的产物的具名导出只能靠静态分析;`module.exports = require(…)` 与 UMD 包装它都分析
767
+ * 不出来,产物只剩 `export default …`——而浏览器侧要 `import { Fragment } from "react"`、`import { Terminal }
768
+ * from "@xterm/xterm"`,整个 import 阶段就失败。ESM 的导出必须**静态可枚举**,所以这里求值入口拿到它的键,
769
+ * 生成一份把它们写成 `export const` 的包装文件,拿包装文件当入口。
531
770
  *
532
- * `react` 的入口是 `module.exports = require('./cjs/react.development.js')`,rolldown 静态分析不出具名
533
- * 导出,产物只剩 `export default …`——而浏览器侧要 `import { Fragment } from "react"`,于是**整个 import
534
- * 阶段就失败**(入口一行都执行不了)。ESM 的导出必须**静态可枚举**(不能 `export * from` 一个运行期对象),
535
- * 所以这里求值入口拿到它的键,生成一份把它们写成 `export const` 的包装文件,拿包装文件当入口。
771
+ * 类型由 {@link moduleKindOf} 先按 **exports 落点 + 扩展名/type** 静态判定,只有 CJS 才进来;
772
+ * 这里求值一次只为**拿键**(`export const <键>` + `default = module.exports`)。ESM 原封不动。
536
773
  *
537
- * @returns 包装文件路径;入口不是 CJS、或求值拿不到键时 undefined(保持原样)。
774
+ * @returns 包装文件路径;入口不是 node 能求值的 CJS、或没有可静态导出的键时 undefined(保持原样)。
538
775
  */
539
776
  async function cjsShimOf(options) {
540
- const source = await readFile(options.entry, "utf8").catch(() => "");
541
- if (!(/\.(?:cjs|js)$/u.test(options.entry) && /(?:^|\n)\s*(?:module\.exports\s*=|exports\.[A-Za-z_$][A-Za-z0-9_$]*\s*=)/u.test(source))) return void 0;
542
- let keys;
777
+ let loaded;
543
778
  try {
544
- const loaded = createRequire(options.entry)(options.entry);
545
- keys = Object.keys(loaded ?? {});
779
+ loaded = createRequire(options.entry)(options.entry);
546
780
  } catch {
547
781
  return;
548
782
  }
549
- const named = keys.filter((key) => key !== "default" && /^[A-Za-z_$][A-Za-z0-9_$]*$/u.test(key));
783
+ const named = Object.keys(loaded ?? {}).filter((key) => key !== "default" && /^[A-Za-z_$][A-Za-z0-9_$]*$/u.test(key) && !RESERVED_EXPORT_NAMES.has(key));
550
784
  if (named.length === 0) return void 0;
551
- const file = join(options.outDir, `${options.exportName.replaceAll("/", "+")}.cjs-shim.mjs`);
552
- await mkdir(dirname(file), { recursive: true });
553
- await writeFile(file, [
554
- `import mod from ${JSON.stringify(options.entry)};`,
785
+ const id = `\0dsh-cjs-shim:${options.name}:${options.exportName}`;
786
+ const code = [
787
+ `import mod from ${JSON.stringify(options.from)};`,
555
788
  "const __cjs = mod ?? {};",
556
789
  "export default __cjs;",
557
790
  ...named.map((key) => `export const ${key} = __cjs[${JSON.stringify(key)}];`),
558
791
  ""
559
- ].join("\n"), "utf8");
792
+ ].join("\n");
560
793
  options.log?.(`[bundle-cjs] ${options.name}/${options.exportName}: 补 ${String(named.length)} 个具名导出\n`);
561
- return file;
794
+ return {
795
+ id,
796
+ code
797
+ };
562
798
  }
563
799
  /**
564
- * CSS 入口的包装:**样式文件不能直接当入口**——vite 的库模式固定 `cssCodeSplit: false`,遇到
565
- * `rolldownOptions.input` 里有 `.css` 直接报错。包一层 side-effect import,让样式插件按它一贯的方式
566
- * 把它编成"注入 `<style>`"的模块。
800
+ * CSS 出口的产物:**就是 CSS 文本**(不包装成 JS、不注入 `<style>`)。
567
801
  *
568
- * @returns 包装文件路径;入口不是 CSS 时 undefined。
802
+ * JS 里的 `import "x.css"` 是另一条路(`cssInlinePlugins` 在构建期内联成注入样式的模块),与本函数无关。
803
+ * 这里的 `url()` 指向包内资产:读到字节、按内容 hash 落到 `assets/`、改写成联邦地址——否则浏览器会按
804
+ * CSS 的相对路径去请求 `.pnpm` 里的文件,路由按清单服务就是 404。
805
+ *
806
+ * `lightningcss` 的 visitor 是同步回调,所以分两遍:第一遍只收集相对 `url()`,异步读资产/算 hash;
807
+ * 第二遍按映射改写(全程异步 IO)。
808
+ *
809
+ * @param options - 入口文件、出口名、缓存目录、包键、产物清单。
569
810
  */
570
- async function cssShimOf(options) {
571
- if (!options.entry.endsWith(".css")) return void 0;
572
- const file = join(options.outDir, `${options.exportName.replaceAll("/", "+")}.css-shim.mjs`);
811
+ async function bundleStyleOf(options) {
812
+ const { transform } = await import("lightningcss");
813
+ const source = await readFile(options.entry);
814
+ const relativeUrl = (ref) => ref !== "" && !ref.startsWith("#") && !ref.startsWith("/") && !/^[a-z][a-z0-9+.-]*:/iu.test(ref);
815
+ const refs = /* @__PURE__ */ new Set();
816
+ transform({
817
+ filename: options.entry,
818
+ code: source,
819
+ minify: false,
820
+ visitor: { Url(node) {
821
+ if (relativeUrl(node.url)) refs.add(node.url);
822
+ } }
823
+ });
824
+ const mapping = /* @__PURE__ */ new Map();
825
+ for (const ref of refs) {
826
+ const absolute = join(dirname(options.entry), ref);
827
+ const bytes = await readFile(absolute).catch(() => void 0);
828
+ if (bytes === void 0) continue;
829
+ const extension = extname(absolute);
830
+ const name = `assets/${basename(absolute, extension)}-${createHash("sha256").update(bytes).digest("hex").slice(0, 8)}${extension}`;
831
+ mapping.set(ref, `${CLIENT_MODULES_PREFIX}/${options.key}/${name}`);
832
+ await mkdir(dirname(join(options.outDir, name)), { recursive: true });
833
+ await writeFile(join(options.outDir, name), bytes);
834
+ options.files[name] = `${options.key}/${name}`;
835
+ }
836
+ const result = transform({
837
+ filename: options.entry,
838
+ code: source,
839
+ minify: false,
840
+ visitor: { Url(node) {
841
+ const rewritten = mapping.get(node.url);
842
+ return rewritten === void 0 ? void 0 : {
843
+ ...node,
844
+ url: rewritten
845
+ };
846
+ } }
847
+ });
848
+ const file = join(options.outDir, options.exportName);
573
849
  await mkdir(dirname(file), { recursive: true });
574
- await writeFile(file, `import ${JSON.stringify(options.entry)};\n`, "utf8");
575
- return file;
850
+ await writeFile(file, result.code);
851
+ options.files[options.exportName] = `${options.key}/${options.exportName}`;
576
852
  }
577
853
  /**
578
854
  * 确保该包的产物在(指纹没变、且**这次要的出口**都在就直接用)。
@@ -657,7 +933,8 @@ async function bundlePackageOnce(options, key) {
657
933
  const outDir = join(options.cacheRoot, key);
658
934
  const manifestPath = join(outDir, ".files.json");
659
935
  options.log?.(`[bundle-rev] ${options.name}: hash-start watch=${options.watch.join(",")}\n`);
660
- const rev = `${BUNDLER_REV}:${await fingerprint(options.watch)}`;
936
+ const rootRev = createHash("sha1").update(dirname(options.watch[0] ?? options.manifestOf ?? "")).digest("hex").slice(0, 8);
937
+ const rev = `${BUNDLER_REV}:${rootRev}:${await fingerprint(options.watch)}`;
661
938
  options.log?.(`[bundle-rev] ${options.name}: hash=${rev}\n`);
662
939
  const cached = JSON.parse(await readFile(manifestPath, "utf8").catch(() => "{}"));
663
940
  const fresh = cached.rev === rev;
@@ -674,32 +951,70 @@ async function bundlePackageOnce(options, key) {
674
951
  for (const { find, replacement } of options.alias) if (typeof find === "string" ? source.startsWith(find) : find.test(source)) return source.replace(find, replacement);
675
952
  }
676
953
  },
677
- browserProcessPlugin(),
954
+ browserProcessPlugin(await clientBuildMeta(options.repoRoot)),
955
+ {
956
+ name: "dsh-css-specifiers",
957
+ resolveId(source, importer) {
958
+ if (!source.endsWith(".css") || source.startsWith(".") || source.startsWith("/")) return;
959
+ try {
960
+ return createRequire(importer ?? join(options.repoRoot, "package.json")).resolve(source);
961
+ } catch {
962
+ return;
963
+ }
964
+ }
965
+ },
678
966
  cssInlinePlugins({
679
967
  name: key,
680
968
  assets: "threshold",
681
969
  assetUrl: (file) => `${CLIENT_MODULES_PREFIX}/${key}/${file}`
682
970
  }),
683
971
  clientAssetPlugin({ assets: "threshold" }),
684
- esmExternalRequirePlugin({ external: [...resolvable.keys()].filter((name) => !NODE_BUILTIN.test(name)).map((name) => barePackageRegExp(name)) })
972
+ esmExternalRequirePlugin({ external: [...resolvable.keys()].filter((name) => !NODE_BUILTIN.test(name) && name !== options.name).map((name) => barePackageRegExp(name)) })
685
973
  ];
686
974
  const startedAt = Date.now();
687
975
  options.log?.(`[bundle-build] ${options.name}: rolldown start\n`);
688
976
  const input = {};
689
- for (const [exportName, entry] of Object.entries(options.input)) input[exportName] = await cjsShimOf({
690
- name: options.name,
691
- exportName,
692
- entry,
693
- ...options.log === void 0 ? {} : { log: options.log },
694
- outDir
695
- }) ?? await cssShimOf({
696
- exportName,
697
- entry,
698
- outDir
699
- }) ?? entry;
977
+ const entryShims = /* @__PURE__ */ new Map();
978
+ const styleEntries = /* @__PURE__ */ new Map();
979
+ for (const [exportName, entry] of Object.entries(options.input)) {
980
+ const kind = await moduleKindOf(entry);
981
+ if (kind === "cjs") {
982
+ const shim = await cjsShimOf({
983
+ name: options.name,
984
+ exportName,
985
+ entry,
986
+ from: entry,
987
+ ...options.log === void 0 ? {} : { log: options.log }
988
+ });
989
+ if (shim !== void 0) entryShims.set(shim.id, shim.code);
990
+ input[exportName] = shim?.id ?? entry;
991
+ continue;
992
+ }
993
+ if (kind === "style") {
994
+ styleEntries.set(exportName, entry);
995
+ const styleId = `\0dsh-style:${options.name}:${exportName}:entry`;
996
+ entryShims.set(styleId, "export {};\n");
997
+ input[exportName] = styleId;
998
+ continue;
999
+ }
1000
+ input[exportName] = entry;
1001
+ }
1002
+ if (entryShims.size !== 0) plugins.push({
1003
+ name: "dsh-entry-shims",
1004
+ resolveId(source) {
1005
+ return entryShims.has(source) ? source : void 0;
1006
+ },
1007
+ load(source) {
1008
+ return entryShims.get(source);
1009
+ }
1010
+ });
700
1011
  const bundle = await rolldown({
701
1012
  input,
702
1013
  plugins,
1014
+ onLog: (level, log, handler) => {
1015
+ if (log.code === "CONFIGURATION_FIELD_CONFLICT" && log.message?.includes("experimentalDecorators") === true) return;
1016
+ handler(level, log);
1017
+ },
703
1018
  ...options.tsconfig === void 0 ? {} : { resolve: { tsconfigFilename: options.tsconfig } },
704
1019
  transform: {
705
1020
  target: [
@@ -709,10 +1024,7 @@ async function bundlePackageOnce(options, key) {
709
1024
  "safari16.4"
710
1025
  ],
711
1026
  decorator: { legacy: true },
712
- define: {
713
- "process.env": "{ env: {} }",
714
- process: "{ env: {} }"
715
- }
1027
+ define: { process: "{ env: {} }" }
716
1028
  }
717
1029
  });
718
1030
  const { output: items } = await bundle.generate({
@@ -732,8 +1044,10 @@ async function bundlePackageOnce(options, key) {
732
1044
  return;
733
1045
  }
734
1046
  await mkdir(outDir, { recursive: true });
1047
+ const styleExports = new Set(styleEntries.keys());
735
1048
  for (const item of items) {
736
1049
  if (item.type !== "chunk") continue;
1050
+ if (item.isEntry && styleExports.has(item.name)) continue;
737
1051
  await mkdir(dirname(join(outDir, item.fileName)), { recursive: true });
738
1052
  await writeFile(join(outDir, item.fileName), item.code, "utf8");
739
1053
  files[item.isEntry ? item.name : item.fileName] = `${key}/${item.fileName}`;
@@ -744,6 +1058,13 @@ async function bundlePackageOnce(options, key) {
744
1058
  await writeFile(join(outDir, asset.fileName), asset.source ?? "");
745
1059
  files[asset.fileName] = `${key}/${asset.fileName}`;
746
1060
  }
1061
+ for (const [exportName, entry] of styleEntries) await bundleStyleOf({
1062
+ entry,
1063
+ exportName,
1064
+ outDir,
1065
+ key,
1066
+ files
1067
+ });
747
1068
  await writeFile(manifestPath, `${JSON.stringify({
748
1069
  rev,
749
1070
  files
@@ -1009,11 +1330,7 @@ async function startViteDev(ctx, options) {
1009
1330
  const server = await createServer({
1010
1331
  root: appRoot,
1011
1332
  base: "/",
1012
- define: {
1013
- "process.env.NODE_ENV": JSON.stringify(process.env["NODE_ENV"] ?? "development"),
1014
- "process.env": "{ env: {} }",
1015
- process: "{ env: {} }"
1016
- },
1333
+ define: { process: "{ env: {} }" },
1017
1334
  publicDir: false,
1018
1335
  appType: "mpa",
1019
1336
  configFile: false,
@@ -1035,11 +1352,10 @@ async function startViteDev(ctx, options) {
1035
1352
  }
1036
1353
  },
1037
1354
  optimizeDeps: {
1038
- disabled: true,
1039
1355
  noDiscovery: true,
1040
1356
  include: []
1041
1357
  },
1042
- plugins: [dshClientRowsPlugin({
1358
+ plugins: [browserProcessPlugin(await clientBuildMeta(repoRoot)), dshClientRowsPlugin({
1043
1359
  repoRoot,
1044
1360
  rows: () => rowsOf(options.rows),
1045
1361
  resolvePluginUrl: options.resolvePluginUrl,
@@ -1097,7 +1413,8 @@ const inject = ["loader", "webServer"];
1097
1413
  */
1098
1414
  async function clientRowsOf(loader) {
1099
1415
  const found = /* @__PURE__ */ new Map();
1100
- for (const entry of loader.entries()) {
1416
+ for (const raw of loader.entries()) {
1417
+ const entry = raw;
1101
1418
  if (entry.disabled === true || entry.fiber === void 0) continue;
1102
1419
  const name = entry.options?.name;
1103
1420
  if (typeof name !== "string" || name === "") continue;
@@ -1276,6 +1593,7 @@ function apply(ctx) {
1276
1593
  */
1277
1594
  const depsUrlOfSpecifier = async (specifier, importer) => {
1278
1595
  if (specifier.startsWith("@/")) return void 0;
1596
+ if (specifier.endsWith(".css")) return void 0;
1279
1597
  if (NODE_BUILTIN.test(specifier)) return void 0;
1280
1598
  const bare = packageNameOf(specifier) || specifier;
1281
1599
  const root = await rootOf(bare);
@@ -1362,8 +1680,9 @@ function apply(ctx) {
1362
1680
  if (code === void 0) return false;
1363
1681
  const cacheControl = root !== void 0 && !root.includes("node_modules") ? "no-cache" : "public, max-age=31536000, immutable";
1364
1682
  const etag = `"${bundled.rev}"`;
1683
+ const extension = target.slice(target.lastIndexOf(".") + 1).toLowerCase();
1365
1684
  res.writeHead(200, {
1366
- "content-type": "text/javascript; charset=utf-8",
1685
+ "content-type": extension === "css" ? "text/css; charset=utf-8" : "text/javascript; charset=utf-8",
1367
1686
  "cache-control": cacheControl,
1368
1687
  etag
1369
1688
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@morlay/dsh-client-modules",
3
- "version": "0.2.11-alpha.2",
3
+ "version": "0.2.11",
4
4
  "description": "Dev-form web layer for dsh: serves the page, the app entry and every client resource through an in-process vite dev server (middleware mode) — the node-side counterpart of upstream @deepseek-ai/dsh-client-modules, with the module table replaced by vite's resolver, dependency prebundling and HMR.",
5
5
  "keywords": [
6
6
  "client",
@@ -36,26 +36,26 @@
36
36
  "./locale/*.json": "./locale/*.json"
37
37
  },
38
38
  "dependencies": {
39
- "@deepseek-ai/dsh-client-ui-theme": "^0.2.2-alpha.2",
40
- "@deepseek-ai/dsh-host-webserver": "^0.2.2-alpha.2",
41
- "@morlay/dsh-client-ui-primitives": "0.0.14-alpha.2",
42
- "@morlay/dsh-cordis-assets": "0.2.10-alpha.2",
39
+ "@deepseek-ai/dsh-client-ui-theme": "^0.2.2-alpha.3",
40
+ "@deepseek-ai/dsh-host-webserver": "^0.2.2-alpha.3",
41
+ "@morlay/dsh-client-ui-primitives": "0.0.14",
42
+ "@morlay/dsh-cordis-assets": "0.2.10",
43
43
  "@oxc-project/runtime": "^0.153.0",
44
44
  "lightningcss": "^1.30.0",
45
45
  "vite": "^8.3.1"
46
46
  },
47
47
  "devDependencies": {
48
- "@deepseek-ai/cordis": "^4.0.6-alpha.1",
48
+ "@deepseek-ai/cordis": "^4.0.6-alpha.2",
49
49
  "@local/devkit": "0.1.0",
50
50
  "@types/node": "^26.5.0"
51
51
  },
52
52
  "peerDependencies": {
53
- "@deepseek-ai/cordis": "^4.0.6-alpha.1",
53
+ "@deepseek-ai/cordis": "^4.0.6-alpha.2",
54
54
  "react": "^18.3.1",
55
55
  "rolldown": "^1.2.14"
56
56
  },
57
57
  "engines": {
58
- "dsh": ">= 0.2.2-alpha.2 < 0.2.3",
58
+ "dsh": ">= 0.2.2-alpha.3 < 0.2.3",
59
59
  "node": ">=24.11.0"
60
60
  },
61
61
  "dsh": {