我目前正在尝试使用Jquery的插件来处理这些资产。 但是我不知道如何让它像以前那样工作。
首先是我的webpack.config.js
var Encore = require('@symfony/webpack-encore');
Encore
.setOutputPath('public/assets/')
.setPublicPath('/assets')
.cleanupOutputBeforeBuild()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.addEntry('css/bootstrap','./src/Assets/Bootstrap/scss/bootstrap.scss')
.addEntry('js/jquery', './src/Assets/jquery-3.2.1.js')
.addEntry('js/bootstrap', './src/Assets/Bootstrap/dist/js/bootstrap.bundle.js')
.addEntry('js/common', './src/Assets/common.js')
.addEntry('css/backend','./src/Assets/backend.css')
.addEntry('css/admin','./src/Assets/admin.css')
.addEntry('css/frontend','./src/Assets/frontend.css')
.addEntry('css/bfcw','./src/Assets/bfcw.css')
.addEntry('fonts/fa','./src/Assets/FontAwesome/web-fonts-with-css/scss/fontawesome.scss')
.addEntry('fonts/fa-brand','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-brands.scss')
.addEntry('fonts/fa-regular','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-regular.scss')
.addEntry('fonts/fa-solid','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-solid.scss')
.addEntry('fonts/fa-light','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-light.scss')
.addEntry('js/growl', './src/Assets/growl/bootstrap-notify.js')
.enableSassLoader()
.enableSourceMaps(!Encore.isProduction())
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
;
module.exports = Encore.getWebpackConfig();
编译没有任何问题。
但我总是在我的控制台中收到以下消息
bootstrap.bundle.js:6286 Uncaught ReferenceError: $ is not defined
at Object.<anonymous> (bootstrap.bundle.js:6286)
at Object../src/Assets/Bootstrap/dist/js/bootstrap.bundle.js (bootstrap.js:6377)
at __webpack_require__ (bootstrap edfab3caa025dca10315:19)
at bootstrap edfab3caa025dca10315:62
at bootstrap edfab3caa025dca10315:62
Uncaught ReferenceError: $ is not defined
at list:108
我需要在twig模板中使用$(function(){....因为我需要从动态定义的方法中触发一些函数。 我不想将所有内容都放入js文件中,因为某些功能不应公开发布,因为它们是专门为管理员保留的。
此外,我不想将jquery包含在npm / yarn上,因为node_modules文件夹不应该被提交到生产环境中,并且它也不应该在webroot目录中可用。
我会提供任何帮助,并在必要时提供更多详细信息。
答案 0 :(得分:1)
在webpack-encore基本模板中,有以下一行:
// uncomment for legacy applications that require $/jQuery as a global variable
.autoProvidejQuery()
它将jQuery($)定义为全局变量,以便其他脚本可以使用它。这样你就不必为jQuery添加条目了。在这里我syour new webpack.config.js:
var Encore = require('@symfony/webpack-encore');
Encore
.setOutputPath('public/assets/')
.setPublicPath('/assets')
.cleanupOutputBeforeBuild()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.autoProvidejQuery()
.addEntry('css/bootstrap','./src/Assets/Bootstrap/scss/bootstrap.scss')
.addEntry('js/bootstrap', './src/Assets/Bootstrap/dist/js/bootstrap.bundle.js')
.addEntry('js/common', './src/Assets/common.js')
.addEntry('css/backend','./src/Assets/backend.css')
.addEntry('css/admin','./src/Assets/admin.css')
.addEntry('css/frontend','./src/Assets/frontend.css')
.addEntry('css/bfcw','./src/Assets/bfcw.css')
.addEntry('fonts/fa','./src/Assets/FontAwesome/web-fonts-with-css/scss/fontawesome.scss')
.addEntry('fonts/fa-brand','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-brands.scss')
.addEntry('fonts/fa-regular','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-regular.scss')
.addEntry('fonts/fa-solid','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-solid.scss')
.addEntry('fonts/fa-light','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-light.scss')
.addEntry('js/growl', './src/Assets/growl/bootstrap-notify.js')
.enableSassLoader()
.enableSourceMaps(!Encore.isProduction())
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
;
module.exports = Encore.getWebpackConfig();
有关Symfony博客专题文章jQuery and Legacy Applications的更多信息。
答案 1 :(得分:1)
感谢你的帮助,我能够解决这个问题。
我将所有内容都移到了主要的common.js中,这是所有其他网站的基本脚本:
var Encore = require('@symfony/webpack-encore');
Encore
.setOutputPath('public/assets/')
.setPublicPath('/assets')
.cleanupOutputBeforeBuild()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.autoProvidejQuery()
.addEntry('css/bootstrap','./src/Assets/Bootstrap/scss/bootstrap.scss')
.addEntry('js/common', './src/Assets/common.js')
.addEntry('css/backend','./src/Assets/backend.css')
.addEntry('css/admin','./src/Assets/admin.css')
.addEntry('css/frontend','./src/Assets/frontend.css')
.addEntry('css/bfcw','./src/Assets/bfcw.css')
.addEntry('fonts/fa','./src/Assets/FontAwesome/web-fonts-with-css/scss/fontawesome.scss')
.addEntry('fonts/fa-brand','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-brands.scss')
.addEntry('fonts/fa-regular','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-regular.scss')
.addEntry('fonts/fa-solid','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-solid.scss')
.addEntry('fonts/fa-light','./src/Assets/FontAwesome/web-fonts-with-css/scss/fa-light.scss')
.enableSassLoader()
.enableSourceMaps(!Encore.isProduction())
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
;
module.exports = Encore.getWebpackConfig();
我的common.js包括:
// require jQuery normally
const $ = require('jquery');
require('./growl/bootstrap-notify');
require('./Bootstrap/dist/js/bootstrap.bundle');
// create global $ and jQuery variables
global.$ = global.jQuery = $;
这样我也只有一个js文件,有点甜。 webpack让我有点兴奋,但我相信我能忍受它,现在我得到了与背景相关的子系统。 非常感谢
答案 2 :(得分:1)
您也可以将此用于您的公共文件(css或js):
{{1}}
然后你会有一个common.js和common.css