DataTables / DataTablesSrc

DataTables source repository
https://datatables.net
MIT License
587 stars 422 forks source link

Datatables fails to load as a script when require.js is present #213

Closed mwouts closed 2 years ago

mwouts commented 2 years ago

This is a follow-up on this comment.

In some cases (and I don't have control on that), require.js is present in my HTML document. When this is the case I am not able to load datatables as a simple script element. (I know I could use a require.config file, but I'd prefer not to make an exception on whether require.js is there or not)

A minimal example that reproduces the issue is this one:

<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" integrity="sha512-c3Nl8+7g4LMSTdrm621y7kf9v3SDPnhxLNhcjFJbKECVnmZHTdo+IRO05sNLTH/D3vA6u1X32ehoLC7WFVdheg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.js"></script>
<script>
      $(document).ready(function () {
    $('#table').DataTable({});
});
</script>

(the example works when the "require.min.js" library is removed)

Thanks!

AllanJard commented 2 years ago

I've covered some points about this in my comment here, but to keep this topic here - in a Require environment DataTables does not automatically expose itself in the global scope, which I think it typical and correct for AMD modules.

I'm not sure what the correct thing to do here is. Require is loaded, but it isn't being used. If it were used, then it would work. If it weren't loaded, the above would work. It is the combination of the two - Require being loaded, but expecting it to work globally, that is the issue.

I've opened an issue to see if we can get feedback about this from the Require JS folk.

mwouts commented 2 years ago

Yes, that's it. Thank you Allan!

AllanJard commented 2 years ago

We've had a reply in that thread now noting:

if require.min.js is loaded in a page, then the assumption is that module loading via require/define are supposed to be used, particularly if the script supports UMD.

So at this time, I won't be making any changes to our UMD to register DataTables globally and with AMD at the same time.

mwouts commented 2 years ago

Sure, makes sense... and also I agree that my question (the heterogeneity of the environments in which Jupyter notebooks are rendered) is very specific. Anyway, thank you so much for considering it!

AllanJard commented 2 years ago

On the plus side, I've been making good progress with ES modules recently. Going on to be in holiday next week, but should have something to show after that.

mwouts commented 2 years ago

Awesome! Keep me posted... and have a great holiday!