Neon Tsunami

Processing ERB files with Rails Webpacker

July 20, 2017

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, written by Dwight Watson.