简体   繁体   中英

symfony webpack encore huge js files with datatables

I do not know how to optimize the size of my .js files compiled by webpack via webpack-encore on a symfony project 4

here is the js of my layout assets/js/app.js:

require('../css/app.css');
require('bootstrap/js/dist/index.js');

the js of my page assets/js/datatables.js:

require('../css/datatables.css');
require('datatables.net-bs4/js/dataTables.bootstrap4.js');
global.$ = $;

and the webpack configuration file webpack.config.js:

const Encore = require('@symfony/webpack-encore');
Encore
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .addEntry('js/app', './assets/js/app.js')
    .addEntry('js/datatables', './assets/js/datatables.js')
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    .enableVersioning(Encore.isProduction())
    .enableSassLoader()
    .autoProvidejQuery()
;

module.exports = Encore.getWebpackConfig();

notice the size of the datatables.js file ....

$ ls -lah public/build/js/
total 3.2M
drwxr-xr-x 3 dev dev 4.0K Aug 16 12:52 .
drwxr-xr-x 5 dev dev 4.0K Aug 16 12:52 ..
-rw-r--r-- 1 dev dev 470K Aug 16 12:52 app.css
-rw-r--r-- 1 dev dev 740K Aug 16 12:52 app.js
-rw-r--r-- 1 dev dev  14K Aug 16 12:52 datatables.css
-rw-r--r-- 1 dev dev 2.0M Aug 16 12:52 datatables.js
drwxr-xr-x 2 dev dev 4.0K Aug 16 12:52 default

2.0M : this seems excessive for datatables

to close this discussion: compiling for the production environment greatly reduces the size of the files, indeed. here is another solution, use SharedEntries:

const Encore = require('@symfony/webpack-encore');
Encore
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .createSharedEntry('js/app', './assets/js/app.js')
    .addEntry('js/datatables', './assets/js/datatables.js')
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    .enableVersioning(Encore.isProduction())
    .enableSassLoader()
    .autoProvidejQuery();
const config = Encore.getWebpackConfig();
delete config.stats;
module.exports = config;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM