Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Webpack Encore, Multiple JS/CSS Files to one file

I want to create one css/js file from multiple css/js files. Multiple addEntry not working, please check my code and give me the solution.

var Encore = require('@symfony/webpack-encore');

Encore
        // the project directory where compiled assets will be stored
        .setOutputPath('web/build/')
        // the public path used by the web server to access the previous directory
        .setPublicPath('/build')
        .cleanupOutputBeforeBuild()
        .enableSourceMaps(!Encore.isProduction())
        // uncomment to create hashed filenames (e.g. app.abc123.css)
        .enableVersioning(Encore.isProduction())
        // uncomment for legacy applications that require $/jQuery as a global variable
        //.autoProvidejQuery()
        // uncomment to define the assets of the project
        .addEntry('js/app', './assets/js/app.js')
        .addEntry('js/uploader', './assets/js/uploader.js')
        .addStyleEntry('css/icons', './assets/css/icons.scss')
        .addStyleEntry('css/app', './assets/css/app.scss')
        .addStyleEntry('css/uploader', './assets/css/uploader.scss')

        // uncomment if you use Sass/SCSS files
        .enableSassLoader()
        .enableBuildNotifications();

module.exports = Encore.getWebpackConfig();

And add common jQuery and after adding the js files some function is undefined, why?

like image 543
Jagjeet Singh Avatar asked Jun 01 '18 04:06

Jagjeet Singh


2 Answers

You only need one addEntry call. The solution I use to do that is to create a main.js file where I import all the file. Something like this for example:

// CSS
import 'bootstrap/dist/css/bootstrap.min.css';
import './global.css';
import './easy-autocomplete.custom.css';

// JS
const $ = require('jquery/dist/jquery.min');
const jQuery = $;
import 'bootstrap';
import 'jscroll/jquery.jscroll';
import 'easy-autocomplete';
import './global.js';

And then you can use this file in your addEntry like this:

.addEntry('app', './assets/main.js')

After running Encore, you will get a web/build/app.js file and web/build/app.css file

like image 88
Florian Hermann Avatar answered Nov 16 '22 04:11

Florian Hermann


Multiple .addStyleEntry will create multiple files. You can pass an array into .addStyleEntry to make a single css file out of multipe css/sass/less files like:

.addStyleEntry('style', ['./src/sass/style.scss', './node_modules/swiper/dist/css/swiper.css'])

This will create a style.css. The order of the array entries matches the output in the css file.

like image 25
eberhapa Avatar answered Nov 16 '22 04:11

eberhapa