-
Notifications
You must be signed in to change notification settings - Fork 20
/
spandx.config.cjs
114 lines (103 loc) · 2.91 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
const fs = require('node:fs/promises');
const path = require('node:path');
const element = [...process.argv].pop();
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 elementsPath = path.join(__dirname, 'elements');
const elements = await fs.readdir(elementsPath);
const proxyContents = await fs.readFile(path.join(elementsPath, element, 'demo', 'proxy.html'));
const importMapJson = JSON.stringify({
imports: {
'@rhds/elements': 'http://localhost:8000/elements.js',
...Object.fromEntries(elements.map(dir => [
`@rhds/elements/${dir}/${dir}.js`,
`http://localhost:8000/elements/${dir}/${dir}.ts`,
])),
},
});
const { write: origWrite } = res;
res.write = function(chunk, ...rest) {
if (res.getHeader('Content-Type').includes('text/html')) {
if (chunk instanceof Buffer) {
chunk = chunk.toString();
}
chunk = chunk
.replace('</head>', `<script type="importmap">${importMapJson}</script><script async src="https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js" crossorigin="anonymous"></script>\n</head>`)
.replace('</body>', `${proxyContents}\n\n</body>`);
// res.setHeader('Content-Length', chunk.length);
}
origWrite.apply(this, [chunk, ...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,
],
},
},
};