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:28:40 +01:00
|
|
|
const baseurl = process.env.ESBUILD_BASEURL
|
|
|
|
|
|
|
|
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: !serve,
|
|
|
|
bundle: true,
|
|
|
|
metafile: true,
|
|
|
|
splitting: true,
|
|
|
|
outdir: 'dist',
|
|
|
|
format: 'esm',
|
|
|
|
target: ['es2020'],
|
|
|
|
assetNames: 'assets/[name]-[hash]',
|
|
|
|
loader: {
|
|
|
|
'.svg': 'file'
|
|
|
|
},
|
|
|
|
define: {
|
|
|
|
'process.env.BASEURL': JSON.stringify(baseurl)
|
|
|
|
},
|
|
|
|
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) {
|
2023-10-29 06:28:40 +01:00
|
|
|
const { port, host } = await ctx.serve({ servedir: 'dist' })
|
|
|
|
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
|
|
|
}
|