Rails 5.1 webpacker "import" the .js.erb file?

From app/javascript/packs/application.js

I'm trying import "../foo"

where the file is foo.js.erb

. Webpacker and yarn are great for other imports in application.js like import "../bar"

when this file bar.js

, but if I try with the file .js.erb

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

      

I have rails-erb-loader installed and if I look at the webpacker config then the erb loader is evaluated and looks at me, although I haven't touched any of them other than running rails webpacker:install

to create that config.

+4


source to share


2 answers


If you want to do this:

import '../foo'

      

When the actual file is foo.js.erb

, you need to update config/webpack/paths.yml

to include



- .js.erb

      

In the list of extensions. Otherwise, you need to fully specify the name of the file you are importing

import '../foo.js.erb'

      

+11


source


To add Erb support to your JS templates, run bundle exec rails webpacker:install:erb

in a Rails app already configured with Webpacker.



source (from cseelus comment)

0


source







All Articles