mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-09 00:33:20 +08:00
43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
import { dirname, resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { build } from 'esbuild';
|
|
|
|
const sourceDirectory = dirname(fileURLToPath(import.meta.url));
|
|
const packageRoot = resolve(sourceDirectory, '../..');
|
|
const outputPath = resolve(packageRoot, 'lib/client.js');
|
|
const loaderId = process.env.DSH_IM_CLIENT_ID ?? '@xmanrui/dsh-im';
|
|
const logoPath = resolve(packageRoot, 'assets/logo-icon.png');
|
|
const logoDataUrl = `data:image/png;base64,${(await readFile(logoPath)).toString('base64')}`;
|
|
|
|
const result = await build({
|
|
entryPoints: [resolve(sourceDirectory, 'index.js')],
|
|
bundle: true,
|
|
format: 'cjs',
|
|
platform: 'browser',
|
|
target: ['chrome100'],
|
|
external: ['react'],
|
|
define: {
|
|
'globalThis.__DSH_IM_LOGO_DATA_URL__': JSON.stringify(logoDataUrl),
|
|
},
|
|
write: false,
|
|
minify: process.env.NODE_ENV === 'production',
|
|
legalComments: 'none',
|
|
});
|
|
const bundled = result.outputFiles?.[0]?.text;
|
|
if (!bundled) throw new Error('esbuild did not produce a client bundle');
|
|
|
|
const wrapped = `window.__ModuleLoader__.load({
|
|
id: ${JSON.stringify(loaderId)},
|
|
factory: (require) => {
|
|
var module = { exports: {} };
|
|
var exports = module.exports;
|
|
${bundled}
|
|
return module.exports;
|
|
}
|
|
});
|
|
`;
|
|
await mkdir(dirname(outputPath), { recursive: true });
|
|
await writeFile(outputPath, wrapped, 'utf8');
|
|
console.log(`Wrote ${outputPath}`);
|