Web Dev Solutions

Catalin Mititiuc

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

// const { IncomingMessage } = require('node:http');
// const { URL } = require('node:url');
// const path = require('node:path');

// 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 propDesc = Object.getOwnPropertyDescriptor(URL.prototype, 'pathname');

// // console.log('propDesc before', propDesc);

// Object.defineProperty(URL.prototype, 'pathname', {
//   __proto__: null,
//   get: function() {
//     const pathname = propDesc.get.call(this);
//     console.log('get pathname', path.normalize(path.join('public', pathname)));

//     return pathname;
//   },
//   // set: function(val) {
//   //   console.log('set pathname', val);
//   //   propDesc.set.call(this, val);
//   //   // this._pathname = val;
//   // },
// });

// console.log('path', path.normalize(path.join('public', 'assets/css/damage_block.css')))

let version;

const svgUseCacheBust = {
  name: 'svgUseCacheBust',
  setup(build) {
    // build.onStart(() => {
    //   version = Math.random();
    //   console.log('svg use cache bust online', version);
      // const file = fs.readFileSync('./public/assets/images/scenario-side_show.svg', {encoding: 'utf-8'});
      // console.log('file', file);
    // });

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

    build.onEnd(result => {
      // console.log('metafile', JSON.stringify(result.metafile, null, 2));

      // for (k in result.metafile.outputs) {
      //   console.log(result.metafile.outputs[k]);
      // }

      // const filename = Object.keys(result.metafile.outputs).find(key => {
      //   return result.metafile.outputs[key].inputs['public/assets/images/scenario-side_show.svg'];
      // });

      // console.log('filename', filename);

      // const file = fs.readFileSync(filename, { encoding: 'utf-8' });

      // const mapsheets = Object.keys(result.metafile.outputs).find(key => {
      //   return result.metafile.outputs[key].inputs['public/assets/images/mapsheets.svg'];
      // });

      // console.log('mapsheets', path.basename(mapsheets));

      // const newFile = file.replaceAll('mapsheets.svg', path.basename(mapsheets));

      // fs.writeFileSync(filename, newFile);
    });
  }
}

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

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}`);