ekomat-mobilnoe-prilozhenie/scripts/build.js

29 lines
1.1 KiB
JavaScript

import { build } from 'esbuild'
import { cp, mkdir, readdir, rm, writeFile } from 'node:fs/promises'
const origin = process.env.PUBLIC_API_ORIGIN ?? ''
if (origin && new URL(origin).origin !== origin) {
throw new Error('PUBLIC_API_ORIGIN must be an origin without a trailing slash')
}
if (origin && !origin.startsWith('https://')) throw new Error('PUBLIC_API_ORIGIN requires HTTPS')
await rm('dist', { force: true, recursive: true })
await mkdir('dist')
for (const name of await readdir('web')) {
if (!['app.js', 'strings.js'].includes(name)) await cp('web/' + name, 'dist/' + name)
}
await build({
entryPoints: ['web/app.js'],
bundle: true,
format: 'esm',
target: ['safari16', 'chrome109'],
outfile: 'dist/app.js',
define: {
__API_ORIGIN__: JSON.stringify(origin),
__SENTRY_DSN__: JSON.stringify(process.env.PUBLIC_SENTRY_DSN ?? ''),
__SENTRY_RELEASE__: JSON.stringify(process.env.SENTRY_RELEASE ?? ''),
__SENTRY_ENVIRONMENT__: JSON.stringify(process.env.SENTRY_ENVIRONMENT ?? 'dev')
}
})
await writeFile('dist/build.json', JSON.stringify({ release: process.env.SENTRY_RELEASE ?? 'dev' }))