dsh-sansheng-liubu/sansheng-better-sidebar/scripts/build-client.mjs

65 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Builds client.js (browser half) from src/client.
// 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 { createRequire } from 'node:module'
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']
const BUNDLE_ID = 'dsh-sansheng-liubu-better-sidebar'
async function loadEsbuild() {
try { return await import('esbuild') } catch { /* 本地开发时尝试复用主插件依赖 */ }
try {
const require = createRequire(new URL('../../sansheng/package.json', import.meta.url))
return require('esbuild')
} catch { /* 继续给统一错误 */ }
throw new Error('缺少 esbuild:请先在包内安装 devDependencies(pnpm/npm i),再构建 client.js。')
}
export async function bundleClient() {
const esbuild = await loadEsbuild()
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: ' + JSON.stringify(BUNDLE_ID) + ',',
' 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')
}
}