-
-
Save jrunestone/2fbe5d6d5e425b7c046168b6d6e74e95 to your computer and use it in GitHub Desktop.
Here's how to make jQuery DataTables work with npm and webpack. DT checks for AMD compatibility first | |
which breaks when you're using CommonJS with webpack. | |
Install DT core: npm install datatables.net | |
Install a DT style: npm install datatables.net-bs (bootstrap) | |
Install the imports-loader webpack plugin: https://github.com/webpack/imports-loader#disable-amd | |
Create a loader "exception" just for DT in webpack.config.js: | |
module: { | |
loaders: [ | |
{ | |
test: /datatables\.net.*/, | |
loader: 'imports?define=>false' | |
} | |
] | |
} | |
Then to initialize DT in your app, do this in your main entry point: | |
// you can use import or require | |
import 'datatables.net'; | |
import dt from 'datatables.net-bs'; | |
dt(window, $); | |
Now you can use .DataTable(): | |
$('table[data-table]').DataTable(); // or whatever you want |
Oh, I've just noticed that you did call dt(window, $);
in your source and you use no Datatables plugins.
You may need to inspect the (unminified) build output or debug it in the browser to see what is happening with window.$
when your initialization gets executed.
Thanks for you help!
I just found where was my error...
It was completely my fault...
but difficult to find because somewhere unexpected (for me at least).
So in fact the "only" error which was causing this was because as I am learning webpack I read the tree-shaking section of the manual and for some reason I copy and past a part of it in my package.json...
"sideEffects": false
Which was obviously a bad idea/error since everything is not in es6 in my modules (such as datatables but not only).
The config syntax of the loader changed.
Here's what works for me:
https://stackoverflow.com/a/74204588/4653886
Did you execute the functions imported from DataTables and their plugin modules?
I noticed a peculiar behaviour of DataTables JavaScript bundles, when used as ES6 modules. When importing them, they did nothing. No registration in
$.fn
or$.fn.dataTables.ext
. I found, that each module exports an object with thedefault
property, which points to a function expectingwindow
and$
as parameters. When this function gets called, the module performs its registration, like it does in an AMD or a plain JavaScript project. That is why I called that function after importing each module and my single bundle.This is how
esbuild
bundles the source from my comment above, which is taken from a project of mine:The prolog (simplified here) added by
esbuild
to the output bundle shows that DataTables UMD wrappers are called with(exports, module)
to let them adapt to a CommonJS environment. Their exports are re-eported depending on their support of ES6:A simplified DataTables module shows that DataTables do not export __esModule from their UMD wrapper. That is why their exports are put to the property
default
in the single exported object. Also, it shows that unlike in AMD and plain JavaScript environments, it does not execute thefactory
function in the CommonJS environment. It just exports it. That is why their exported functions have to be called manually before you try to use their functionality.You may want to inspect your
webpack
output to understand what is happening there too.