Last active
October 25, 2021 08:55
-
-
Save forsbergplustwo/a0df51122d1a0cc1d10d9edb7199d08c to your computer and use it in GitHub Desktop.
Adding Javascript from a Rails Engine, using importmap-rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# my_engine/my_engine.gemspec | |
spec.add_dependency "importmap-rails", "~> 0.7.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# my_engine/lib/my_engine.rb | |
require "importmap-rails" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# my_engine/lib/my_engine.rb | |
module MyEngine | |
class Engine < ::Rails::Engine | |
initializer "my_engine.importmap", after: "importmap" do |app| | |
app.importmap.draw(MyEngine::Engine.root.join("config/importmap.rb")) | |
end | |
initializer "my_engine.assets_and_hosts" do |app| | |
app.config.assets.paths << MyEngine::Engine.root.join("app", "assets", "javascripts") | |
app.config.assets.precompile += %w[ | |
my_engine_javascript.js | |
] | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# my_engine/config/importmap.rb | |
pin "my_engine_javascript", to: "my_engine_javascript.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/assets/javascripts/my_engine_javascript.js | |
export function hello(name) { | |
console.log(`Hello ${name}!`) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// APP (NOT ENGINE) | |
import { hello } from "my_engine_javascript" | |
hello("Engine") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment