Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules/
/dist/src/
.idea/
demo/
16 changes: 10 additions & 6 deletions dist/opencv.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
define(function () {
return (root.cv = factory());
});
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
} else if (typeof module === 'object' && module !== null) {
// Node.js/CommonJS environment - handle cases where module.exports might be null/undefined
module.exports = factory();
} else if (typeof window === 'object') {
// Browser globals
Expand All @@ -16,10 +14,16 @@
// Web worker
root.cv = factory();
} else {
// Other shells, e.g. d8
// Other shells, e.g. d8 - ensure root exists before setting properties
root = root || (typeof globalThis !== 'undefined' ? globalThis :
typeof global !== 'undefined' ? global :
typeof self !== 'undefined' ? self : {});
root.cv = factory();
}
}(this, function () {
}(typeof globalThis !== 'undefined' ? globalThis :
typeof global !== 'undefined' ? global :
typeof self !== 'undefined' ? self :
typeof window !== 'undefined' ? window : this, function () {

var cv = (() => {
var _scriptName = typeof document != 'undefined' ? document.currentScript?.src : undefined;
Expand Down
44 changes: 33 additions & 11 deletions dist/opencv.js.patch
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
diff --git a/dist/opencv.js b/dist/opencv.js
index af4111b..3ba8a69 100644
index 3ba8a69..00e1686 100644
--- a/dist/opencv.js
+++ b/dist/opencv.js
@@ -41,7 +41,7 @@ else if (typeof define === 'function' && define['amd'])
define([], () => cv);

if (typeof Module === 'undefined')
- Module = {};
+ var Module = {};
return cv(Module);
}));

\ No newline at end of file
@@ -4,10 +4,8 @@
define(function () {
return (root.cv = factory());
});
- } else if (typeof module === 'object' && module.exports) {
- // Node. Does not work with strict CommonJS, but
- // only CommonJS-like environments that support module.exports,
- // like Node.
+ } else if (typeof module === 'object' && module !== null) {
+ // Node.js/CommonJS environment - handle cases where module.exports might be null/undefined
module.exports = factory();
} else if (typeof window === 'object') {
// Browser globals
@@ -16,10 +14,16 @@
// Web worker
root.cv = factory();
} else {
- // Other shells, e.g. d8
+ // Other shells, e.g. d8 - ensure root exists before setting properties
+ root = root || (typeof globalThis !== 'undefined' ? globalThis :
+ typeof global !== 'undefined' ? global :
+ typeof self !== 'undefined' ? self : {});
root.cv = factory();
}
-}(this, function () {
+}(typeof globalThis !== 'undefined' ? globalThis :
+ typeof global !== 'undefined' ? global :
+ typeof self !== 'undefined' ? self :
+ typeof window !== 'undefined' ? window : this, function () {

var cv = (() => {
var _scriptName = typeof document != 'undefined' ? document.currentScript?.src : undefined;
83 changes: 83 additions & 0 deletions test/nextjs-compatibility.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Comprehensive test for Next.js 14.1.0 compatibility
const path = require('path');

describe('Next.js 14.1.0 Compatibility', () => {

test('should handle module.exports being null', () => {
// Save original module.exports
const originalExports = module.exports;

try {
// Set module.exports to null (simulating Next.js 14.1.0 issue)
module.exports = null;

// Clear require cache to force re-evaluation
const opencvPath = path.resolve(__dirname, '../dist/opencv.js');
delete require.cache[opencvPath];

// This should not throw an error
expect(() => {
require(opencvPath);
}).not.toThrow();

} finally {
// Restore original module.exports
module.exports = originalExports;
}
});

test('should handle undefined this context', () => {
const opencvPath = path.resolve(__dirname, '../dist/opencv.js');

// Clear require cache
delete require.cache[opencvPath];

// Load in strict mode context (where 'this' would be undefined)
expect(() => {
(function() {
'use strict';
require(opencvPath);
})();
}).not.toThrow();
});

test('should successfully load opencv.js', () => {
const opencvPath = path.resolve(__dirname, '../dist/opencv.js');

// Clear require cache
delete require.cache[opencvPath];

const cv = require(opencvPath);

expect(cv).toBeDefined();
expect(typeof cv).toBe('object');
});

test('should work in various module environments', () => {
const opencvPath = path.resolve(__dirname, '../dist/opencv.js');

// Test different module.exports scenarios
const scenarios = [
{ name: 'normal exports', exports: {} },
{ name: 'null exports', exports: null },
{ name: 'undefined exports', exports: undefined },
{ name: 'false exports', exports: false }
];

scenarios.forEach(scenario => {
const originalExports = module.exports;

try {
module.exports = scenario.exports;
delete require.cache[opencvPath];

expect(() => {
require(opencvPath);
}).not.toThrow();

} finally {
module.exports = originalExports;
}
});
});
});