I am working on a Symfony 5.2.6 project and I am trying to use datatables.net library in my project, but can't find a way to import it properly.
I am using a lot of js/jquery libraries and everything is working well except datatables. (I am using Metronic admin template)
This is my webpack.config.js :
const Encore = require('@symfony/webpack-encore');
const CopyWebpackPlugin = require('copy-webpack-plugin');
if (!Encore.isRuntimeEnvironmentConfigured()) {
Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}
Encore
.setOutputPath('public/build/')
.setPublicPath('/build')
.addStyleEntry('basecss', './assets/sass/style.scss')
.addStyleEntry('pluginscss', './assets/plugins/plugins.scss')
.addStyleEntry('extrascss', './assets/css/extras.css')
.addEntry('app', './assets/app.js')
.addEntry('plugins', './assets/plugins/plugins.js')
.addEntry('scripts', './assets/scripts.js')
.addEntry('test', './assets/test.js')
.addEntry('page-ms-liste', './assets/pages/matiereseche/liste.js')
.addStyleEntry('page-login-css', './assets/pages/authentication/login.css')
.enableStimulusBridge('./assets/controllers.json')
.splitEntryChunks()
.enableSingleRuntimeChunk()
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.configureBabel((config) => {
config.plugins.push('@babel/plugin-proposal-class-properties');
})
.configureBabelPresetEnv((config) => {
config.useBuiltIns = 'usage';
config.corejs = 3;
})
// enables Sass/SCSS support
.enableSassLoader()
.addPlugin(new CopyWebpackPlugin({
patterns: [
{ from: './assets/images', to: 'images' }
],
}))
.addLoader({
test: require.resolve('jquery'),
use: [{
loader: 'expose-loader',
options: {
exposes: [
{
globalName: "$",
override: true,
},
{
globalName: "jQuery",
override: true,
}
]
}
}]})
.addLoader({
test: '/datatables\.net.*/',
use: [{
loader: 'imports-loader',
options: {
imports: {
moduleName: 'jquery',
name: '$',
},
additionalCode: "var define = false;"
}
}]})
;
const config = Encore.getWebpackConfig();
module.exports = config;
I also tried to use .autoProvidejQuery()
Inside my scripts.js I have :
window.jQuery = window.$ = require('jquery');
// ...
require('datatables.net');
require('datatables.net-bs4');
Then in my js file :
var t = $("#datatable");
t.DataTable(.....)
The error :
Uncaught TypeError: $(...).DataTable is not a function
I found a lot of threads on this topic, but I tried everything without success (using loaders, ...) I also tried to import jquery from CDN and datatables too, but I have a jquery issue (jquery undefined)
If someone has an idea...
Thank you.