-
-
Save jmcker/e22579a00a785cbd430408a0ba482eea to your computer and use it in GitHub Desktop.
Electron 5.x demo app that demonstrates how to enable ES modules support.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<base href="app://./" /> | |
<meta http-equiv="Content-Security-Policy" content="script-src 'self' app:; object-src 'self' app:;"> | |
<script type="module" src="./module.js"></script> | |
</head> | |
<body> | |
Check the console! | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { app, protocol, BrowserWindow } = require('electron'); | |
const createProtocol = require('./protocol.js'); | |
const url = require('url'); | |
const path = require('path'); | |
const SCHEME_NAME = 'app'; | |
// Scheme must be registered before the app is ready | |
protocol.registerSchemesAsPrivileged([{ | |
scheme: SCHEME_NAME, | |
standard: true, | |
secure: true, | |
bypassCSP: false, | |
allowServiceWorkers: true, | |
supportFetchAPI: true, | |
corsEnabled: false | |
}]); | |
function createWindow(loadPath) { | |
let win = new BrowserWindow({ | |
webPreferences: { | |
nodeIntegration: true | |
}, | |
width: 1000, | |
height: 700 | |
}); | |
win.loadURL(url.format({ | |
pathname: loadPath, | |
protocol: SCHEME_NAME, | |
slashes: true | |
})); | |
win.webContents.openDevTools(); | |
} | |
app.on('ready', () => { | |
createProtocol(SCHEME_NAME); | |
createWindow('./index.html'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("ES module loaded"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "demo", | |
"version": "2.0.0", | |
"main": "main.js" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { protocol } = require('electron'); | |
const { readFile } = require('fs'); | |
const { extname, join, dirname } = require('path'); | |
const { URL } = require('url'); | |
const createProtocol = (scheme, normalize = true) => { | |
console.log(`Registering ${scheme} protocol...`); | |
protocol.registerBufferProtocol(scheme, | |
(request, respond) => { | |
let pathName = new URL(request.url).pathname; | |
pathName = decodeURI(pathName); // Needed in case URL contains spaces | |
console.log(`${scheme}:// request for ${pathName}.`); | |
readFile(join(__dirname, pathName), (error, data) => { | |
if (error) { | |
console.warn('Error:'); | |
console.log(error); | |
// Respond with the NET_ERROR code for FILE_NOT_FOUND | |
// See: https://cs.chromium.org/chromium/src/net/base/net_error_list.h | |
respond(-6); | |
return; | |
} | |
let extension = extname(pathName).toLowerCase(); | |
let mimeType = ''; | |
if (extension === '.js' || extension === '.mjs') { | |
mimeType = 'text/javascript'; | |
} | |
else if (extension === '.html') { | |
mimeType = 'text/html'; | |
} | |
else if (extension === '.css') { | |
mimeType = 'text/css'; | |
} | |
else if (extension === '.svg' || extension === '.svgz') { | |
mimeType = 'image/svg+xml'; | |
} | |
else if (extension === '.json') { | |
mimeType = 'application/json'; | |
} | |
respond({ mimeType, data }); | |
}); | |
}, | |
(error) => { | |
if (error) { | |
console.error(`Failed to register ${scheme} protocol`, error); | |
} | |
} | |
); | |
}; | |
module.exports = createProtocol; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment