2026-09-22 20:15:00 +08:00
|
|
|
import { build } from 'esbuild'
|
|
|
|
|
import { resolve, dirname } from 'path'
|
|
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
|
import * as dotenv from 'dotenv'
|
|
|
|
|
import * as fs from 'fs'
|
|
|
|
|
import * as path from 'path'
|
|
|
|
|
|
|
|
|
|
// Load production environment variables
|
|
|
|
|
dotenv.config({ path: '.env.prod' })
|
|
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
|
|
|
const __dirname = dirname(__filename)
|
|
|
|
|
|
|
|
|
|
async function buildWebClientProd() {
|
|
|
|
|
try {
|
|
|
|
|
console.log('Building webclient with production configuration...')
|
|
|
|
|
|
|
|
|
|
// Config is now generated by the prebuild step (generate-prod-env.ts)
|
|
|
|
|
// No need to copy config files here
|
|
|
|
|
|
|
|
|
|
// Set NODE_ENV to production for the build
|
|
|
|
|
process.env.NODE_ENV = 'production'
|
|
|
|
|
|
|
|
|
|
// Baked DEFAULT base URL. This only sets the fallback; getBaseUrl() still
|
2026-10-26 00:50:43 +08:00
|
|
|
// resolves window.replypierBaseUrl / window.APP_CONFIG at runtime, so an
|
2026-09-22 20:15:00 +08:00
|
|
|
// env change on a deployment needs no rebuild of this artifact.
|
2026-10-26 00:50:43 +08:00
|
|
|
const apiUrl = process.env.VITE_API_URL || 'https://api.replypier.site/api/v1'
|
2026-09-22 20:15:00 +08:00
|
|
|
console.log('Using API URL:', apiUrl)
|
|
|
|
|
|
2026-10-26 00:50:43 +08:00
|
|
|
const sourceFile = resolve(dirname(__dirname), 'src/webclient/replypier.js')
|
2026-09-22 20:15:00 +08:00
|
|
|
|
|
|
|
|
await build({
|
|
|
|
|
entryPoints: [sourceFile],
|
|
|
|
|
bundle: true,
|
|
|
|
|
minify: true,
|
2026-10-26 00:50:43 +08:00
|
|
|
outfile: resolve(dirname(__dirname), 'public/webclient/replypier.min.js'),
|
2026-09-22 20:15:00 +08:00
|
|
|
format: 'iife',
|
|
|
|
|
target: ['es2015'],
|
|
|
|
|
loader: {
|
|
|
|
|
'.svg': 'text',
|
|
|
|
|
},
|
|
|
|
|
define: {
|
|
|
|
|
'process.env.NODE_ENV': '"production"',
|
2026-10-26 00:50:43 +08:00
|
|
|
'__REPLYPIER_API_URL__': JSON.stringify(apiUrl),
|
2026-09-22 20:15:00 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Copy output to dist/webclient directory
|
2026-10-26 00:50:43 +08:00
|
|
|
const publicWebclientPath = resolve(dirname(__dirname), 'public/webclient/replypier.min.js')
|
2026-09-22 20:15:00 +08:00
|
|
|
const distWebclientDir = resolve(dirname(__dirname), 'dist/webclient')
|
2026-10-26 00:50:43 +08:00
|
|
|
const distWebclientPath = resolve(distWebclientDir, 'replypier.min.js')
|
2026-09-22 20:15:00 +08:00
|
|
|
|
|
|
|
|
// Ensure dist/webclient directory exists
|
|
|
|
|
if (!fs.existsSync(distWebclientDir)) {
|
|
|
|
|
fs.mkdirSync(distWebclientDir, { recursive: true })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy the built file to dist/webclient
|
|
|
|
|
fs.copyFileSync(publicWebclientPath, distWebclientPath)
|
|
|
|
|
console.log('Production web client copied to dist/webclient/')
|
|
|
|
|
|
|
|
|
|
console.log('Production web client built successfully!')
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Production build failed:', error)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buildWebClientProd()
|