-
Notifications
You must be signed in to change notification settings - Fork 359
/
esbuild.js
117 lines (107 loc) · 3.6 KB
/
esbuild.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const fs = require("fs")
const { context } = require("esbuild")
const dotenv = require("dotenv")
dotenv.config()
const isDev = process.argv[2] === "--dev"
const isProdBuild = process.argv[2] === "--build"
let hashIndexPlugin = {
name: "hash-index-plugin",
setup(build) {
build.onStart(() => {
const files = fs.readdirSync("app/public/dist/client")
files.forEach((file) => {
// remove old files
if (file.startsWith("index-") && file.endsWith(".js")) {
fs.unlinkSync(`app/public/dist/client/${file}`)
}
if (file.startsWith("index-") && file.endsWith(".css")) {
fs.unlinkSync(`app/public/dist/client/${file}`)
}
})
})
build.onEnd((result) => {
if (result.errors.length > 0) {
console.log(`build ended with ${result.errors.length} errors`)
}
updateHashedFilesInIndex()
})
}
}
context({
entryPoints: ["./app/public/src/index.tsx"],
entryNames: "[dir]/[name]-[hash]",
assetNames: "[dir]/[name]-[hash]",
outfile: "app/public/dist/client/index.js",
external: ["assets/*"],
bundle: true,
metafile: true,
minify: isProdBuild,
sourcemap: isProdBuild,
plugins: [hashIndexPlugin],
target: "es2016",
define: {
"process.env.FIREBASE_API_KEY": `"${process.env.FIREBASE_API_KEY}"`,
"process.env.FIREBASE_AUTH_DOMAIN": `"${process.env.FIREBASE_AUTH_DOMAIN}"`,
"process.env.FIREBASE_PROJECT_ID": `"${process.env.FIREBASE_PROJECT_ID}"`,
"process.env.FIREBASE_STORAGE_BUCKET": `"${process.env.FIREBASE_STORAGE_BUCKET}"`,
"process.env.FIREBASE_MESSAGING_SENDER_ID": `"${process.env.FIREBASE_MESSAGING_SENDER_ID}"`,
"process.env.FIREBASE_APP_ID": `"${process.env.FIREBASE_APP_ID}"`,
"process.env.DISCORD_SERVER": `"${process.env.DISCORD_SERVER}"`,
"process.env.MIN_HUMAN_PLAYERS": `"${process.env.MIN_HUMAN_PLAYERS}"`,
}
})
.then((context) => {
if (isDev) {
// Enable watch mode
context.watch()
} else {
// Build once and exit if not in watch mode
context.rebuild().then((result) => {
if (result.metafile) {
// use https://esbuild.github.io/analyze/ to analyse
fs.writeFileSync(
"app/public/dist/client/esbuild.meta.json",
JSON.stringify(result.metafile)
)
}
context.dispose()
})
}
})
.catch((error) => {
console.error(error)
process.exit(1)
})
function updateHashedFilesInIndex() {
//update hash in index.html
const fs = require("fs")
const path = require("path")
const distDir = path.join(__dirname, "app/public/dist/client")
const htmlFile = path.join(__dirname, "app/views/index.html")
const htmlOutputFile = path.join(distDir, "index.html")
// Find the hashed script file
const scriptFile = fs
.readdirSync(distDir)
.find((file) => file.startsWith("index-") && file.endsWith(".js"))
const cssFile = fs
.readdirSync(distDir)
.find((file) => file.startsWith("index-") && file.endsWith(".css"))
if (scriptFile) {
// Read the HTML file
let htmlContent = fs.readFileSync(htmlFile, "utf8")
// Replace the placeholder with the actual script tag
htmlContent = htmlContent
.replace(
'<script src="index.js" defer></script>',
`<script src="${scriptFile}" defer></script>`
)
.replace(
`<link rel="stylesheet" type="text/css" href="index.css" />`,
`<link rel="stylesheet" type="text/css" href="${cssFile}">`
)
// Write the updated HTML back to the file
fs.writeFileSync(htmlOutputFile, htmlContent, "utf8")
} else {
console.error("Hashed entry files not found.")
}
}