blob: 2a7dca497c3f6b9462621e983e235410e3104569 [file] [log] [blame]
Frank Borden42c1a452022-08-11 16:27:20 +02001import { esbuildPlugin } from "@web/dev-server-esbuild";
2import cors from "@koa/cors";
3
4/** @type {import('@web/dev-server').DevServerConfig} */
5export default {
6 port: 8081,
7 plugins: [
8 esbuildPlugin({
9 ts: true,
10 target: "es2020",
11 tsconfig: "polygerrit-ui/app/tsconfig.json",
12 }),
13 ],
14 nodeResolve: true,
15 rootDir: "polygerrit-ui/app",
16 middleware: [
17 // Allow files served from the localhost domain to be used on any domain
18 // (ex: gerrit-review.googlesource.com), which happens during local
19 // development with Gerrit FE Helper extension.
20 cors({ origin: "*" }),
21 // The issue solved here is that our production index.html does not load
22 // 'gr-app.js' as an ESM module due to our build process, but in development
23 // all our source code is written as ESM modules. When using the Gerrit FE
24 // Helper extension to see our local changes on a production site we see a
25 // syntax error due to this mismatch. The trick used to fix this is to
26 // rewrite the response for 'gr-app.js' to be a dynamic import() statement
27 // for a fake file 'gr-app.mjs'. This fake file will be loaded as an ESM
28 // module and when the server receives the request it returns the real
29 // contents of 'gr-app.js'.
30 async (context, next) => {
31 const isGrAppMjs = context.url.includes("gr-app.mjs");
32 if (isGrAppMjs) {
33 // Load the .ts file of the entrypoint instead of .js to trigger esbuild
34 // which will convert every .ts file to .js on request.
35 context.url = context.url.replace("gr-app.mjs", "gr-app.ts");
36 }
37
38 // Pass control to the next middleware which eventually loads the file.
39 // see https://koajs.com/#cascading
40 await next();
41
42 if (!isGrAppMjs && context.url.includes("gr-app.js")) {
43 context.body = "import('./gr-app.mjs')";
44 }
45 },
46 ],
47};