Search code examples
javascriptmergerequirejscodemirror

Trouble setting up CodeMirror Merge View + Require.js


I have a project using Require.js where I'm using CodeMirror to view/edit a JavaScript file in the browser (this and all CodeMirror addons work as expected).

I now need to show any diffs in the file, so I've been trying to implement the merge.js addon, but having troubles with the diff_match_patch.js lib that is also required.

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 seeing

"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. I've also copied the mark-up from the demo and I'm seeing the same "diff_match_patch is not a constructor" error. Referencing the diff_match_patch.js from the CDN gives the same error as well.

Any ideas?

Thanks!


Solution

  • More discussion, and a solution, at the corresponding github issue