Include jQuery datatables in webpack.ProvidePlugin on webpack

Viewed 903

I am currently importing jQuery datatables on every javascript file I create.

import 'datatables.net/js/jquery.dataTables';
import 'datatables.net-bs4/js/dataTables.bootstrap4';

$('#example').DataTable();

Is it possible to make it global that I'm not required to import it everytime? jQuery has this behavior and I'm able to access $ on every javascript without importing it.

In my environment.js, I currently have this:

const {environment} = require('@rails/webpacker');

const webpack = require('webpack');
environment.plugins.append('Provide', new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery',
  Popper: ['popper.js', 'default']
}));

module.exports = environment;

I believe that this is the file that loads packages globally. Is it possible to include datatables here?

So far I have tried this:

environment.plugins.append('Provide', new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery',
  Popper: ['popper.js', 'default'],
  DataTable: 'datatables.net'
}));

And this:

environment.plugins.append('Provide', new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery',
  Popper: ['popper.js', 'default'],
  DataTable: 'datatables.net/js/jquery.dataTables'
}));

But no luck. Do you know what the problem might be?

1 Answers
Related