WIP to support LibreJS in vite build

This commit is contained in:
Bruno Windels 2021-12-08 14:49:03 +01:00
parent c87628b614
commit 1d3a551da6

View file

@ -49,6 +49,7 @@ export default {
// ]),
// important this comes before service worker
// otherwise the manifest and the icons it refers to won't be cached
addLibreJSHeaders(),
injectWebManifest("assets/manifest.json"),
injectServiceWorker("sw.js"),
],
@ -75,3 +76,39 @@ export default {
function scriptTagPath(htmlFile, index) {
return `${htmlFile}?html-proxy&index=${index}.js`;
}
function addLibreJSHeaders() {
return addHeaderPlugin((name, fileName) => {
if (fileName.endsWith(".js")) {
switch (name) {
case "index":
return {
banner: "/* @license magnet:?xt=urn:btih:8e4f440f4c65981c5bf93c76d35135ba5064d8b7&dn=apache-2.0.txt Apache-2.0 */",
footer: "/* @license-end */"
};
case "vendor":
return {
banner: "/* @license multiple but all open source */",
footer: "/* @license-end */"
}
}
}
return null;
});
}
function addHeaderPlugin(callback) {
return {
name: "hydrogen:addHeader",
renderChunk: (code, chunk) => {
const result = callback(chunk.name, chunk.fileName);
if (result?.banner) {
code = result.banner + code;
}
if (result?.footer) {
code = code + result.footer;
}
return code;
}
}
}