84 lines
3.3 KiB
TypeScript
84 lines
3.3 KiB
TypeScript
import { resolve } from 'node:path'
|
|
import type { TsdownPlugin, UserConfig } from 'tsdown'
|
|
import { externalClientBundle } from './tools/client-build.js'
|
|
|
|
// The vendored dshx adapter resolves the target Harness client platform table
|
|
// from tools/harness by default (a copy of dsh-v0.2.2-rc.2
|
|
// packages/client/web/src/platform.ts), so a clean checkout builds without a
|
|
// Harness source checkout. Set DSHX_HARNESS to build against another target.
|
|
const bundle = externalClientBundle('dsh-better-chat', ['src/host.ts'], {
|
|
clientEntry: 'src/client/index.tsx',
|
|
}) as UserConfig[]
|
|
|
|
const portableOutput: TsdownPlugin = {
|
|
name: 'dsh-better-chat-portable-output',
|
|
generateBundle(_options, output) {
|
|
const client = output['client.js']
|
|
if (client?.type !== 'chunk') this.error('client.js was not emitted')
|
|
const root = resolve('.').replaceAll('\\', '/').replace(/\/$/u, '')
|
|
client.code = client.code.replace(
|
|
/^([ \t]*\/\/(?:#region )? ?)\\0dshx-css-module:([^\r\n]*?)(\r?)$/gmu,
|
|
(_match, prefix: string, rawPath: string, ending: string) => {
|
|
let cssPath = rawPath.replaceAll('\\', '/')
|
|
const rootPrefix = `${root}/`
|
|
if (cssPath.startsWith(rootPrefix)) cssPath = cssPath.slice(rootPrefix.length)
|
|
return `${prefix}\\0dshx-css-module:${cssPath}${ending}`
|
|
},
|
|
)
|
|
if (/^[ \t]*\/\/(?:#region )? ?\\0dshx-css-module:(?:\/|[A-Za-z]:[\\/])/mu.test(client.code)) {
|
|
this.error('client.js contains a non-portable CSS module path')
|
|
}
|
|
},
|
|
}
|
|
|
|
/**
|
|
* Guard against the 0.3.7 regression class: the host entry must not ship a
|
|
* relative specifier that cannot resolve inside the published tarball.
|
|
*/
|
|
const hostPortableOutput: TsdownPlugin = {
|
|
name: 'dsh-better-chat-host-portable-output',
|
|
generateBundle(_options, output) {
|
|
for (const [fileName, chunk] of Object.entries(output)) {
|
|
if (chunk.type !== 'chunk') continue
|
|
const offenders = [
|
|
...chunk.code.matchAll(/from\s*["'](\.[^"']*\.ts)["']/g),
|
|
...chunk.code.matchAll(/from\s*["'](\.\.?\/[^"']*node_modules\/[^"']*)["']/g),
|
|
].map((match) => match[1])
|
|
if (offenders.length > 0) {
|
|
this.error(
|
|
`${fileName} contains relative specifiers that will not resolve in the published package: ` +
|
|
`${[...new Set(offenders)].join(', ')}. Relative imports must be bundled, not externalised.`,
|
|
)
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
export default bundle.map((config) => {
|
|
if (config.name === 'dsh-better-chat') {
|
|
const { external: _external, ...hostConfig } = config
|
|
return {
|
|
...hostConfig,
|
|
plugins: [
|
|
...(Array.isArray(hostConfig.plugins)
|
|
? hostConfig.plugins
|
|
: hostConfig.plugins === undefined
|
|
? []
|
|
: [hostConfig.plugins]),
|
|
hostPortableOutput,
|
|
],
|
|
// `neverBundle: true` keeps bare npm specifiers external and bundles
|
|
// relative imports; see the adapter's documentation for the 0.3.7
|
|
// ERR_MODULE_NOT_FOUND regression this prevents.
|
|
deps: { ...hostConfig.deps, neverBundle: true },
|
|
}
|
|
}
|
|
if (config.name !== 'dsh-better-chat/client') return config
|
|
const plugins = Array.isArray(config.plugins)
|
|
? config.plugins
|
|
: config.plugins === undefined
|
|
? []
|
|
: [config.plugins]
|
|
return { ...config, plugins: [...plugins, portableOutput] }
|
|
})
|