Using diff_match_patch.js with Require.js question

Hi All,
I’m having trouble getting the Merge view working with Require.js. I need to have all assets served up locally, so I placed the diff_match_patch.js file in the same directory as merge.js because I thought that merge.js was expecting it there:

// merge.js
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror", "diff_match_patch"], mod);

But looking at my inspector, I was seing

"NetworkError: 404 Not Found - http://localhost:8080/js/diff_match_patch.js"

So I moved diff_match_patch.js to the /js/ directory. (which isn’t ideal, but okay if it works)

No 404s, but now I’m seeing this:

// merge.js 
// (line 550, col 12)
"TypeError: diff_match_patch is not a constructor
var dmp = new diff_match_patch();"

I’m not sure what I’m doing wrong, but if anyone could shed some light on this, it’d be great. The rest of the CodeMirror installation is working fine. I’m using it on another page to just show/edit some code, and it, plus all the addons, are working as expected.

Thanks!

Update:
If found that the script is stopping when a new instance of diff_match_patch is trying to be initialized. (line 550, merge.js)

Is this because Require.js is wrapping it in a way that it can’t be passed in this way? Changing

var dmp = new diff_match_patch(); 

to

var dmp = diff_match_patch;

allows the 2 versions of the code to be displayed, but of course, the diff isn’t shown.

I feel like I may have to start hunting for a new diff library if I can’t get this resolved soon, so any help is greatly appreciated!
Thanks

See the github issue you opened

1 Like