-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.dev-server.config.js
193 lines (154 loc) · 5.16 KB
/
webpack.dev-server.config.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* eslint-disable @typescript-eslint/no-var-requires */
/**
* This webpack config establishes a proxy server to enable a local build of library-registry-admin
* to connect to a remote Library Registry back-end. Requests for webpack assets are served from
* memory; other requests are proxied to the specified Library Registry.
*
* Usage:
* npm run dev-server -- --env=backend=https://registry-tpp-qa.palaceproject.io
* or
* npx webpack serve --config webpack.dev-server.config --env=backend=https://registry-tpp-qa.palaceproject.io
*
* This makes the library-registry webapp available at http://localhost:8080/admin.
*/
const {
createProxyMiddleware,
responseInterceptor,
} = require("http-proxy-middleware");
const { merge } = require("webpack-merge");
const { URL } = require("url");
const dev = require("./webpack.dev.config.js");
/**
* The public path to local webpack assets. This is chosen to have low chance of collision with any
* path on the back-end. This should start and end with slashes.
*/
const devAssetsPublicPath = "/webpack-dev-assets/";
/**
* Build the webpack configuration.
* See https://webpack.js.org/configuration/configuration-types/#exporting-a-function
*
* @param {object} env The environment.
* @returns The webpack configuration.
*/
module.exports = (env) => {
const { backend } = env;
if (!backend) {
console.error("Please specify the URL of a Library Registry back-end.");
console.error(
"Example: npm run dev-server -- --env=backend=https://registry-tpp-qa.palaceproject.io"
);
throw "No back-end URL was specified.";
}
console.info(`Using Library Registry back-end: ${backend}`);
const backendUrl = new URL(backend);
/**
* Rewrite a location header (as received in a 3xx response). This changes back-end URLs to
* point to the local server instead.
*
* @param res The response.
* @param req The request.
*/
const rewriteLocationHeader = (res, req) => {
const location = res.getHeader("location");
if (!location) {
return;
}
// Use the referer as the base URL, if location is relative.
const referer = req.headers.referer;
const locationUrl = new URL(location, referer);
if (locationUrl.host !== backendUrl.host) {
return;
}
const requestHost = req.headers.host;
if (!requestHost) {
return;
}
locationUrl.protocol = "http";
locationUrl.host = requestHost;
res.setHeader("location", locationUrl.href);
};
/**
* Rewrite a location header (as received in a 3xx response). This changes back-end URLs to
* point to the local server instead. The CM may redirect to a URL that contains a "redirect"
* parameter that contains another URL. The "redirect" parameter is also rewritten.
*
* @param res The response.
* @param req The request.
*/
const rewriteCMLocationHeader = (res, req) => {
const location = res.getHeader("location");
if (!location) {
return;
}
const locationUrl = new URL(location);
if (locationUrl.host !== backendUrl.host) {
return;
}
const requestHost = req.headers.host;
if (!requestHost) {
return;
}
locationUrl.protocol = "http";
locationUrl.host = requestHost;
const redirectParam = locationUrl.searchParams.get("redirect");
if (redirectParam) {
const redirectUrl = new URL(redirectParam);
if (redirectUrl.host == backendUrl.host) {
redirectUrl.protocol = "http";
redirectUrl.host = requestHost;
locationUrl.searchParams.set("redirect", redirectUrl.href);
}
}
res.setHeader("location", locationUrl.href);
};
/**
* Rewrites an HTML response. This changes jsdelivr CDN URLs in the page to point to the webpack
* assets on the local server instead. This is a simple find-and-replace.
*
* @param responseBuffer A buffer containing the response body.
* @param req The request.
* @returns
*/
const rewriteHTML = (responseBuffer, req) => {
const requestHost = req.headers.host;
if (!requestHost) {
return responseBuffer;
}
const page = responseBuffer.toString("utf8");
const packageName = process.env.npm_package_name;
const cdnUrlPattern = `"https://cdn.jsdelivr.net/npm/${packageName}(@.*?)?/dist/(.*?)"`;
return page.replace(
new RegExp(cdnUrlPattern, "g"),
`"http://${requestHost}${devAssetsPublicPath}$2"`
);
};
const proxyMiddleware = createProxyMiddleware({
changeOrigin: true,
onProxyRes: responseInterceptor(
async (responseBuffer, proxyRes, req, res) => {
rewriteLocationHeader(res, req);
const contentType = res.getHeader("content-type");
if (contentType && contentType.startsWith("text/html")) {
return rewriteHTML(responseBuffer, req);
}
return responseBuffer;
}
),
proxyTimeout: 5000,
secure: false,
selfHandleResponse: true,
target: backend,
timeout: 5000,
});
const config = merge(dev, {
devServer: {
onAfterSetupMiddleware: (devServer) => {
devServer.app.use("/", proxyMiddleware);
},
},
output: {
publicPath: devAssetsPublicPath,
},
});
return config;
};