๐Ÿ“ฆ Kong / insomnia

๐Ÿ“„ esbuild.ts ยท 70 lines
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
70import fs from 'node:fs';

import { analyzeMetafile, build, type BuildOptions, context } from 'esbuild';

const isProd = Boolean(process.env.NODE_ENV === 'production');
const watch = Boolean(process.env.ESBUILD_WATCH);
const isDebug = Boolean(process.env.DEBUG);
const version = process.env.VERSION || 'dev';
const config: BuildOptions = {
  outfile: './dist/index.js',
  bundle: true,
  metafile: isDebug,
  platform: 'node',
  minify: isProd,
  target: 'node22',
  sourcemap: true,
  format: 'cjs',
  tsconfig: 'tsconfig.json',
  alias: {
    electron: '../insomnia/send-request/electron',
  },
  plugins: [
    // taken from https://github.com/tjx666/awesome-vscode-extension-boilerplate/blob/main/scripts/esbuild.ts
    {
      name: 'umd2esm',
      setup(build) {
        build.onResolve({ filter: /^(vscode-.*|estree-walker|jsonc-parser)/ }, args => {
          const pathUmdMay = require.resolve(args.path, {
            paths: [args.resolveDir],
          });
          // Call twice the replace is to solve the problem of the path in Windows
          const pathEsm = pathUmdMay.replace('/umd/', '/esm/').replace('\\umd\\', '\\esm\\');
          return { path: pathEsm };
        });
      },
    },
  ],
  define: {
    'process.env.DEFAULT_APP_NAME': JSON.stringify(isProd ? 'Insomnia' : 'insomnia-app'),
    'process.env.VERSION': JSON.stringify(isProd ? version : 'dev'),
    '__DEV__': JSON.stringify(!isProd),
  },
  // node-llama-cpp is not included here because inso does not need it
  external: ['@getinsomnia/node-libcurl', 'fsevents', 'mocha'],
  entryPoints: ['./src/index.ts'],
};

if (watch) {
  async function watch() {
    const ctx = await context(config);
    await ctx.watch();
  }
  watch();
} else {
  if (isDebug) {
    async function buildWithDebug() {
      const result = await build(config);

      if (result.metafile) {
        fs.mkdirSync('./artifacts', { recursive: true });
        fs.writeFileSync('./artifacts/meta.json', JSON.stringify(result.metafile));
        fs.writeFileSync('./artifacts/bundle-analysis.log', await analyzeMetafile(result.metafile));
      }
    }

    buildWithDebug();
  }
  build(config);
}