-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.js
More file actions
187 lines (162 loc) · 4.8 KB
/
server.js
File metadata and controls
187 lines (162 loc) · 4.8 KB
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
import * as path from "path";
import * as http from "http";
import { URL } from "url";
import * as fs from "fs/promises";
import * as esbuild from "esbuild";
import getPort from "get-port";
import { default as chalk } from "chalk";
import fastGlob from "fast-glob";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const { bold, underline } = chalk;
// Custom resolver that externalizes http:// and https:// imports
const httpExternalResolver = {
name: "http-external",
setup(build) {
// Mark all paths starting with "http://" or "https://" as external
build.onResolve({ filter: /^https?:\/\// }, (args) => {
return { path: args.path, external: true };
});
},
};
// Custom resolver that rewrites react->preact/compat
const preactResolver = {
name: "preact",
setup(build) {
build.onResolve({ filter: /^react-dom\/test-utils$/ }, (_args) => {
return { path: require.resolve("preact/test-utils") };
});
build.onResolve({ filter: /^react-dom$/ }, (_args) => {
return { path: require.resolve("preact/compat") };
});
build.onResolve({ filter: /^react$/ }, (_args) => {
return { path: require.resolve("preact/compat") };
});
},
};
function getDefaultConfig() {
return {
bundle: true,
format: "esm",
target: ["es2020"],
};
}
function getExtraConfig(preact) {
let jsxConfig = {};
const plugins = [httpExternalResolver];
if (preact) {
plugins.push(preactResolver);
jsxConfig = {
jsxFactory: "h",
jsxFragment: "Fragment",
};
}
return {
plugins,
...jsxConfig,
};
}
export async function bundle({ glob, preact, outdir }) {
const entryPoints = await fastGlob(glob);
const printLocation = glob.map((g) => underline(path.join(process.cwd(), g))).join(", ");
try {
console.log(`ESBuild bundling ${printLocation} into ${underline(outdir)} directory`);
await esbuild.build({
entryPoints: entryPoints,
outdir,
...getDefaultConfig(),
...getExtraConfig(preact),
});
} catch (err) {
console.error("Error occurred during bundling", err);
// eslint-disable-next-line no-process-exit
process.exit(1);
}
}
export async function start({ dir, ext, glob, preact }) {
const proxyPort = await getPort({ port: 2222 });
const esbuildPort = await getPort({ port: 2221 });
let entryPoints;
let printLocation;
if (dir) {
// Deprecated options code path
ext = ext || [".js", ".ts"];
const pluginDir = path.join(process.cwd(), dir);
const allFiles = await fs.readdir(pluginDir);
entryPoints = allFiles
.filter((filename) => {
return ext.includes(path.extname(filename));
})
.map((filename) => {
return path.join(dir, filename);
});
printLocation = underline(pluginDir);
} else {
entryPoints = await fastGlob(glob);
printLocation = glob.map((g) => underline(path.join(process.cwd(), g))).join(", ");
}
const esbuildServeConfig = {
port: esbuildPort,
};
const esbuildConfig = {
entryPoints: entryPoints,
...getDefaultConfig(),
...getExtraConfig(preact),
};
const { host, port } = await esbuild.serve(esbuildServeConfig, esbuildConfig);
const proxyServer = http.createServer((req, res) => {
const { pathname, searchParams } = new URL(req.url, `http://${req.headers.host}`);
if (searchParams.has("dev")) {
res.writeHead(200, {
"content-type": "application/javascript",
"access-control-allow-origin": "*",
});
res.end(templateForPathname(pathname));
return;
}
const options = {
hostname: host,
port: port,
path: req.url,
method: req.method,
headers: req.headers,
};
// Forward each incoming request to esbuild
const proxyReq = http.request(options, (proxyRes) => {
res.writeHead(proxyRes.statusCode, {
...proxyRes.headers,
"access-control-allow-origin": "*",
});
proxyRes.pipe(res, { end: true });
});
// Forward the body of the request to esbuild
req.pipe(proxyReq, { end: true });
});
proxyServer.listen(proxyPort, () => {
console.log(`ESBuild for ${printLocation} on port ${underline(esbuildPort)}`);
console.log(`Development server started on ${bold(`http://127.0.0.1:${proxyPort}/`)}`);
});
}
function templateForPathname(pathname) {
return `// Development plugin with auto-reload
class Plugin {
constructor() {
this.plugin = null;
}
async render(container) {
const cacheBust = Date.now();
const modulePath = "${pathname}?" + cacheBust;
const { default: RevealMarket } = await import(modulePath);
this.plugin = new RevealMarket();
await this.plugin?.render?.(container);
}
draw(ctx) {
this.plugin?.draw?.(ctx);
}
destroy() {
this.plugin?.destroy?.();
}
}
export default Plugin;
`;
}