-
Notifications
You must be signed in to change notification settings - Fork 0
/
spandx.config.cjs
117 lines (105 loc) · 3.06 KB
/
spandx.config.cjs
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
const fs = require('node:fs/promises');
const path = require('node:path');
const element = [...process.argv].pop();
console.log(element);
// if (!element.match(/^rh-/)) {
// // eslint-disable-next-line no-console
// console.log('Please specify a component e.g.', '\n\tnpm run proxy rh-footer');
// process.exit(1);
// }
/**
* @param {import('node:http').ClientRequest} _req
* @param {import('node:http').ServerResponse} res
* @param {() => void} next
*/
async function injectLocalSources(_req, res, next) {
try {
const proxyContents = await fs.readFile(path.join('demo', 'proxy.html'));
const importMapJson = JSON.stringify(
{
"imports": {
"@patternfly/elements/" : "https://www.redhatstatic.com/dx/v1-alpha/@patternfly/[email protected]/",
"@rhds/elements/rh-button/rh-button.js":"https://www.redhatstatic.com/dx/v1-alpha/@rhds/[email protected]/elements/rh-button/rh-button.js",
"@rhds/elements/":"https://www.redhatstatic.com/dx/v1-alpha/@rhds/[email protected]/elements/",
"@rhds/elements/lib/":"https://www.redhatstatic.com/dx/v1-alpha/@rhds/[email protected]/lib/",
"/src/super-clippy.js": "/src/super-clippy.js",
}
},);
const { write: origWrite } = res;
// eslint-disable-next-line func-names
res.write = function(chunk, ...rest) {
let someChunk = chunk;
if (res.getHeader('Content-Type').includes('text/html')) {
if (someChunk instanceof Buffer) {
someChunk = chunk.toString();
}
someChunk = someChunk
.replace('</head>', `<script type="importmap">${importMapJson}</script>\n</head>`)
.replace('</body>', `${proxyContents}\n\n</body>`);
res.setHeader('Content-Length', chunk.length);
}
origWrite.apply(this, [someChunk, ...rest]);
};
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
} finally {
next();
}
}
module.exports = {
host: {
local: 'localhost',
},
port: 'auto',
open: !true,
startPath: '/',
verbose: false,
routes: {
// shut off web components bundle
// '/sites/all/libraries/webrh/dist/js/webrh.webcomponents.min.js': '',
'/node_modules/': {
host: 'http://localhost:8000',
path: '/node_modules/'
},
'/en/node_modules/': {
host: 'http://localhost:8000',
path: '/node_modules/'
},
'@rhds/elements/': {
host: 'http://localhost:8000',
path: '/elements/',
watch: './elements/'
},
'/en/elements/': {
host: 'http://localhost:8000',
path: '/elements/',
watch: './elements/'
},
'/elements/': {
host: 'http://localhost:8000',
path: '/elements/',
watch: './elements/'
},
'/lib/': {
host: 'http://localhost:8000',
path: '/lib/',
},
'/en/lib/': {
host: 'http://localhost:8000',
path: '/lib/',
},
'/': {
host: 'https://www.redhat.com',
watch: './'
},
},
bs: {
proxy: {
target: 'https://www.redhat.com',
middleware: [
injectLocalSources,
],
},
},
};