Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rails-erb-loader

usabilityhub215.8kMIT5.5.2

Embedded Ruby (.erb) webpack loader for Ruby projects.

erb, loader, webpack, ruby, rails

readme

rails-erb-loader

npm version npm downloads Standard - JavaScript Style Guide standard-readme compliant Build Status

Embedded Ruby (.erb) webpack loader for Ruby projects.

Compiles Embedded Ruby template files in any Ruby project. Files are built using either the Erubis or ERB gem.

Table of Contents

Install

npm

$ npm install rails-erb-loader --save-dev

yarn

$ yarn add -D rails-erb-loader

Usage

Add rails-erb-loader to your rules.

// webpack.config.js

module.exports = {
    module: {
      rules: [
        {
          test: /\.erb$/,
          enforce: 'pre',
          loader: 'rails-erb-loader'
        },
      ]
    }
  }
};

Now you can use .erb files in your project, for example:

app/assets/javascripts/UserFormFields.jsx.erb

/* rails-erb-loader-dependencies models/user models/image */

export default function UserFormFields() {
  return (
    <div>
      <label htmlFor='avatar'>
        Avatar
      </label>
      <ImageField id='avatar' maxSize={<%= Image::MAX_SIZE %>} />
      <label htmlFor='name'>
        Name
      </label>
      <input
        id='name'
        type='text'
        maxLength={<%= User::MAX_NAME_LENGTH %>}
      />
      <label htmlFor='age'>
        Age
      </label>
      <input
        id='age'
        type='number'
        min={<%= User::MIN_AGE %>}
        max={<%= User::MAX_AGE %>}
      />
    </div>
  )
}

Configuration

Options

Can be configured with UseEntry#options.

Option Default Description
dependenciesRoot "app" The root of your Rails project, relative to webpack's working directory.
engine "erb" ERB Template engine, "erubi", "erubis" and "erb" are supported.
runner "./bin/rails runner" Command to run Ruby scripts, relative to webpack's working directory.
timeoutMs 0 Timeout for the runner task in milliseconds. 0 is no timeout. Set this if you want a hanging runner to error out the build.
env process.env Environment variables to be passed to runner.

For example, if your webpack process is running in a subdirectory of your Rails project:

{
  loader: 'rails-erb-loader',
  options: {
    runner: '../bin/rails runner',
    dependenciesRoot: '../app',
  }
}

Also supports building without Rails:

{
  loader: 'rails-erb-loader',
  options: {
    runner: 'ruby',
    engine: 'erb'
  }
}

Dependencies

If your .erb files depend on files in your Ruby project, you can list them explicitly. Inclusion of the rails-erb-loader-dependency (or -dependencies) comment will tell webpack to watch these files - causing webpack-dev-server to rebuild when they are changed.

Watch individual files

List dependencies in the comment. .rb extension is optional.

/* rails-erb-loader-dependencies models/account models/user */

Watch a whole directory

To watch all files in a directory, end the path in a /.

/* rails-erb-loader-dependencies ../config/locales/ */

Contribute

Questions, bug reports and pull requests welcome. See GitHub issues.

License

MIT

changelog

Change Log

[5.5.2] - 2019-02-05

  • Fix broken unicode characters in output. - #77

[5.5.1] - 2019-01-03

  • Prevent webpack loader callback from being called multiple times - #76

[5.5.0] - 2018-08-28

  • Allow env to be specified in options - #70

[5.4.2] - 2018-05-08

  • Improve error reporting when killed by signal - #65

[5.4.1] - 2018-03-08

  • Fix error clearing timeout - #61

[5.4.0] - 2018-03-08

  • Remove input file limit by streaming instead of using a fixed size buffer - #56

[5.3.0] - 2018-03-01

  • Replace timeout option with timeoutMs - #55

[5.2.1] - 2017-08-25

  • Add timeout option - #46

[5.2.0] - 2017-08-25

UNPUBLISHED Accidentally published without pulling #46

[5.1.0] - 2017-07-28

  • Support Webpack 3 - #41
  • Add test suite - #41

[5.0.2] - 2017-06-13

  • Use execFile to spawn the source transformer - #37

[5.0.1] - 2017-05-16

  • Support projects in paths containing spaces - #35

[5.0.0] - 2017-04-01

  • Breaking Set default engine to ERB - #31
  • Add support for Erubi engine - #31

[4.0.0] - 2017-04-01

  • Breaking Remove support for webpack 1 style configuration under config.railsErbLoader - #28
  • Breaking Remove support for dependencies in configuration (only via config comments) - #29
  • Breaking Remove cacheable option and comment - all files are cacheable by default - #27
  • Breaking Error when a dependency comment points to a non-existant file/directory - #29
  • Support adding a directory as a depedency - #29
  • Better error handling on invalid runner option - #26
  • Skip parsing comments in production - #29

[3.2.0] - 2016-12-12

  • Add engine config option to specify templating engine - #21
  • Add runner config option to specify Ruby executable - #21
  • Deprecate rails config option in preference for more flexible runner

[3.1.0] - 2016-12-11

  • Added rails option - #20

[3.0.1] - 2016-11-30

  • Ensure support back to Node 0.10.0 - #13, #17
  • Remove dependency node-uuid - #15
  • Include MIT license text

[3.0.0] - 2016-11-19

  • Breaking Use Erubis instead of ERB gem to render templates. This gem is bundled by default with Rails 3.0 and above - #7

[2.0.0] - 2016-11-07

  • Breaking Rename project from uh-erb-loader to rails-erb-loader - #6
  • Breaking Add file caching by default - #4
  • Add support for query parameters
    • cacheable
    • dependencies
    • dependenciesRoot
    • parseComments
  • Add configuration comments
    • rails-erb-loader-depedencies
    • rails-erb-loader-depedencies-root (undocumented)
    • rails-erb-loader-cacheable

[1.1.1] - 2016-11-07

Deprecate uh-erb-loader in favor of rails-erb-loader.

[1.1.0] - 2016-05-26

  • Ignore unwanted output from Rails by delimiting desired output - #1

[1.0.0] - 2016-05-05

Initial release