我正在将Rails应用程序从sprockets
迁移到webpacker
。我在布局中加载包时遇到问题。我必须手动包括包装中包含的软件包。这是预期的还是可以解决的?
错误
Uncaught Error: Include d3.js (v3.0.3 or greater) and topojson on this page before creating a new map
at new l (datamaps.usa.min.js:1)
at (index):226
(index):298 Uncaught ReferenceError: shortcut is not defined
at (index):298
app / javascript / packs / client_application.js
require("jquery")
require("shortcut.js/shortcut")
require("topojson")
require("d3/d3.min")
require("js/d3_tip")
client.haml(不起作用)
= javascript_pack_tag "client_application"
client.haml(有效)
= javascript_pack_tag "client_application"
= javascript_include_tag "shortcut.js/shortcut"
= javascript_include_tag "d3/d3.min"
= javascript_include_tag "topjson"
答案 0 :(得分:0)
一般来说,以下工作流程适用于我(以 d3
为例):
1.通过 yarn
yarn add d3
2.配置/webpack/environment.js
为刚刚添加的包创建一个别名:
const aliasConfig = {
'd3': 'd3/dist/d3.js'
};
environment.config.set('resolve.alias', aliasConfig);
别名必须指向包的主 JS 文件,相对于 node_modules
文件夹。
3. app/javascript/packs/application.js
添加导入:
import "d3"
奖金
作为替代方案,您可以跳过 #2 并在 app/javascript/packs/application.js
中添加额外的一行,将导入的库分配给全局窗口对象:
window.d3 = d3