Skip to content

Commit 3f20a75

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 b6e2b06 commit 3f20a75

File tree

13 files changed

+56
-59
lines changed

13 files changed

+56
-59
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,6 @@ yarn-debug.log*
8282
!.yarn/releases
8383
!.yarn/sdks
8484
!.yarn/versions
85+
86+
# Ignore automatically generated JavaScript files.
87+
/app/javascript/generated/*

app/javascript/application.js

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

4747
// I18n locales
4848
import { I18n } from "i18n-js";
49-
import locales from "../../tmp/locales.json";
49+
import locales from "./generated/locales.json";
5050

5151
Promise.all(
5252
Object.keys(locales).map(locale => import(`select2/dist/js/i18n/${locale}`))
@@ -74,7 +74,7 @@ i18n.locale = userLocale;
7474
window.I18n = i18n;
7575

7676
// Routes
77-
import * as Routes from 'routes.js.erb';
77+
import * as Routes from 'generated/routes';
7878
window.Routes = Routes;
7979

8080
// 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: 0 additions & 4 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 || '';
@@ -45,7 +42,6 @@ const envConfig = module.exports = {
4542
filename: 'icons/[hash].png'
4643
},
4744
},
48-
erb,
4945
{
5046
// Exclude non-JS file from select2-i18n, otherwise breaking our dynamic language lookup.
5147
test: /select2\/dist\/js\/i18n\/build\.txt$/,

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
@@ -23,7 +23,6 @@
2323
"jquery-ui": "^1.14.1",
2424
"jquery-ujs": "^1.2.3",
2525
"mini-css-extract-plugin": "^2.9.2",
26-
"rails-erb-loader": "usabilityhub/rails-erb-loader#master",
2726
"rails_admin": "3.3.0",
2827
"sass": "^1.83.4",
2928
"sass-loader": "^16.0.4",

0 commit comments

Comments
 (0)