简体   繁体   English

symfony webpack用数据表重新添加了巨大的js文件

[英]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 我不知道如何在symfony项目4上通过webpack-encore优化由webpack编译的.js文件的大小

here is the js of my layout assets/js/app.js: 这是我的布局资产/js/app.js的js:

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

the js of my page assets/js/datatables.js: 我的页面Assets / js / datatables.js的js:

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

and the webpack configuration file webpack.config.js: 和webpack配置文件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 .... 注意datatables.js文件的大小...。

$ 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 2.0M:这对于数据表来说似乎过多

to close this discussion: compiling for the production environment greatly reduces the size of the files, indeed. 结束本次讨论:为生产环境进行编译确实可以大大减少文件的大小。 here is another solution, use SharedEntries: 这是另一个解决方案,请使用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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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