/
esbuild.config.js
205 lines (181 loc) · 4.44 KB
/
esbuild.config.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* eslint-disable require-await */
/* eslint-disable no-promise-executor-return */
/* eslint-disable no-constant-condition */
console.log("ESBuild config loaded");
import http from "http";
import {lessLoader} from "esbuild-plugin-less";
import {clean} from "esbuild-plugin-clean";
import {copy} from "esbuild-plugin-copy";
import {merge} from "webpack-merge";
import {build, context} from "esbuild";
import {zip} from "zip-a-folder";
import manifest from "./uxp/manifest.json" with {type: "json"};
import chokidar from "chokidar";
import {typecheckPlugin} from "@jgoz/esbuild-plugin-typecheck";
const mode = process.argv[2];
//console.log(copy);
console.log(`Mode: ${mode}`);
let isProduction = false;
switch (mode) {
case "--build":
isProduction = true;
break;
case "--watch":
break;
default:
console.error(`unknown mode: ${mode}`);
process.exit(1);
}
// store request to send reload signal later
let reloadResult;
/** @type {import("esbuild").Plugin} */
let reloadPlugin = {
name: "reloadPlugin",
setup(build) {
// eslint-disable-next-line require-await
build.onEnd(async result => {
if (!reloadResult) {
console.log("Nothing to reload.");
return;
}
console.log("Sending reload signal...");
reloadResult.statusCode = 200;
reloadResult.setHeader("Content-Type", "text/plain");
reloadResult.end("Reload now!");
});
},
};
// reload code injection
const reloadCode = `
(async () => {
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
while (true) {
try {
await fetch("http://127.0.0.1:3033");
location.reload();
}catch(err) {
console.log(err);
await sleep(500);
}
}
})();
`;
/** @type {import("esbuild").CommonOptions} */
const esBuildConfigBase = {
entryPoints: ["./src/shared/classes/Main.ts"],
logLevel: "info",
bundle: true,
minify: false,
sourcemap: "inline",
platform: "browser",
target: ["es2022", "node18"],
external: ["photoshop", "uxp", "fs", "os"],
outfile: "./dist/index.js",
// adds less plugin
plugins: [
clean({
patterns: "./dist",
cleanOn: "start",
}),
copy({
assets: {
from: "./uxp/**",
to: "./",
},
watch: true,
copyOnStart: true,
}),
lessLoader(),
typecheckPlugin({
watch: true,
}),
],
footer: {
// fixes sourcemap issue
js: reloadCode + "\n//# sourceURL=webpack-internal:///./src/",
},
// Disable some features/minification to make it work in UXP
supported: {
// JavaScript
"top-level-await": false,
// CSS... because UXP is special
"hex-rgba": false,
"inline-style": false,
"inset-property": false,
"is-pseudo-class": false,
"modern-rgb-hsl": false,
"nesting": false,
"rebecca-purple": false,
},
};
/** @type {import("esbuild").CommonOptions} */
const esBuildConfigProduction = {
minify: true,
keepNames: true,
sourcemap: false,
footer: {js: ""},
legalComments: "inline",
};
const config = merge(
esBuildConfigBase,
isProduction && esBuildConfigProduction,
);
if (!isProduction) {
config.plugins.push(reloadPlugin);
}
(async () => {
try {
const start = Date.now();
if (isProduction) {
// build plugin
await build(config);
// pack plugin into installer
await zip("./dist", `./installer/${manifest.name}_${manifest.id}_v${manifest.version.replace(/\./gm, "-")}.ccx`);
} else {
const hostname = "127.0.0.1";
const port = 3033;
const server = http.createServer((req, res) => {
console.log("Waiting for reload signal...");
reloadResult = res;
});
server.setTimeout(0);
server.timeout = 0;
server.keepAliveTimeout = 0;
server.requestTimeout = 0;
server.headersTimeout = 0;
server.listen(port, hostname, () => {
console.log(`Reload server running at http://${hostname}:${port}/`);
});
// prepare watcher
let ctx = await context(config);
// add some extra files to watch for
chokidar.watch("./uxp/**",
{
// only after change happened
ignoreInitial: true,
// polling to group changes
usePolling: true,
interval: 500,
},
).on("all", async (event, path) => {
console.log(event, path);
// cancel already running build
await ctx.cancel();
// dispose old context
await ctx.dispose();
// create new context
ctx = await context(config);
// start watching again
await ctx.watch();
});
// start watching
await ctx.watch();
}
console.log("ESBuild finished: " + (Date.now() - start) + "ms");
} catch (err) {
console.error(err);
process.exit(1);
}
})();