mtcute/packages/node/build.config.js

48 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-08-21 11:05:07 +03:00
import { fileURLToPath } from 'node:url'
2024-11-16 16:00:12 +03:00
/** @type {import('@fuman/build/vite').CustomBuildConfig} */
2024-08-21 11:05:07 +03:00
export default () => {
const clientId = fileURLToPath(new URL('./src/client.ts', import.meta.url))
return {
2024-11-16 16:00:12 +03:00
viteConfig: {
build: {
rollupOptions: {
external: ['@mtcute/crypto-node'],
},
},
},
pluginsPre: [
2024-08-21 11:05:07 +03:00
{
// very much a crutch, but it works
// i couldn't figure out a way to hook into the esm->cjs transform,
// so i'm just replacing the await import with require and then back
name: 'mtcute-node-build-plugin',
transform(code, id) {
if (id === clientId) {
return code.replace('await import(', 'require(')
}
return code
},
generateBundle(output, bundle) {
if (output.format !== 'es') return
let found = false
for (const chunk of Object.values(bundle)) {
if (chunk.code.match(/require\("@mtcute\/crypto-node"\)/)) {
found = true
chunk.code = chunk.code.replace('require("@mtcute/crypto-node")', '(await import("@mtcute/crypto-node"))')
2024-08-21 11:05:07 +03:00
}
}
if (!found) {
throw new Error('Could not find crypto-node import')
}
},
},
],
}
}