Skip to content

Commit 23f7277

Browse files
committed
Remove rails-erb-loader and unify JS-generated files
We have two integrations between JavaScript and Rails: * js-routes * i18n-js Both allow using the same interface in JavaScript as their Ruby pendant. Previously, the setup of both was different and somewhat "hidden" (with temporary files). Now, we make the setup more obvious and put the generated files in a subdirectory of the main `app/javascript` folder. For the Raketask integration, we use the respective Ruby methods directly, rather than invoking a subprocess (improves performance).
1 parent b60738f commit 23f7277

File tree

13 files changed

+57
-60
lines changed

13 files changed

+57
-60
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,6 @@ yarn-debug.log*
8585
!.yarn/releases
8686
!.yarn/sdks
8787
!.yarn/versions
88+
89+
# Ignore automatically generated JavaScript files.
90+
/app/javascript/generated/*

app/javascript/application.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import 'jquery-ui/themes/base/sortable.css'
5858

5959
// I18n locales
6060
import { I18n } from "i18n-js";
61-
import locales from "../../tmp/locales.json";
61+
import locales from "./generated/locales.json";
6262

6363
// Fetch user locale from html#lang.
6464
// This value is being set on `app/views/layouts/application.html.slim` and
@@ -73,7 +73,7 @@ i18n.locale = userLocale;
7373
window.I18n = i18n;
7474

7575
// Routes
76-
import * as Routes from 'routes.js.erb';
76+
import * as Routes from 'generated/routes';
7777
window.Routes = Routes;
7878

7979
// ACE editor

app/javascript/routes.js.erb

Lines changed: 0 additions & 1 deletion
This file was deleted.

config/environments/development.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,7 @@
113113

114114
# Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
115115
config.generators.apply_rubocop_autocorrect_after_generate!
116+
117+
# Automatically update js-routes file when routes.rb is changed
118+
config.middleware.use(JsRoutes::Middleware)
116119
end

config/i18n.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
translations:
3-
- file: tmp/locales.json
3+
- file: app/javascript/generated/locales.json
44
patterns:
55
- "*"
66
- "!*.activerecord"

config/initializers/js_routes.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
# frozen_string_literal: true
22

3-
JsRoutes.setup do |config|
4-
config.documentation = false
5-
config.url_links = true
3+
JsRoutes.setup do |c|
4+
# Setup your JS module system:
5+
# ESM, CJS, AMD, UMD or nil.
6+
c.module_type = 'ESM'
7+
8+
# Follow javascript naming convention
9+
# but lose the ability to match helper name
10+
# on backend and frontend consistently.
11+
# c.camel_case = true
12+
13+
# Generate only helpers that match specific pattern.
14+
# c.exclude = /^api_/
15+
# c.include = /^admin_/
16+
17+
# Generate `*_url` helpers besides `*_path`
18+
# for apps that work on multiple domains.
19+
c.url_links = true
20+
21+
# Specify the file that will be generated.
22+
c.file = Rails.root.join('app/javascript/generated/routes.js')
23+
24+
# Include JSDoc comments in generated file.
25+
c.documentation = true
26+
27+
# More options:
28+
# @see https://github.com/railsware/js-routes#available-options
629
end

config/webpack/loaders/erb.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

config/webpack/webpack.config.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ const TerserPlugin = require("terser-webpack-plugin");
1010
const WebpackAssetsManifest = require('webpack-assets-manifest');
1111
const { SubresourceIntegrityPlugin } = require("webpack-subresource-integrity");
1212

13-
// Custom ERB loader to disable Spring and prevent crashes
14-
const erb = require("./loaders/erb");
15-
1613
// This setting will change the absolute path used to refer
1714
// external files (images, fonts, ...) in the generated assets
1815
const relative_url_root = process.env.RAILS_RELATIVE_URL_ROOT || '';
@@ -44,8 +41,7 @@ const envConfig = module.exports = {
4441
generator: {
4542
filename: 'icons/[hash].png'
4643
},
47-
},
48-
erb
44+
}
4945
]
5046
},
5147
optimization: {

lib/tasks/before_assets_tasks.rake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# frozen_string_literal: true
22

33
task before_assets_precompile: :environment do
4-
system('bundle exec i18n export')
4+
I18nJS.call(config_file: './config/i18n.yml')
5+
JsRoutes.generate!(typed: true)
56
end
67

78
# every time you execute 'rake assets:precompile'
89
# run 'before_assets_precompile' first
910
Rake::Task['assets:precompile'].enhance ['before_assets_precompile']
1011

1112
task before_assets_clobber: :environment do
12-
system('rm -rf ./tmp/locales.json')
13+
FileUtils.rm_rf('./app/javascript/generated/.', secure: true)
1314
end
1415

1516
# every time you execute 'rake assets:precompile'

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"opensans-webkit": "^1.1.0",
3838
"pnp-webpack-plugin": "^1.7.0",
3939
"propagating-hammerjs": "^3.0.0",
40-
"rails-erb-loader": "usabilityhub/rails-erb-loader#master",
4140
"rails_admin": "3.3.0",
4241
"sass": "^1.83.4",
4342
"sass-loader": "^16.0.4",

0 commit comments

Comments
 (0)