erratic-gate/build.js

61 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-10-29 04:54:24 +01:00
import * as esbuild from 'esbuild'
import { htmlPlugin } from '@craftamap/esbuild-plugin-html'
import { sassPlugin } from 'esbuild-sass-plugin'
2023-10-29 05:46:53 +01:00
import * as fs from 'fs'
2023-10-29 04:54:24 +01:00
const serve = process.env.ESBUILD_SERVE === '1'
2023-10-29 06:57:06 +01:00
const baseurl = process.env.ESBUILD_BASEURL || ''
2024-12-04 17:04:53 +01:00
const nodeEnv = process.env.NODE_ENV || 'production'
const isProd = nodeEnv !== 'development'
2023-10-29 06:28:40 +01:00
console.log(`Building with baseurl ${baseurl}`)
2023-10-29 04:54:24 +01:00
const ctx = await esbuild.context({
2023-10-29 06:28:40 +01:00
entryPoints: ['src/index.ts'],
minify: isProd,
2023-10-29 06:28:40 +01:00
bundle: true,
metafile: true,
splitting: isProd,
2023-10-29 06:28:40 +01:00
outdir: 'dist',
format: 'esm',
target: ['es2020'],
assetNames: 'assets/[name]',
chunkNames: 'chunks/[name]',
2023-10-29 06:28:40 +01:00
loader: {
'.svg': 'file'
},
define: {
'process.env.BASEURL': JSON.stringify(baseurl),
'process.env.NODE_ENV': JSON.stringify(nodeEnv)
2023-10-29 06:28:40 +01:00
},
plugins: [
htmlPlugin({
files: [
{
filename: 'index.html',
entryPoints: ['src/index.ts'],
favicon: 'public/favicon.ico',
htmlTemplate: 'public/index.html',
scriptLoading: 'module'
}
]
}),
sassPlugin({})
],
publicPath: baseurl
2023-10-29 04:54:24 +01:00
})
if (serve) {
2024-11-27 10:41:36 +01:00
await ctx.watch()
const { port, host } = await ctx.serve({
servedir: 'dist',
fallback: 'dist/index.html'
})
2023-10-29 06:28:40 +01:00
console.log(`Serving on ${host}:${port}`)
2023-10-29 05:46:53 +01:00
} else {
2023-10-29 06:28:40 +01:00
await ctx.rebuild()
await ctx.dispose()
fs.cpSync('./dist/index.html', './dist/404.html') // Needed to please github pages
console.log(`Project bundled successfully`)
2023-10-29 04:54:24 +01:00
}