我想从服务对象调用 Javascript 库。
我在/app/javascript/packs/readability.js
.
import Readability from '@mozilla/readability';
function articleBody(document) {
var article = new Readability(document).parse();
return article.content;
}
我发现 webpacker-ed 文件可以通过asset_pack_path
在 Views 中使用。我尝试将其添加到我的服务对象中,如下所示(使用therubyracer gem)。
(我尝试使用ExecJS,它似乎比 therubyracer 更受欢迎,但即使使用带有 ES5 Javascript 的未处理文件,它也只会返回错误。)
cxt = V8::Context.new
cxt.load(ActionController::Base.helpers.asset_pack_path 'readability.js')
puts cxt.eval("articleBody('http://example.com/')")
这是回归No such file or directory @ rb_sysopen - /packs/js/readability-20f57636.js
。
localhost:3000/packs/js/readability-20f57636.js
在浏览器中加载良好。
如何在服务对象中加载 webpacker 处理的文件?