fix: electron sourcemap issues (#1919)

This commit is contained in:
Peng Xiao
2023-04-13 21:37:50 +08:00
committed by GitHub
parent 6571ec2df6
commit 934e242116
4 changed files with 63 additions and 14 deletions

View File

@@ -1,23 +1,48 @@
import { protocol, session } from 'electron';
import { join } from 'path';
protocol.registerSchemesAsPrivileged([
{
scheme: 'assets',
privileges: {
secure: false,
corsEnabled: true,
supportFetchAPI: true,
standard: true,
bypassCSP: true,
},
},
]);
function toAbsolutePath(url: string) {
let realpath = decodeURIComponent(url);
const webStaticDir = join(__dirname, '../../../resources/web-static');
if (url.startsWith('./')) {
// if is a file type, load the file in resources
if (url.split('/').at(-1)?.includes('.')) {
realpath = join(webStaticDir, decodeURIComponent(url));
} else {
// else, fallback to load the index.html instead
realpath = join(webStaticDir, 'index.html');
}
}
return realpath;
}
export function registerProtocol() {
if (process.env.NODE_ENV === 'production') {
protocol.interceptFileProtocol('file', (request, callback) => {
const url = request.url.replace(/^file:\/\//, '');
const webStaticDir = join(__dirname, '../../../resources/web-static');
if (url.startsWith('./')) {
// if is a file type, load the file in resources
if (url.split('/').at(-1)?.includes('.')) {
const realpath = join(webStaticDir, decodeURIComponent(url));
callback(realpath);
} else {
// else, fallback to load the index.html instead
const realpath = join(webStaticDir, 'index.html');
console.log(realpath, 'realpath', url, 'url');
callback(realpath);
}
}
const realpath = toAbsolutePath(url);
// console.log('realpath', realpath, 'for', url);
callback(realpath);
});
protocol.registerFileProtocol('assets', (request, callback) => {
const url = request.url.replace(/^assets:\/\//, '');
const realpath = toAbsolutePath(url);
// console.log('realpath', realpath, 'for', url);
callback(realpath);
});
}
@@ -35,6 +60,7 @@ export function registerProtocol() {
'DELETE',
'OPTIONS',
];
// responseHeaders['Content-Security-Policy'] = ["default-src 'self'"];
}
callback({ responseHeaders });