58 lines
2.2 KiB
JavaScript
58 lines
2.2 KiB
JavaScript
// Builds client.js (browser half) from src/client. Mirrors the DS Bot wrapper so dsh can
|
||
// load it as a classic script and resolve platform modules from the shell.
|
||
// node scripts/build-client.mjs writes client.js
|
||
// node scripts/build-client.mjs --check exit 1 if client.js differs from a fresh build
|
||
import { readFileSync, writeFileSync } from 'node:fs'
|
||
import { dirname, join } from 'node:path'
|
||
import { fileURLToPath } from 'node:url'
|
||
|
||
const PLUGIN = dirname(dirname(fileURLToPath(import.meta.url)))
|
||
const OUTPUT = join(PLUGIN, 'client.js')
|
||
const PLATFORM_MODULES = ['react', 'react-dom', '@deepseek-ai/dsh-client-ui-primitives']
|
||
|
||
export async function bundleClient() {
|
||
let esbuild
|
||
try { esbuild = await import('esbuild') } catch {
|
||
throw new Error('缺少 esbuild:请先在包内安装 devDependencies(pnpm/npm i),再构建 client.js。')
|
||
}
|
||
const result = await esbuild.build({
|
||
absWorkingDir: PLUGIN,
|
||
entryPoints: ['src/client/index.js'],
|
||
bundle: true,
|
||
format: 'cjs',
|
||
platform: 'browser',
|
||
target: 'es2022',
|
||
external: PLATFORM_MODULES,
|
||
treeShaking: false,
|
||
charset: 'utf8',
|
||
write: false,
|
||
logLevel: 'silent',
|
||
})
|
||
if (result.warnings.length > 0) throw new Error('esbuild warnings:\n' + result.warnings.map(w => w.text).join('\n'))
|
||
return [
|
||
'// Generated from src/client by `npm run build` (scripts/build-client.mjs). Do not edit by hand.',
|
||
'window.__ModuleLoader__.load({',
|
||
" id: 'dsh-sansheng',",
|
||
' factory: (require) => {',
|
||
'const module = { exports: {} }',
|
||
result.outputFiles[0].text.trimEnd(),
|
||
"return Object.defineProperty({ ...module.exports }, Symbol.toStringTag, { value: 'Module' })",
|
||
' },',
|
||
'})',
|
||
'',
|
||
].join('\n')
|
||
}
|
||
|
||
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
||
const next = await bundleClient()
|
||
if (process.argv.includes('--check')) {
|
||
let current = ''
|
||
try { current = readFileSync(OUTPUT, 'utf8') } catch {}
|
||
if (current !== next) { console.error('client.js 与 src/client 不一致,请运行 npm run build'); process.exit(1) }
|
||
console.log('client.js 是最新的')
|
||
} else {
|
||
writeFileSync(OUTPUT, next)
|
||
console.log('已写出 client.js')
|
||
}
|
||
}
|