esbuild.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* eslint-disable @typescript-eslint/no-var-requires */
  2. const esbuild = require('esbuild');
  3. const path = require('path');
  4. const commandArgs = process.argv.slice(2);
  5. const nativeNodeModulesPlugin = () => ({
  6. name: 'native-node-modules',
  7. setup(build) {
  8. // If a ".node" file is imported within a module in the "file" namespace, resolve
  9. // it to an absolute path and put it into the "node-file" virtual namespace.
  10. build.onResolve({ filter: /\.node$/, namespace: 'file' }, (args) => {
  11. const resolvedId = require.resolve(args.path, {
  12. paths: [args.resolveDir],
  13. });
  14. if (resolvedId.endsWith('.node')) {
  15. return {
  16. path: resolvedId,
  17. namespace: 'node-file',
  18. };
  19. }
  20. return {
  21. path: resolvedId,
  22. };
  23. });
  24. // Files in the "node-file" virtual namespace call "require()" on the
  25. // path from esbuild of the ".node" file in the output directory.
  26. build.onLoad({ filter: /.*/, namespace: 'node-file' }, (args) => ({
  27. contents: `
  28. import path from ${JSON.stringify(args.path)}
  29. try { module.exports = require(path) }
  30. catch {}
  31. `,
  32. resolveDir: path.dirname(args.path),
  33. }));
  34. // If a ".node" file is imported within a module in the "node-file" namespace, put
  35. // it in the "file" namespace where esbuild's default loading behavior will handle
  36. // it. It is already an absolute path since we resolved it to one above.
  37. build.onResolve({ filter: /\.node$/, namespace: 'node-file' }, (args) => ({
  38. path: args.path,
  39. namespace: 'file',
  40. }));
  41. // Tell esbuild's default loading behavior to use the "file" loader for
  42. // these ".node" files.
  43. const opts = build.initialOptions;
  44. opts.loader = opts.loader || {};
  45. opts.loader['.node'] = 'file';
  46. },
  47. });
  48. /* Bundle server */
  49. esbuild.build({
  50. entryPoints: ['./src/server.ts'],
  51. bundle: true,
  52. platform: 'node',
  53. target: 'node18',
  54. external: ['pg-native'],
  55. sourcemap: commandArgs.includes('--sourcemap'),
  56. watch: commandArgs.includes('--watch'),
  57. outfile: 'dist/server.bundle.js',
  58. plugins: [nativeNodeModulesPlugin()],
  59. logLevel: 'info',
  60. minifySyntax: true,
  61. minifyWhitespace: true,
  62. });
  63. const glob = require('glob');
  64. /* Migrations */
  65. const migrationFiles = glob.sync('./src/config/migrations/*.ts');
  66. esbuild.buildSync({
  67. entryPoints: migrationFiles,
  68. platform: 'node',
  69. target: 'node18',
  70. minify: false,
  71. outdir: 'dist/config/migrations',
  72. logLevel: 'info',
  73. format: 'cjs',
  74. minifySyntax: true,
  75. minifyWhitespace: true,
  76. });