diff --git a/.gitignore b/.gitignore index b7acd9c..cb7dede 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /node_modules/ /dist/src/ .idea/ +demo/ diff --git a/dist/opencv.js b/dist/opencv.js index 3ba8a69..00e1686 100644 --- a/dist/opencv.js +++ b/dist/opencv.js @@ -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; diff --git a/dist/opencv.js.patch b/dist/opencv.js.patch index 43fc1b4..005ddfa 100644 --- a/dist/opencv.js.patch +++ b/dist/opencv.js.patch @@ -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; diff --git a/test/nextjs-compatibility.test.js b/test/nextjs-compatibility.test.js new file mode 100644 index 0000000..f2034ee --- /dev/null +++ b/test/nextjs-compatibility.test.js @@ -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; + } + }); + }); +}); \ No newline at end of file