【发布时间】:2021-06-28 02:51:00
【问题描述】:
我真的不敢相信让 JQuery 与 Rails 6 和 Webpacker 6 一起工作会如此困难。
Rails 6: How to add jquery-ui through webpacker? 中的建议似乎没有奏效,但很难知道它是否是同一个代码堆栈。
我正在使用 Rails 6.1 和 Webpacker 6.0 的预发布版本来让 Heroku 运行良好。哦,我的大部分“Javascript”都在 .coffee 文件中。
我什至尝试将 application.js 重命名为 application.coffee 并重新格式化,但也没有用。
我的 Gemfile 有
gem 'webpacker', '~> 6.0.0.beta.6'
我做了以下"
yarn add jquery jquery-ui-dist jquery-blockui
然后在 webpacker 6 样式中配置如下:
# config/webpacker/base.js
const { webpackConfig, merge } = require('@rails/webpacker')
const customConfig = require('./custom')
module.exports = merge(webpackConfig, customConfig)
# config/webpacker/custom.js
module.exports = {
resolve: {
alias: {
jquery: 'jquery/src/jquery',
jquery_ui: 'jquery-ui-dist/jquery-ui.js'
}
}
}
和
# code/app/packs/entrypoints/application.js
global.$ = require("jquery")
require("jquery") // Don't really need to require this...
require("jquery-ui")
require("jquery-ui-dist/jquery-ui")
这都是来自多个来源的尝试,包括这篇文章 - Rails 6: How to add jquery-ui through webpacker?、https://github.com/rails/webpacker 等。
顺便说一句,我正在尝试从 Rails 5 迁移我的 Coffescript,因此这广泛使用了 JQuery $ global。
非常感谢任何帮助。
【问题讨论】:
标签: javascript jquery ruby-on-rails webpack coffeescript