Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load an existing custom lib which is having non AMD and AMD library using requireJS

In my project, there is a custom third-party library (a single build file), which they have created using a couple of non-AMD and AMD files. And most of the properties are exposed directly to window objects. In our project as per the new requirement, we have to load this third-party library in asynchronous mode using requireJS. I tried with shim configuration but am getting an error from third party library saying that globals/window, globals/documents are needed.

  1. How to resolve the above error in the current project without editing a third-party library? is it possible?
  2. How to consume this third party libs properties in the project, since all properties are exposed directly to window Object.

This is the error we are encountering now

enter image description here

Could anyone please help me with this? Thanks in Advance!

like image 435
Asish AP Avatar asked Nov 06 '22 01:11

Asish AP


1 Answers

It looks like your 3rd party library is video.js. Video.js was (see below) creating two global objects global/window and global/document which seem to be used for testing video.js on the server where there is no window or document.

You can see their definition here and then the import. If you're willing to edit the file (and stop video.js tests from working server-side) then that's a simple solution.

You said you didn't want to edit the library though, so let's look for other solutions. You could just go ahead and define them like so:

define('global/window', [], () => {
  return window;
});

define('global/document', ['global/window'], (window) => {
  return window.document;
});

Then load video.js:

require(['video.7.5.0.min.js'], (videojs) => {
  window.videojs = videojs;
  // any other initialization you want here
});

Also it might be helpful to know which version of video.js you're using. It appears this might have been fixed in 7.11.1 (in this PR) so there's a good chance that just upgrading to that version or later makes the problem go away.

There's a lot more discussion on this problem in this issue, but it appears to be closed due to the previously mentioned PR.

like image 171
Mordred Avatar answered Nov 12 '22 14:11

Mordred