Rails 5.1 webpacker“导入".js.erb 文件? [英] Rails 5.1 webpacker "import" a .js.erb file?
问题描述
从 app/javascript/packs/application.js
我试图 import "../foo"
文件是 foo.js.erb
.Webpacker 和 yarn 非常适合 application.js 中的其他导入,例如 import "../bar"
当该文件是 bar.js
但如果我尝试使用.js.erb
文件我从 webpack-dev-server 得到这个错误:
From app/javascript/packs/application.js
I'm trying to import "../foo"
where the file is foo.js.erb
. Webpacker and yarn are working great for other imports in application.js, for example import "../bar"
when that file is bar.js
but if I try with a .js.erb
file I get this error from webpack-dev-server:
ERROR in ./app/javascript/packs/application.js
Module not found: Error: Can't resolve '../foo' in '/Users/blah/backlot/projects/test/app/javascript/packs'
@ ./app/javascript/packs/application.js 3:0-27
@ multi (webpack)-dev-server/client?http://localhost:8080 ./app/javascript/packs/application.js
我确实安装了 rails-erb-loader 并且如果我查看 webpacker 配置,正在评估 erb 加载器并且在我看来是正确的,尽管除了运行 rails webpacker 之外我没有触及任何其他内容:安装
以生成该配置.
I do have rails-erb-loader installed and if I look at the webpacker config, the erb loader is being evaluated and looks right to me although I didn't touch any of that except for running rails webpacker:install
to generate that config.
推荐答案
如果你想做的是:
import '../foo'
当实际文件为foo.js.erb
时,需要更新config/webpack/webpacker.yml
以包含
When the actual file is foo.js.erb
, you need to update config/webpack/webpacker.yml
to include
- .js.erb
在扩展列表中.否则你需要完全指定你要导入的文件名
In the list of extensions. Otherwise you need to fully specify the file name you are importing
import '../foo.js.erb'
这篇关于Rails 5.1 webpacker“导入".js.erb 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!