Symfony Webpack Encore,多个 JS/CSS 文件到一个文件 [英] Symfony Webpack Encore, Multiple JS/CSS Files to one file
问题描述
我想从多个 css/js 文件创建一个 css/js 文件.多个 addEntry 不起作用,请检查我的代码并给我解决方案.
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();
并添加常用的jQuery,添加js文件后,有些功能未定义,为什么?
And add common jQuery and after adding the js files some function is undefined, why?
推荐答案
您只需要一个 addEntry 调用.我用来执行此操作的解决方案是创建一个 main.js 文件,在其中导入所有文件.像这样的东西,例如:
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';
然后你可以像这样在你的 addEntry 中使用这个文件:
And then you can use this file in your addEntry like this:
.addEntry('app', './assets/main.js')
运行 Encore 后,你会得到一个 web/build/app.js 文件和 web/build/app.css 文件
After running Encore, you will get a web/build/app.js file and web/build/app.css file
这篇关于Symfony Webpack Encore,多个 JS/CSS 文件到一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!