Dwight Watson's blog

Processing ERB files with Rails Webpacker

This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.

When using Webpacker you may want to interpolate some values into your assets. For example, API keys are best to come from the deployment environment rather than being hard-coded into your assets. With Webpacker this is really easy, you can just append .erb to your scripts and import it.

First, import the script that requires processing including the .erb suffix.

import "./api.js.erb";

Then just include your embedded Ruby as you would usually.

export default new ApiClient({
apiKey: '<%= ENV['API_CLIENT_KEY'] %>'
});

A blog about Laravel & Rails by Dwight Watson;

Picture of Dwight Watson

Follow me on Twitter, or GitHub.