Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
blob: 5e160031d080aaaf13d8cb9295a6912f1b1226cc (plain)
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
const { createServer } = require('esbuild-server');
const path = require('node:path');
const fs = require('node:fs');
const { IncomingMessage } = require('node:http');

class Request extends IncomingMessage {
  constructor(socket) {
    super(socket);
  }

  get url() {
    console.log('getter called', this._url);
    return this._url;
  }

  set url(val) {
    console.log('setter called', val);
    this._url = val;
  }
}


const resolveImportedSvg = {
  name: 'resolveImportedSvg',
  setup(build) {
    build.onResolve({ filter: /\.svg$/ }, args => {
      return {
        path: path.resolve('public', args.path),
      };
    });
  }
}

const resolveSvgImports = {
  name: 'resolveSvgImports',
  setup(build) {
    build.onStart(() => {
      fs.rmSync(path.resolve(build.initialOptions.outdir), { recursive: true, force: true });
    });

    build.onResolve({ filter: /\.svg$/ }, args => {
      return {
        path: path.resolve('public', args.path),
      };
    });
  }
}

const server = createServer(
  {
    bundle: true,
    define: {
      'env': `"${process.env.NODE_ENV || 'dev'}"`,
    },
    entryPoints: ['src/index.js', 'src/soldier_record_block.js', 'src/map.js'],
    outdir: 'build',
    // ...(process.env.NODE_ENV !== 'test') && {
    //   outdir: 'build'
    // },
    plugins: [resolveSvgImports],
    loader: {
      '.svg': 'file'
    },
    assetNames: 'assets/images/[name]-[hash]',
  },
  {
    static: 'public',
    ...(process.env.NODE_ENV === 'test') && {
      port: 3005,
      injectLiveReload: false,
      watch: false,
      // http: { IncomingMessage: Request }
    }
  }
);

const buildStart = Date.now();
server
  .start()
  .then((d) => {
    console.log(`Build completed in ${Date.now() - buildStart}ms`);
  })
  .catch(() => {
    console.error('Build failed');
  });
console.log(`${process.env.NODE_ENV === 'test' ? 'Test' : 'Development'} server running at ${server.url}`);