Skip to content

Commit

Permalink
fix: Allow using in ESM bundled by Vite #390
Browse files Browse the repository at this point in the history
  • Loading branch information
megahertz committed Dec 30, 2023
1 parent 3526fec commit af7b7a0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/main/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ function initializePreload({
}) {
let preloadPath = typeof preloadOption === 'string'
? preloadOption
: path.resolve(__dirname, '../renderer/electron-log-preload.js');
: undefined;

if (!fs.existsSync(preloadPath)) {
try {
preloadPath = path.resolve(
__dirname,
'../renderer/electron-log-preload.js',
);
} catch {
// Ignore, the file is bundled to ESM
}

if (!preloadPath || !fs.existsSync(preloadPath)) {
preloadPath = path.join(
electronApi.getAppUserDataPath() || os.tmpdir(),
'electron-log-preload.js',
Expand Down
10 changes: 9 additions & 1 deletion src/main/transports/file/packageJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
* @return {{ name?: string, version?: string}}
*/
function readPackageJson() {
return tryReadJsonAt(require.main && require.main.filename)
return tryReadJsonAt(getMainModulePath())
|| tryReadJsonAt(extractPathFromArgs())
|| tryReadJsonAt(process.resourcesPath, 'app.asar')
|| tryReadJsonAt(process.resourcesPath, 'app')
Expand Down Expand Up @@ -96,3 +96,11 @@ function extractPathFromArgs() {
const userDataDir = matchedArgs[0];
return userDataDir.replace('--user-data-dir=', '');
}

function getMainModulePath() {
if (typeof require === 'function') {
return require.main?.filename;
}

return undefined;
}

0 comments on commit af7b7a0

Please sign in to comment.