Symfony Webpack Encore,多个 JS/CSS 文件到一个文件

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

我想从多个 css/js 文件创建一个 css/js 文件。多个 addEntry 不起作用,请检查我的代码并提供解决方案。

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();

并添加common jQuery 并且在添加js文件后一些功能未定义,为什么?

您只需调用一次 addEntry。我使用的解决方案是创建一个 main.js 文件,我在其中导入所有文件。例如:

// 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';

然后您可以像这样在 addEntry 中使用此文件:

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

运行安可后,你会得到一个web/build/app.js文件和web/build/app.css文件

多个 .addStyleEntry 将创建多个文件。您可以将一个数组传递给 .addStyleEntry 以从多个 css/sass/less 文件中创建一个 css 文件,例如:

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

这将创建一个 style.css。数组条目的顺序与 css 文件中的输出匹配。