-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.js
122 lines (111 loc) · 4.22 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
118
119
120
121
122
/* eslint-disable no-useless-escape */
// esbuild.js
import { execSync } from "child_process"
import { build, context } from "esbuild"
import fs from "fs"
import figlet from "figlet"
import Compress from "compress-images"
const INPUT_IMAGE_PATH = "src/images/**/*.{jpg,JPG,jpeg,JPEG,png,svg,gif}"
const OUTPUT_IMAGE_PATH = "public/images/"
const pkg = JSON.parse(fs.readFileSync("./package.json"))
const watch = process.argv.includes("--watch")
const dev = process.argv.includes("--dev") || process.env.NODE_ENV === "development"
const banner = "/* eslint-disable linebreak-style */\n" +
"/*\n" +
figlet.textSync("Up Urban Climbing", { horizontalLayout: "full", font: "Big" }) +
"\n" +
` v${pkg.version}\n\n\n` +
` ${pkg.description}\n\n` +
` Author: ${pkg.author}\n` +
` License: ${pkg.license}\n` +
` Repository: ${pkg.repository.url}\n\n` +
` Build date: ${new Date().toUTCString()}\n\n` +
" This program is free software: you can redistribute it and/or modify */\n\n"
const buildOptions = {
entryPoints: ["src/app.ts"],
bundle: true,
minify: dev ? false : true,
sourcemap: true,
color: true,
outdir: "public/dist",
target: ['chrome58', 'firefox57', 'safari11', 'edge18'],
banner: {
js: banner
},
plugins: [
{
name: "TypeScriptDeclarationsPlugin",
setup(build) {
build.onEnd((result) => {
if (result.errors.length > 0) {
console.log("\u001b[31mESM Build failed!\u001b[37m")
process.exit(1)
}
execSync("npx tsc --emitDeclarationOnly")
console.log("\u001b[36mTypeScript declarations generated!\u001b[37m")
// copy src/index.html to public/index.html
fs.copyFileSync("src/index.html", "public/index.html")
})
}
},
{
name: "CompressImagesPlugin",
setup(build) {
build.onEnd(() => {
console.log("\u001b[36mCompressing images...\u001b[37m")
Compress(INPUT_IMAGE_PATH, OUTPUT_IMAGE_PATH, {
compress_force: false,
statistic: true,
autoupdate: true,
}, false,
{ jpg: { engine: "mozjpeg", command: ["-quality", "70"] } },
{ png: { engine: "false", command: false } },
{ svg: { engine: "false", command: false } },
{ gif: { engine: "false", command: false } },
function (error, completed, statistic) {
console.log("-------------");
console.log(error);
console.log(completed);
console.log(statistic);
console.log("-------------");
}
)
})
}
}
]
}
if (dev) {
const ctx = await context(buildOptions)
if (watch) await ctx.watch().then(() => {
console.log("\u001b[36mWatching...\u001b[37m")
})
// Enable serve mode
await ctx.serve({
servedir: "public",
port: 8080,
onRequest: (args) => {
if (args.path === "/") {
args.path = "/index.html"
}
console.log(`\u001b[36m${args.method} ${args.path}\u001b[37m`)
}
}).then((server) => {
console.log(`\u001b[36mServing at http://localhost:${server.port}\u001b[37m`)
// Open browser
switch (process.platform) {
case "darwin":
execSync(`open http://localhost:${server.port}`)
break
case "win32":
execSync(`start http://localhost:${server.port}`)
break
default:
execSync(`xdg-open http://localhost:${server.port}`)
}
})
} else {
await build(buildOptions)
}
console.log("\u001b[36mESM Build succeeded!\u001b[37m")
// Enable watch mode