Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Fix: Webpacked electron app can't load native module on windows #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/cv.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
const path = require('path');
const { resolvePath } = require('./commons');

const requirePath = path.join(__dirname, process.env.BINDINGS_DEBUG ? '../build/Debug/opencv4nodejs' : '../build/Release/opencv4nodejs')

const logDebug = process.env.OPENCV4NODES_DEBUG_REQUIRE ? require('npmlog').info : () => {}
const isElectron = process.versions.hasOwnProperty('electron')
const packagePath = 'node_modules/opencv4nodejs-prebuilt'

const debugOrRelease = process.env.BINDINGS_DEBUG ? 'Debug' : 'Release'
const binaryName = 'opencv4nodejs.node'
let requirePath
let cv = null
let requireFunc
const logDebug = process.env.OPENCV4NODES_DEBUG_REQUIRE ? require('npmlog').info : () => {}
try {
logDebug('require', 'require path is ' + requirePath)
cv = require(requirePath);
if (isElectron) {
// in electron we are referencing the node module from a packed file in some static folder. So relative path will be complicated. Use absolute path instead.
const electron = require("electron")
const appPath = (electron.app || electron.remote.app).getAppPath()
requirePath = path.resolve(appPath, packagePath, `build/${debugOrRelease}/${binaryName}`)
// requireing a .node file fails with webpack. __non_webpack_require__ tells webpack not to resolve the dependency
logDebug('require', 'require path is ' + requirePath)
cv = __non_webpack_require__(requirePath);
} else {
requirePath = path.join(__dirname, `../build/${debugOrRelease}/${binaryName}`)
logDebug('require', 'require path is ' + requirePath)
cv = require(requirePath);
}
} catch (err) {
logDebug('require', 'failed to require cv with exception: ' + err.toString())
throw err
Expand Down