1 | 'use strict';
|
---|
2 |
|
---|
3 | const { createReadStream } = require('fs');
|
---|
4 | const { join } = require('path');
|
---|
5 |
|
---|
6 | const clientBasePath = join(__dirname, '..', '..', 'client');
|
---|
7 |
|
---|
8 | function routes(server) {
|
---|
9 | const app = server.app;
|
---|
10 | const middleware = server.middleware;
|
---|
11 | const options = server.options;
|
---|
12 |
|
---|
13 | app.get('/__webpack_dev_server__/live.bundle.js', (req, res) => {
|
---|
14 | res.setHeader('Content-Type', 'application/javascript');
|
---|
15 |
|
---|
16 | createReadStream(join(clientBasePath, 'live.bundle.js')).pipe(res);
|
---|
17 | });
|
---|
18 |
|
---|
19 | app.get('/__webpack_dev_server__/sockjs.bundle.js', (req, res) => {
|
---|
20 | res.setHeader('Content-Type', 'application/javascript');
|
---|
21 |
|
---|
22 | createReadStream(join(clientBasePath, 'sockjs.bundle.js')).pipe(res);
|
---|
23 | });
|
---|
24 |
|
---|
25 | app.get('/webpack-dev-server.js', (req, res) => {
|
---|
26 | res.setHeader('Content-Type', 'application/javascript');
|
---|
27 |
|
---|
28 | createReadStream(join(clientBasePath, 'index.bundle.js')).pipe(res);
|
---|
29 | });
|
---|
30 |
|
---|
31 | app.get('/webpack-dev-server/invalidate', (_req, res) => {
|
---|
32 | server.invalidate();
|
---|
33 | res.end();
|
---|
34 | });
|
---|
35 |
|
---|
36 | app.get('/webpack-dev-server/*', (req, res) => {
|
---|
37 | res.setHeader('Content-Type', 'text/html');
|
---|
38 |
|
---|
39 | createReadStream(join(clientBasePath, 'live.html')).pipe(res);
|
---|
40 | });
|
---|
41 |
|
---|
42 | app.get('/webpack-dev-server', (req, res) => {
|
---|
43 | res.setHeader('Content-Type', 'text/html');
|
---|
44 |
|
---|
45 | res.write(
|
---|
46 | '<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>'
|
---|
47 | );
|
---|
48 |
|
---|
49 | const outputPath = middleware.getFilenameFromUrl(options.publicPath || '/');
|
---|
50 | const filesystem = middleware.fileSystem;
|
---|
51 |
|
---|
52 | writeDirectory(options.publicPath || '/', outputPath);
|
---|
53 |
|
---|
54 | res.end('</body></html>');
|
---|
55 |
|
---|
56 | function writeDirectory(baseUrl, basePath) {
|
---|
57 | const content = filesystem.readdirSync(basePath);
|
---|
58 |
|
---|
59 | res.write('<ul>');
|
---|
60 |
|
---|
61 | content.forEach((item) => {
|
---|
62 | const p = `${basePath}/${item}`;
|
---|
63 |
|
---|
64 | if (filesystem.statSync(p).isFile()) {
|
---|
65 | res.write(`<li><a href="${baseUrl + item}">${item}</a></li>`);
|
---|
66 |
|
---|
67 | if (/\.js$/.test(item)) {
|
---|
68 | const html = item.substr(0, item.length - 3);
|
---|
69 | const containerHref = baseUrl + html;
|
---|
70 |
|
---|
71 | const magicHtmlHref =
|
---|
72 | baseUrl.replace(
|
---|
73 | // eslint-disable-next-line
|
---|
74 | /(^(https?:\/\/[^\/]+)?\/)/,
|
---|
75 | '$1webpack-dev-server/'
|
---|
76 | ) + html;
|
---|
77 |
|
---|
78 | res.write(
|
---|
79 | `<li><a href="${containerHref}">${html}</a>` +
|
---|
80 | ` (magic html for ${item}) (<a href="${magicHtmlHref}">webpack-dev-server</a>)` +
|
---|
81 | `</li>`
|
---|
82 | );
|
---|
83 | }
|
---|
84 | } else {
|
---|
85 | res.write(`<li>${item}<br>`);
|
---|
86 |
|
---|
87 | writeDirectory(`${baseUrl + item}/`, p);
|
---|
88 |
|
---|
89 | res.write('</li>');
|
---|
90 | }
|
---|
91 | });
|
---|
92 |
|
---|
93 | res.write('</ul>');
|
---|
94 | }
|
---|
95 | });
|
---|
96 | }
|
---|
97 |
|
---|
98 | module.exports = routes;
|
---|