Skip to content

Commit

Permalink
fix: Allow using in ESM bundled by Vite (electron loading workaround) #…
Browse files Browse the repository at this point in the history
  • Loading branch information
megahertz committed Dec 30, 2023
1 parent a8448c9 commit ae1f0d6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-log",
"version": "5.0.4",
"version": "5.0.5-beta.2",
"description": "Just a simple logging module for your Electron application",
"main": "src/index.js",
"browser": "src/renderer/index.js",
Expand Down
16 changes: 7 additions & 9 deletions src/main/electronApi.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
'use strict';

require('./electronApiLoader');

const electron = require('electron');
const os = require('os');
const path = require('path');

/** @type {Electron.Main} */
let electron;
try {
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
electron = require('electron');
} catch {
electron = null;
}

module.exports = {
getAppUserDataPath() {
return getPath('userData');
Expand Down Expand Up @@ -231,3 +225,7 @@ function sendIpcToRenderer(channel, message) {
}
});
}

function loadElectron() {

Check warning on line 229 in src/main/electronApi.js

View workflow job for this annotation

GitHub Actions / main (ubuntu-latest, latest, latest, false)

'loadElectron' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 229 in src/main/electronApi.js

View workflow job for this annotation

GitHub Actions / main (ubuntu-latest, 16, latest, false)

'loadElectron' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 229 in src/main/electronApi.js

View workflow job for this annotation

GitHub Actions / main (ubuntu-latest, 14, latest, true)

'loadElectron' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 229 in src/main/electronApi.js

View workflow job for this annotation

GitHub Actions / main (ubuntu-latest, latest, 16, true)

'loadElectron' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 229 in src/main/electronApi.js

View workflow job for this annotation

GitHub Actions / main (ubuntu-latest, latest, 13, true)

'loadElectron' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 229 in src/main/electronApi.js

View workflow job for this annotation

GitHub Actions / main (macos-latest, latest, latest, true)

'loadElectron' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 229 in src/main/electronApi.js

View workflow job for this annotation

GitHub Actions / main (windows-latest, latest, latest, true)

'loadElectron' is defined but never used. Allowed unused vars must match /^_/u

}
23 changes: 23 additions & 0 deletions src/main/electronApiLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

// This is a workaround for node.js environment when Electron dependency isn't
// installed.

let electronIsAvailable = false;

try {
// eslint-disable-next-line global-require
require('electron');
electronIsAvailable = true;
} catch {
// No Electron installed
}

if (!electronIsAvailable) {
try {
require.cache.electron = { exports: {} };
} catch {
// Looks like there is no other way to provide support for node.js app
// without Electron installed.
}
}
7 changes: 4 additions & 3 deletions src/main/transports/file/packageJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ function extractPathFromArgs() {
}

function getMainModulePath() {
if (typeof require === 'function') {
try {
// Requires isn't available in ESM
return require.main?.filename;
} catch {
return undefined;
}

return undefined;
}

0 comments on commit ae1f0d6

Please sign in to comment.