/** * 客户端 bundle 包装脚本 * 将 tsdown 生成的 CJS bundle 包装为 DSH 客户端模块系统格式: * window.__ModuleLoader__.load({ id, factory: (require) => {...} }) */ import { readFileSync, writeFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; const root = dirname(dirname(fileURLToPath(import.meta.url))); const bundlePath = join(root, 'lib/client/index.js'); const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')); const bundle = readFileSync(bundlePath, 'utf8'); const wrapped = 'window.__ModuleLoader__.load({\n' + ' id: "' + pkg.name + '",\n' + ' factory: (require) => {\n' + ' var module = { exports: {} };\n' + ' var exports = module.exports;\n' + ' Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });\n' + bundle + '\n' + ' return module.exports;\n' + ' }\n' + '});\n'; writeFileSync(bundlePath, wrapped, 'utf8'); console.log('client bundle wrapped ->', bundlePath, '(' + wrapped.length + ' bytes)');