‘liusuyi’
2023-08-09 161b9318e345c8a0c9cdc133b33a1c759495f323
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
'use strict';
 
const { createReadStream } = require('fs');
const { join } = require('path');
 
const clientBasePath = join(__dirname, '..', '..', 'client');
 
function routes(server) {
  const app = server.app;
  const middleware = server.middleware;
  const options = server.options;
 
  app.get('/__webpack_dev_server__/live.bundle.js', (req, res) => {
    res.setHeader('Content-Type', 'application/javascript');
 
    createReadStream(join(clientBasePath, 'live.bundle.js')).pipe(res);
  });
 
  app.get('/__webpack_dev_server__/sockjs.bundle.js', (req, res) => {
    res.setHeader('Content-Type', 'application/javascript');
 
    createReadStream(join(clientBasePath, 'sockjs.bundle.js')).pipe(res);
  });
 
  app.get('/webpack-dev-server.js', (req, res) => {
    res.setHeader('Content-Type', 'application/javascript');
 
    createReadStream(join(clientBasePath, 'index.bundle.js')).pipe(res);
  });
 
  app.get('/webpack-dev-server/invalidate', (_req, res) => {
    server.invalidate();
    res.end();
  });
 
  app.get('/webpack-dev-server/*', (req, res) => {
    res.setHeader('Content-Type', 'text/html');
 
    createReadStream(join(clientBasePath, 'live.html')).pipe(res);
  });
 
  app.get('/webpack-dev-server', (req, res) => {
    res.setHeader('Content-Type', 'text/html');
 
    res.write(
      '<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>'
    );
 
    const outputPath = middleware.getFilenameFromUrl(options.publicPath || '/');
    const filesystem = middleware.fileSystem;
 
    writeDirectory(options.publicPath || '/', outputPath);
 
    res.end('</body></html>');
 
    function writeDirectory(baseUrl, basePath) {
      const content = filesystem.readdirSync(basePath);
 
      res.write('<ul>');
 
      content.forEach((item) => {
        const p = `${basePath}/${item}`;
 
        if (filesystem.statSync(p).isFile()) {
          res.write(`<li><a href="${baseUrl + item}">${item}</a></li>`);
 
          if (/\.js$/.test(item)) {
            const html = item.substr(0, item.length - 3);
            const containerHref = baseUrl + html;
 
            const magicHtmlHref =
              baseUrl.replace(
                // eslint-disable-next-line
                /(^(https?:\/\/[^\/]+)?\/)/,
                '$1webpack-dev-server/'
              ) + html;
 
            res.write(
              `<li><a href="${containerHref}">${html}</a>` +
                ` (magic html for ${item}) (<a href="${magicHtmlHref}">webpack-dev-server</a>)` +
                `</li>`
            );
          }
        } else {
          res.write(`<li>${item}<br>`);
 
          writeDirectory(`${baseUrl + item}/`, p);
 
          res.write('</li>');
        }
      });
 
      res.write('</ul>');
    }
  });
}
 
module.exports = routes;